Django没有将_id打印到控制台

时间:2014-04-17 22:09:33

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

我在Django 1.6中有一个简单的问答应用程序,但我无法在控制台中打印question_id。提交的答案是打印得很好,但没有运气得到question_id。它没有抛出任何错误

这是我的models.py。

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Question(models.Model):
    user = models.ForeignKey(User)
    question = models.CharField(max_length=120)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    update = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.question

class Answer(models.Model):
    question = models.ForeignKey(Question)
    answer = models.CharField(max_length=120)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    update = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.answer

class UserAnswer(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    answer = models.ForeignKey(Answer, null=True, blank=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    update = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.answer.answer

这是我的views.py

from django.contrib import messages
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, RequestContext, Http404, HttpResponseRedirect

from .models import Question, Answer, UserAnswer

def all_questions(request):
    questions = Question.objects.all()

    if request.method == 'POST':
        print request.POST['question_id']
        print request.POST['answer']
    return render_to_response('questions/all.html', locals(), context_instance=RequestContext(request))

这是我的模板。

{% extends 'base.html' %}

{% block content %}
<div class='row'>

    {% for question in questions %}    
        {{ question }}<br/>
        <form method='POST' action=''>{% csrf_token %}
        <input type='hidden' vlaue='{{ question.id }}' name='question_id'/>
            {% for ans in question.answer_set.all %}
            <input type='radio' value='{{ ans }}' name='answer'>{{ ans }} <br/>
            {% endfor %}
            <input type='submit' value='Submit'>
        </form>
    {% endfor %}

</div>

{% endblock %}

1 个答案:

答案 0 :(得分:1)

你的问题很可能是一个错字:

{% extends 'base.html' %}

{% block content %}
<div class='row'>

    {% for question in questions %}    
        {{ question }}<br/>
        <form method='POST' action=''>{% csrf_token %}
        <input type='hidden' **vlaue**='{{ question.id }}' name='question_id'/>
            {% for ans in question.answer_set.all %}
            <input type='radio' value='{{ ans }}' name='answer'>{{ ans }} <br/>
            {% endfor %}
            <input type='submit' value='Submit'>
        </form>
    {% endfor %}

</div>

{% endblock %}

应该是:

{% extends 'base.html' %}

{% block content %}
<div class='row'>

    {% for question in questions %}    
        {{ question }}<br/>
        <form method='POST' action=''>{% csrf_token %}
        <input type='hidden' value='{{ question.id }}' name='question_id'/>
            {% for ans in question.answer_set.all %}
            <input type='radio' value='{{ ans }}' name='answer'>{{ ans }} <br/>
            {% endfor %}
            <input type='submit' value='Submit'>
        </form>
    {% endfor %}

</div>

{% endblock %}