P
P
paevlk20072014-09-14 12:11:52
Django
paevlk2007, 2014-09-14 12:11:52

Django - create a form and go to its detail?

Hi everybody!
Why when creating a form:

def contact_add(request, template_name="contact_add.html"):
    if request.method == 'POST': 
        form = ContactForm(request.POST)
        if form.is_valid():
            # ...
            return HttpResponseRedirect(revers('contact_details')) 
    else:
        form = ContactForm()
    return render(request, template_name, {'form': form, })

def contact_details(request, contact_id, template_name="contact_details.html"):
    contact_details = get_object_or_404(Contact, id=contact_id)
    ctx = {'contact_details': contact_details}
    return render(request, template_name, ctx)
urls.py

url(r'^add/$', views.contact_add, name='accessories_add'),
url(r'^detail/(?P<contact_id>\d+)/$', views.contact_details, name='contact_details'),

Error: Reverse for 'contact_details' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['detail/(?Pcontact_id\d+)/$']
Apparently the form hasn't been saved yet, and we already require its id.
Is there a way out???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rrooom, 2014-09-14
@paevlk2007

...
obj = form.save()
...
return HttpResponseRedirect(reverse('contact_details', args=(obj.id)))
...

Read the boards more carefully. There is a mandatory argument in the url - Id of the record, reverse must guess which one?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question