Я создаю приложение Django и сталкиваюсь с проблемами, когда маршруты не определены. Это то же самое приложение опроса DJango, которое я пытаюсь создать, но код документации не работает. Вот мой код ниже:
djangoproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^simpleapp/', include('simpleapp.urls')),
url(r'^admin/', admin.site.urls),
]
simpleapp/views.py
from django.shortcuts import render
from django.http import HttpResponse, request
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
simpleapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /simpleapp/
# url('', views.index, name='index'),
# ex: /simpleapp/5/
url('<int:question_id>/', views.detail, name='detail'),
# ex: /simpleapp/5/results/
url('<int:question_id>/results/', views.results, name='results'),
# ex: /simpleapp/5/vote/
url('<int:question_id>/vote/', views.vote, name='vote'),
]
Если я откомментирую первый URL-адрес пути simpleapp/urls.py
кода, все показанные страницы будут иметь путь. Однако, если я оставлю путь URL '' закомментированным, то маршруты приведут к следующей ошибке:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/simpleapp/34/
Using the URLconf defined in simple_django.urls, Django tried these URL patterns, in this order:
^simpleapp/ <int:question_id>/ [name='detail']
^simpleapp/ <int:question_id>/results/ [name='results']
^simpleapp/ <int:question_id>/vote/ [name='vote']
^admin/
The current path, simpleapp/34/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Мне не удалось получить path()
, импортированный с помощью django.conf.urls
или django.urls
. url()
прошло успешно. Я использую Python версии 3.6.7 и Django версии 2.1.5. Чего мне не хватает?
![enter image description here](https://i.stack.imgur.com/PSL46.png)
![enter image description here](https://i.stack.imgur.com/fDskW.png)
![enter image description here](https://i.stack.imgur.com/8Oy8F.png)
![enter image description here](https://i.stack.imgur.com/GUi7j.png)