Django模板无法加载

时间:2017-09-25 21:39:46

标签: python django templates django-templates django-views

遵循Django网站的教程,目前在Pt3(https://docs.djangoproject.com/en/1.11/intro/tutorial03/

教程指出:“此代码应创建一个项目符号列表,其中包含教程2中的”内容为何“的问题。链接指向问题的详细信息页面。” 添加模板并运行服务器后,我仍然只看到这个 previous page and no updated template 这有什么不对?谢谢你的帮助!

django_proj / mysite的/ mysite的/ urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]

django_proj / mysite的/轮询/ urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    #ex: /polls/
    url(r'^$', views.index, name='index'),

    # 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'),
]

django_proj / mysite / polls / views.py

from django.shortcuts import render
    from django.http import HttpResponse
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,
    }
    return HttpResponse(template.render(context, request))

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)
def index(request):
        return HttpResponse("At Polls Index")

/django_proj/mysite/polls/templates/polls/index.html

{% 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 %}

类似的问题:Django Template not loading

1 个答案:

答案 0 :(得分:1)

views.py 删除这个,因为你已经定义了2次。

def index(request):
    return HttpResponse("At Polls Index")