Python 如何在django类视图中显示问题答案?
在models.py文件中,我有两个类:问答。在views.py文件中,我使用的是django.views.generic中的现成类表单。那么如何在detailview HTML中显示问题答案呢 models.py文件Python 如何在django类视图中显示问题答案?,python,django,Python,Django,在models.py文件中,我有两个类:问答。在views.py文件中,我使用的是django.views.generic中的现成类表单。那么如何在detailview HTML中显示问题答案呢 models.py文件 from django.db import models from django.conf import settings from django.utils import timezone from django.urls import reverse from django.
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
class Question(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField('Title', max_length=70)
content = models.TextField('Body', null=True, blank=True)
date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('current', kwargs={'pk': self.pk})
class Meta:
verbose_name_plural = 'Questions'
verbose_name = 'Question'
class Answer(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
content = models.TextField('Body', null=True, blank=True)
date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.content
class Meta:
verbose_name_plural = 'Answers'
verbose_name = 'Answer'
views.py
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Question, Answer
from django.shortcuts import redirect
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView,
DeleteView
)
def index(request):
context = {
'questions': Question.objects.all()
}
return render(request, 'f/index.html', context)
class QuestionListView(ListView):
model = Question
template_name = 'f/index.html'
context_object_name = 'questions'
ordering = ['-date']
class QuestionDetailView(DetailView):
model = Question
answers = Answer
class QuestionCreateView(LoginRequiredMixin, CreateView):
model = Question
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
问题_detail.html
在这个文件中是每个问题的详细信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ object.title }} - :) </title>
</head>
<body>
<img class="circle-img" src="{{ object.author.profile.image.url }}">
<p class="question-title">{{ object.title }}</p>
<p class="question-body">{{ object.content }}</p>
<p class="post-date">{{ object.date|date:"F d, Y" }}</p>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'question-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'question-delete' object.id %}">Delete</a>
</div>
<script>
console.log('You are the author of this question :3');
</script>
{% endif %}
</body>
</html>
{{object.title}}-:)
{{object.title}
{{object.content}
{{object.date | date:“fd,Y”}
{%if object.author==user%}
log(“您是这个问题的作者:3”);
{%endif%}
我已经在管理页面中创建了问题的答案您可以使用它从问题
实例获取答案
对象。像这样:
# template: question_detail.html
{% for answer in object.answer_set.all %}
{{ answer.content }}
{{ answer.author }}
{% endfor %}
是的,谢谢,但是如何在views.py中查看QuestionDetailView?您只需要从该视图中删除
answers=Answer
。这部分代码在那里没有任何功能。