I work on a small app in Django, a website where a student puts a question using a form and the teacher will answer from admin site. The problem is that I can not display the answer and I don't know what I'm doing wrong. I attach models, template and view.
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>
The error i get is : 'function' object has no attribute 'answer'. Can anyone help me to understand how to display the answers? I read a lot of articles but nothing helped. Thanks.