On Aug 2, 3:27 pm, alan-l <alan.lough...@essexcricket.org.uk> wrote:
> Hi,
> i have the below that im thought would be the best approach to have a
> add contact page on my app:
>
> class ContactForm(ModelForm):
>     class Meta:
>         model = Contacts
>         fields = ('contact_type', 'firstname', 'surname', 'notes')
>
> def addcontact(request, request_id=0,
> template_name='addcontact.html'):
>     if request.POST:
>         form = ContactForm(request.POST)
>         form.save()
>         request_id = form.id
>         # If the save was successful, redirect to another page
>         redirect_url = reverse(addcontact, args=request_id)
>         return HttpResponseRedirect(redirect_url)
>     else:
>         form = ContactForm()
>         return render_to_response(template_name, {
>         'form': form, }, context_instance=RequestContext(request))
>
> i thought this would be the correct way to return the id of the newly
> inserted record based on the 1st page of the tutorial, but im getting
> the error: "'ContactForm' object has no attribute 'id'
> Although it currently just loads the same page, the eventual plan is
> to redirect to a url like /contacts/viewcontact/PRIMARYKEY
>
> i saw something similar in the admin "save and continue" where it
> retirects using the pk, so thought there would be an obvoius answer im
> missing. Would appreciate it if it could be pointed out  what im doing
> wrong.
>
> Thank you,
>
> Alan

You're almost there. The thing to remember is that `form.save()`
returns an instance of the model, and it's that returned value - not
the form itself - that gets the id. So you need:

        new_instance = form.save()
        request_id = new_instance.id
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to