【问题标题】:Django Tutorial Part 3 - Index.html not doing anythingDjango 教程第 3 部分 - Index.html 没有做任何事情
【发布时间】:2015-03-02 18:21:24
【问题描述】:

我正在编写 django 教程的第 3 部分,并且在设置 index.html 和视图之后,虽然没有发生任何事情。

这就是教程所说的应该发生的事情,“通过将浏览器指向“/polls/”来加载页面,您应该会看到一个项目符号列表,其中包含教程 1 中的“What's 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'),
)

网址.py

我不确定为什么什么都没发生,我可能做错了什么。有什么建议么?谢谢!

【问题讨论】:

  • 您的问题是您有多个索引方法。第一个看起来不错,但第二个正在替换第一个的行为。
  • 谢谢,就是这样。教程说要保持其他所有内容不变,虽然我现在看到它在谈论除其他索引之外的所有内容,所以我从未想过要删除它。
  • @dylrei 将其作为答案发布,以便 OP 可以接受。这显然是唯一的答案。
  • @Two-BitAlchemist 好的,完成。
  • @jddg5wa 请您接受有效的答案吗?除非您勾选绿色复选标记,否则清理 StackOverflow 的机器人女佣不会理解。 :)

标签: python django


【解决方案1】:

问题是你有不止一种索引方法。第一个看起来不错,但第二个正在取代第一个的行为。

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 2012-06-05
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2016-12-24
    • 2017-08-21
    • 2018-11-02
    相关资源
    最近更新 更多