Domanda

My urls.py is:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', redirect('home')),
    url(r"^home/$", 'my_project.views.user_login', name="home"),
    #url(r'^/$', redirect('home')),
)

I want to direct http://127.0.0.1:8000 and http://127.0.0.1:8000/home to same view for processing. I have other urlpatterns which I have not included above for simplicity. Currently, it throws the following error when I access this http://127.0.0.1:8000/home/:

ImproperlyConfigured at /home/
The included urlconf cmv_project.urls doesn't have any patterns in it
Request Method: GET
Request URL:    http://127.0.0.1:8000/home/
Django Version: 1.6
Exception Type: ImproperlyConfigured
Exception Value:    
The included urlconf cmv_project.urls doesn't have any patterns in it

when I comment-out the second urlpattern (url(r'^$', redirect('home')),), it works. I tried to switch the this urlpattern to the last. but it still did not help. Having this pattern, breaks other Urlpatterns, in the urls.py. I dont't know why? how to fix this?

È stato utile?

Soluzione

Try to attach the second url to the same view as the third url use something like this.

url(r"^$",my_project.views.user_login, name="home")

Another approach would be.

from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^/$', RedirectView.as_view(url=reverse_lazy('home'))),

Does this work??

Altri suggerimenti

I think it's similar to this question. Basically, when you use redirect with a name, it's going to use reverse to find it. See example 2 here. You'll need to use reverse_lazy instead.

To fix this, you could either do:

redirect(reverse_lazy('home'))

or:

redirect('home/')

Note that if you do the first option, you'll need to import reverse_lazy. If you do the second option, you're hard-coded which isn't ideal.

If we use the name mentioned in the urls.py within the redirect method, we must use within quote as like:

redirect(articles, year = "2045", month = "02") is wrong 

redirect('articles', year = "2045", month = "02") is right
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top