Django:无法加载模板索引 [英] Django: can't load template index

查看:53
本文介绍了Django:无法加载模板索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Django 1.5教程:编写您的第一个Django应用程序.在第3部分中,它将介绍如何加载名为polls/index.html的模板.当我将浏览器指向"/polls/"时,但当我进入浏览器

i'm following the Django 1.5 tutorial: Writing your first Django app. In part 3, it teaches how to load a template called polls/index.html. It's supposed to show a bulleted-list containing "What's up", when i point to browser at "/polls/", but when I go to browser

http://localhost:8000/polls/

,页面只是空白.

这是我的民意调查/urls.py

here's my polls/urls.py

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<poll_id>\d+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
    )

这是我的民意调查/views.py

here's my polls/views.py

# Create your views here.

from django.http import HttpResponse
from django.template import RequestContext, loader

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = RequestContext(request, {
    'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(template.render(context))

def detail(request, poll_id):
    return HttpResponse("You're looking at poll %s." % poll_id)

def results(request, poll_id):
    return HttpResponse("You're looking at the results of poll %s." % poll_id)

def vote(request, poll_id):
    return HttpResponse("You're voting on poll %s." % poll_id)

这是我的index.html目录

here's my index.html's directory

mysite/polls/templates/polls/index.html

这是我的index.html

here's my index.html

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_pol_list %}
        <li><a href="/polls/{{poll.id}}">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

有人遇到同样的问题吗?

Anyone have the same problem?

THX !!!

推荐答案

首先不建议使用基于函数的视图.尝试基于类的视图,例如 django.views.generic.base.TemplateView .
如果要使用基于函数的视图, django.shortcuts.render 很有用:

First of all function-based views are deprecated. Try a class-based view like django.views.generic.base.TemplateView.
If you want to use function-based views django.shortcuts.render is useful:

from django.shortcuts import render

def index(request):
    return render(request, 'templates/index.html', {'extra_context' : 'hello'}) 

get_template 返回 TemplateDoesNotExist 异常,因为您错过了投票"中的"l":

get_template returns a TemplateDoesNotExist exception because you missed an 'l' in 'poll':

{% for poll in latest_pol_list %} 

我不会删除余下的帖子,因为这对您来说是一种好习惯,希望您会发现它有用.

I'm not removing the rest of my post because it's good practice for you, hope you will find it useful.

这篇关于Django:无法加载模板索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆