0

我正在制作这个 django 应用程序,它是一个投票应用程序,因此它会显示问题,当您单击问题时它会显示选择。我正在使用本教程https://docs.djangoproject.com/en/1.4/intro/tutorial03 /

我收到的错误是,当我点击问题时它不显示选项。 在此处输入图像描述

但它显示在我的管理页面中 在此处输入图像描述

我的 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'

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

4

1 回答 1

2

更新模板中的这一行

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

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

因为你有choice_text领域而不是choice

于 2013-02-14T13:13:08.427 回答