我正在研究django教程的第3部分,并在设置index.html和视图之后,尽管没有发生任何事情。
这是教程应该发生的事情,“通过将浏览器指向”/ polls /“来加载页面,你应该会看到一个项目符号列表,其中包含教程1中的”What is up“问题。链接点到问题的详细页面。“
列表没有出现,我只看到“你好,世界。你在民意调查指数。”
这些是我的文件:
from django.http import HttpResponse
from django.shortcuts import render
from polls.models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
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 = "Your 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)
Views.py
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Index.Html(位于mysite / polls / templates / polls)
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
Urls.py
我不确定为什么没有发生任何事情,我可能做错了什么。有什么建议?谢谢!
答案 0 :(得分:4)
问题是您有多个索引方法。第一个看起来很好,但第二个正在取代第一个的行为。