Pergunta

In my views.py I have the following code:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world!")

def show_day(request):
    return HttpResponse("The day is: test123")

and in my urls.py in the application folder, I have:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^/show_day/$', views.show_day, name='show_day'),

)

now, http://127.0.0.1:8000/myApp does return the index() view, but http://127.0.0.1:8000/myApp/show_day doesn't, but rather gives a 404 error.

What am I doing wrong? Is the problem with the regex in the URLconf?

Foi útil?

Solução

You have an unnecessary leading slash in the second URL. It should be

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

Outras dicas

In urls.py, remove the leading slash ('/') from the show_day regex:

# urls.py
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),

    # Don't lead with a slash!
    url(r'^show_day/$', views.show_day, name='show_day'),
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top