Sunday, July 7, 2013

Re: type object 'DataTable' has no attribute 'datFile'

On Saturday, 6 July 2013 12:07:34 UTC+1, Nigel Legg wrote:
I have the following code:
models.py:
class DataTable(models.Model):
    datFile = models.CharField(max_length = 200)
    structFile = models.CharField(max_length = 200)
    bannerVar = models.CharField(max_length = 50)
    stubVar = models.CharField(max_length = 50)
    stubNets = models.BooleanField()
   
    def __unicode__(self):
        return u'%s %s %s %s' % (self.datFile, self.structFile, self.bannerVar, self.stubVar)

forms.py:
class DataTableForm(forms.ModelForm):

    class Meta:

        model = DataTable
   
views.py:
def datatables(request):
    if request.method == 'POST':
        form = DataTableForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('myapp/tabspec.html')
    else:
        form = DataTableForm()
    return render(request, 'myapp/datatables.html', {
        'form': form,
    })

def tabspec(request):
    try:
        datFile = DataTable.datFile
        context = Context({
            'datFile': datFile,
        })
    except DataTable.DoesNotExist:
        raise Http404
    return HttpResponse(template.render(context))

The DataTable view shows, as a form; I enter the values for the ields, when I click the button, I get the error "type object 'DataTable' has no attribute 'datFile'".  I thought maybe I should be referring to DataTableForm.datFile, but this gave the same error.
I am confused, could any one tell me where I am going wrong?

datFile (terrible name, by the way) is a field on a DataTable *instance*. You need to pass an ID value to the tabspec view (via the URL) from your form view, then use DataTable.objects.get(pk=id) in that tabspec view.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate