Page not found 404 on Django site?

前端 未结 13 2334
既然无缘
既然无缘 2020-12-14 00:46

I\'m following the tutorial on Django\'s site to create a simple poll app. However, Django is unable to resolve \"//127.0.0.1:8000/polls\" , even though I\'ve defined the re

13条回答
  •  我在风中等你
    2020-12-14 01:14

    Django unable to resolve 127.0.0.1:8000/polls because url config defined as r'^polls/'.

    Usual workaround:

    mySite/urls.py:

    from django.conf.urls import patterns, include, url
    from django.contrib import admin
    
    urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls')),
    )
    

    Note: Whenever Django encounters include(), It chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

    mySite/polls/urls.py:

    from django.conf.urls import patterns, url
    from polls import views
    
    urlpatterns = patterns('polls.views',
    url(r'^$', 'index', name='index'), 
    )
    

    Note: Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.

    Answer If

    If you want to access 127.0.0.1:8000/polls Note: without trailing slash

    use view based url

    url(r'^polls', 'polls.views.index', name='index'),
    

    So now you can access 127.0.0.1:8000/polls without trailing slash.

提交回复
热议问题