0
我在Django 1.6中有一個簡單的問答應用程序,並且我無法在控制檯中打印question_id。提交的答案印刷得很好,但沒有運氣得到question_id。它不會拋出任何錯誤Django不打印_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 %}
圖。如果我堅持了幾個小時。我寧願Django拋出10個錯誤,也不願意在我的模板中輸入錯誤! – Shaun
好主意..我現在開始我的清單。 – Shaun
@needaname:請參閱[如何根據CC:WIKI?刪除我的帖子名稱](http://meta.stackexchange.com/q/96732)和[如何刪除我的帳戶?]( http://meta.stackexchange.com/q/5999)。當您發佈內容時,您將內容授權給Stack Exchange;憤怒 - 相當多的刪除通常會被還原。 –