Monday, July 22, 2013

Re: Saving data from a form.

Thanks Christian. I'll get my head around this soon, I hope.



On 22 July 2013 14:53, Christian Erhardt <christian.erhardt@mojo2k.de> wrote:
The error lies in this two rows:

form.save()
DataTable_id = DataTable.id

DataTable is the class, not the instance of the saved object! form.save() returns an instance of what has been saved. So your code should look like this:

my_new_datatable_instance = form.save()

That should do the trick


Am Montag, 22. Juli 2013 13:02:46 UTC+2 schrieb Nigel Legg:
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()
            DataTable_id = DataTable.id
            return render(request, 'myapp/tabspec/{{DataTable_id}}.html', {
                'DataTable_id' : DataTable.id,
                })
    else:
        form = DataTableForm()
    return render(request, 'myapp/datatables.html', {
        'form': form,
    })


When I input data on the form, and click "Confirm Selections", I get
"object DataTable has no attribute 'DataTable_id'"
Should I be using a different method to save the data from the form to the database?

--
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.
 
 

--
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