David Dahan
David Dahan

Reputation: 11172

How to avoid hardcoded url in Django view in that case?

I know we usually reverse(someview) to avoid hardcoded urls in views.

But in my case:

@user_passes_test(is_logged_owner, login_url=reverse(signin_owner))
def view_1(request):
    # stuff...

def signin_owner(request):
    # stuff...

This does not work, this raises an ImproperlyConfigured error at any URL (message is : The included urlconf hellodjango.urls doesn't have any patterns in it).

I have no idea of what's happening since views seems to be well defined in urls.py (and I never had mistakes like this in 6 months).

root urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('myapp.urls')),
)

myapp urls.py (part of it):

url(r'^signin_owner$', views.signin_owner, name='signin_owner'),

Thanks!

Upvotes: 1

Views: 339

Answers (1)

Sudip Kafle
Sudip Kafle

Reputation: 4391

Your are using URL reversal before your URLconf is loaded due to which reverse wouldn't work. The solution to this is using reverse_lazy instead.

Now your view will be something like this:

@user_passes_test(is_logged_owner, login_url=reverse_lazy('signin_owner',))
def view_1(request):
    # stuff...

Upvotes: 3

Related Questions