I'm writing a site where users can ask questions. I don't understand what problem is in my code, but when I'm trying to show all answers on certain question - nothing appears on the screen. I tried to use everything: QuerySet with filter, get, using id, but nothing works. Seems like problem in two thing: Django doesn't understand what answers to show or my template is incorrect. Here is the code:
models.py
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=255, unique=True, db_index=True, verbose_name="URL")
detail = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('detail', kwargs={'slug': self.slug})
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
detail = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.detail
urls.py
urlpatterns = [
path('', QuestionView.as_view(), name='forum'),
path('search/', SearchView.as_view(), name='search'),
path('question/<slug:slug>/', QuestionDetailView.as_view(), name='detail'),
path('answer/', AnswerView.as_view(), name='answer')
]
views.py
class QuestionView(ListView):
model = Question
template_name = 'forum/forum.html'
context_object_name = 'questions'
ordering = ['-date_posted']
paginate_by = 1
class QuestionDetailView(DetailView):
model = Question
template_name = 'forum/detail.html'
class AnswerView(ListView):
model = Answer
template_name = 'forum/detail.html'
context_object_name = 'answers'
def get_queryset(self):
quest = Question.objects.get(id=1)
return Answer.objects.filter(question=quest)
templates/detail.html
{% for answer in answers %}
<div >
<p>{{ answer.detail }}</p>
<p><a href="#" >BigBossData</a><span>5 комментариев</span></p>
</div>
{% endfor %}
CodePudding user response:
Probably you should use a forward relation.
quest = Question.objects.get(pk=1)
answers = quest.answer_set.all()
CodePudding user response:
def get_queryset(self): quest = Question.objects.get(id=1) return Answer.objects.filter(question=quest)
Instead of using id=1, use pk=1
Btw since it always gets the first question with pk=1, the template will only and always show answers of question 1. Since you said "I'm trying to show all answers on certain question". So for this you need some changes.
URL:
urlpatterns = [
path('', QuestionView.as_view(), name='forum'),
path('search/', SearchView.as_view(), name='search'),
path('question/<slug:slug>/', QuestionDetailView.as_view(), name='detail'),
**path('answer/<slug:slug>/', AnswerView.as_view(), name='answer')**
]
views:
class AnswerView(ListView):
model = Answer
template_name = 'forum/detail.html'
context_object_name = 'answers'
def get_queryset(self):
**question_slug = self.kwargs['slug']
quest = Question.objects.get(slug=question_slug) # this will give you the question object, whose answers you want to show
return Answer.objects.filter(question=quest) # this will return all answers for the above question.**
Hope this helps