Page not found 404 on Django site?

前端 未结 13 2300
既然无缘
既然无缘 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:05

    Page not found?

    If you get an error page here, check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/.

    Source : https://docs.djangoproject.com/en/3.0/intro/tutorial01/

    0 讨论(0)
  • 2020-12-14 01:11

    You put the urls.py folder into the outer MySite folder, you are suppose to put it in the inner one so its not mySite/urls.py, but mySite/mySite/urls.py:

    ran into the same mistake when i did the tutorial

    0 讨论(0)
  • 2020-12-14 01:14

    I had the same problem.

    It turns out I was confused because of the multiple directories named "mysite".

    I wrongly created a urls.py file in the root "mysite" directory (which contains "manage.py"), then pasted in the code from the website.

    To correct it I deleted this file, went into the mysite/mysite directory (which contains "settings.py"), modified the existing "urls.py" file, and replaced the code with the tutorial code.

    In a nutshell, make sure your urls.py file is in the right directory.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-14 01:15

    Another way to access 127.0.0.1:8000/polls would be to redirect the browser when accessing 127.0.0.1:8000. It is done by editing .../mysite/mysite/urls.py as follows:

    from django.conf.urls import include, url
    from django.contrib import admin
    from polls import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^polls/', include('polls.urls', namespace='polls')),
        url(r'^$', views.index, name='index'),
    ]
    
    0 讨论(0)
  • 2020-12-14 01:20

    To make the answer clear for beginners who has this issue by following the tutorial, the project root URLconf is the one in the same folder as settings.py which is:

     mysite/mysite/urls.py
    

    Just make sure import 'include'. The code looks like:

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

    So in mysite/mysite/settings.py:

    The line should be:

    ROOT_URLCONF = 'mysite.urls'

    You don't need create a fresh new root URLconf.

    0 讨论(0)
提交回复
热议问题