

檔案結構如上圖,使用正則運算式r'^$',url(r'^$', views.index, name='index'),無法打開localhost:8000\polls\index頁面, 而改成
url('', views.index, name='index')就可以了,請問這是什么原因?而且這個url還必須放在最下面,如果放在其他幾個url之上,那么
打開index之后,再打開其他頁面都顯示index頁面,請高手指點一下,這又是怎么回事?
polls\urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from mysite import view
from . import views
urlpatterns = [
# path('',views.polls),
# ex: /polls/,
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
url(r'^$', views.index, name='index'),
]
polls\views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.http import Http404
# Create your views here.
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
#output = ', '.join([q.question_text for q in latest_question_list])
return render(request,'polls/index.html',context)
.....
uj5u.com熱心網友回復:
哦,原來這里不能輸入localhost:8000/polls/index,而要輸入localhost:8000/polls/,如果非要加上index,則必須把r'^$', 改成r'',并且把這條url放在最后面,不知道什么原因轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/110833.html
上一篇:求python大神這種題怎么做.
下一篇:求神幫忙,跪謝
