我在Django的一个小应用程序上工作,这是一个学生使用表单提问的网站,老师会从管理站点回答。问题是我无法显示答案,我不知道我做错了什么。 我附上模型,模板和视图。
models.py
from django.db import models
from datetime import datetime
from django.db.models import CASCADE
class Question(models.Model):
title = models.CharField(max_length=120)
question_content = models.TextField()
date = models.DateTimeField(blank=True, null=True, default=datetime.now)
class Meta:
ordering = ('-date',)
def __str__(self):
return self.title
class Answer(models.Model):
question = models.ForeignKey(Question, related_name='answers', on_delete=CASCADE)
answer_content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created',)
def __str__(self):
return 'Answer to {}'.format(self.question)
views.py
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import ListView
from registration.forms import RegistrationForm, QuestionForm
from registration.models import *
def question(request):
if request.method == 'POST':
form = QuestionForm(request.POST)
if form.is_valid():
form.save()
questions = Question.objects.all()
return render(request, 'posturi.html', {'question': questions})
else:
# if post request is not true, returning blog
form = QuestionForm()
return render(request, 'blog.html', {'form': form})
def question_list(request):
questions = Question.objects.all()
answers = question.answers // this is the problem i can not solve
paginator = Paginator(questions, 6) # 6 question in each page
page = request.GET.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
questions = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
questions = paginator.page(paginator.num_pages)
return render(request, 'posturi.html', {'question': questions, 'answer': answers})
posturi.html
<div class="container">
<div class="row justify-content-center col-md-10 col-md-offset-1">
{% for questions in question %}
<div class=" card ">
<div class="card-header"><h3>{{ questions.title }} :</h3></div>
<hr>
<div class="card-text">{{ questions.question_content | linebreaks }}</div>
<div class="card-footer"><h4>{{ questions.date }}</h4></div>
{% for answers in question.answer_set.all %}
<div class="card-header"><h3>Raspuns la : "{{ answers.question.title }}"</h3></div>
<hr>
<div class="card-text">{{ answers.answer_content | linebreaks }}</div>
<div class="card-footer"><h4>{{ answers.created }}</h4></div>
{% empty %}
<div class="card-text"><h4>Fara raspuns.</h4></div>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
我得到的错误是:'function'对象没有属性'answer'。任何人都可以帮我理解如何显示答案吗?我读了很多文章,但没有任何帮助。 谢谢。
答案 0 :(得分:0)
应该是question.answers.all()而不仅仅是question.answers。有关详细信息,请查看here。
答案 1 :(得分:0)
question
是您在views.py
中定义的功能,这就是错误说明的原因
&#39;功能&#39;对象没有属性&#39;回答&#39;
您可能希望使用上一行中定义的问题
def question_list(request):
questions = Question.objects.all()
answers = questions.answers
答案 2 :(得分:0)
感谢您的时间;终于我找到了正确的答案: 为了得到显示的答案,我使用'select_related()',如:
def问题列表(请求): 问题= Question.objects.all() 答案= Answer.objects.select_related()
返回渲染(请求,'posturi.html',{'question':问题,'answer':答案}
和在模板中: {%代表答案%%} .... {%endfor%}
也许有人需要这个。