Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lookup a named url in a view?

I want to fill out the success_url of a django form view using a named url:

class RegisterView(generic.edit.FormView):
    template_name = "cmmods/register.html"
    form_class = RegisterForm
    #success_url = reverse('complete-registration')
    success_url = "/cmmods/complete-registration"

When I type the URL explicitly, as uncommented above, it works.

When I try to do a reverse lookup of the url (currently commented out, above) , I get:

The included urlconf 'cm_central.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

I guess it's clear that my urls.py is actually valid (does have patterns in it), since the uncommented version of the code works.

How should I be doing this?

like image 521
GreenAsJade Avatar asked Dec 25 '22 01:12

GreenAsJade


1 Answers

That means the dependency has not been loaded yet. You can use reverse_lazy to defer the evaluation of the url pattern

Like this:

success_url = reverse_lazy('complete-registration')

Documentation of reverse_lazy can be found here

like image 141
karthikr Avatar answered Dec 27 '22 14:12

karthikr