Django错误选择不显示

时间:2013-02-14 13:06:08

标签: django

我正在制作这个django应用程序,它是一个民意调查应用程序,所以它显示问题,当你点击问题时它会显示选项。我正在使用这个教程https://docs.djangoproject.com/en/1.4/intro/tutorial03/

我收到的错误是,当我点击问题时,它不显示选项。 enter image description here

但它显示在我的管理页面中 enter image description here

我的detail.html模板是

<h1>{{ poll.question }}</h1>
<ul>
{% for choice in poll.choice_set.all %}
    <li>{{ choice.choice }}</li>
{% endfor %}
</ul>

我的views.py是

from django.http import HttpResponse
from myapp.models import Poll ,choice
from django.template import Context, loader
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404


def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    return render_to_response('myapp/index.html', {'latest_poll_list':     latest_poll_list})



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)

def detail(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('myapp/detail.html', {'poll': p})

我的模特是:

from django.db import models
from django.contrib import admin
class Poll(models.Model):
    question=models.CharField(max_length=200)
    pub_date=models.DateTimeField('date published')
    def __unicode__(self):
        return self.question



# Create your models here.
class choice(models.Model):
    poll=models.ForeignKey(Poll)
    choice_text=models.CharField(max_length=200)
    votes=models.IntegerField(default=0)
    def __unicode__(self):
        return self.choice_text
class ChoiceInline(admin.TabularInline):
    model = choice 
    extra = 3
class PollAdmin(admin.ModelAdmin):
    fields = ['pub_date','question']
    inlines=[ChoiceInline]
    list_display =('question','pub_date')
    list_filter=['pub_date']
    search_fields=['question']
    #date_hierarchy='pub_date'

我不知道如何解决这个问题

1 个答案:

答案 0 :(得分:2)

在模板中更新此行

<li>{{ choice.choice }}</li>

<li>{{ choice.choice_text }}</li>

因为您有choice_text字段而不是choice