gpt4 book ai didi

python - 如何让Django中的选择字段不显示所有数据,而仅显示与选择字段相关的数据?

转载 作者:行者123 更新时间:2023-12-01 08:18:05 26 4
gpt4 key购买 nike

我有这个网站,其目的是根据用户选择的相关主题查看数据库中的数据。我已经设法使数据显示出来,但是当我单击查看按钮时,数据库中找到的所有数据都会显示出来,而不注意所选主题。我不确定这是因为我组织数据库的方式还是我的表单有问题。

这是我正在使用的模型:

    from django.db import models
from home.choices import *

# Create your models here.

class Topic(models.Model):
topic_name = models.IntegerField(
choices = question_topic_name_choices, default = 1)
topic_question = models.ForeignKey('Question',
on_delete=models.CASCADE,
blank=True,
null=True)
topic_answer = models.ForeignKey('Answer',
on_delete=models.CASCADE,
blank=True,
null=True)
def __str__(self):
return '%s' % self.topic_name

class Image (models.Model):
image_file = models.ImageField()

def __str__(self):
return '%s' % self.image_file

class Question(models.Model):
question_description = models.TextField()
question_answer = models.ForeignKey( 'Answer',
on_delete=models.CASCADE,
blank=True,
null=True)
question_image = models.ForeignKey( 'Image',
on_delete=models.CASCADE,
blank=True,
null=True)

def __str__(self):
return '%s' % self.question_description

class Answer(models.Model):
answer_description = models.TextField()
answer_image = models.ForeignKey( 'Image',
on_delete=models.CASCADE,
blank=True,
null=True)
def __str__(self):
return '%s' % self.answer_description

这是 View 文件:

    from django.shortcuts import render, render_to_response, redirect
from .choices import *
from django.views.generic import TemplateView
from home.models import Topic, Image, Question, Answer
from home.forms import TopicForm


class QuizView(TemplateView):
template_name = "index.html"

def get(self, request):
form = TopicForm()
args = {"form": form}
return render(request, self.template_name, args)

def post(self, request):
form = TopicForm(request.POST)
if form.is_valid():
form = TopicForm()
posts = Question.objects.all()
args = {"form": form, "posts": posts}
return render(request, self.template_name, args)

这是表单文件:

    from django import forms
from betterforms.multiform import MultiModelForm
from .models import Topic, Image, Question, Answer
from .choices import question_topic_name_choices

class TopicForm(forms.ModelForm):

class Meta:
model = Topic
fields = ['topic_name',]

# 'class': 'home-select-one'

By the way how can I add this class into the ChoiceField?

这是 html 文件:

        {% extends 'base.html' %}
{% block content %}
<h4>International Baccalaureate Physics</h4>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" id="home-Physics-time-button">It is Physics Time</button>
</form>
{% for post in posts %}
<table style="margin: 10px auto; width: 90%; line-height: 35px;">
<tbody>
<tr>
<td style = "border: 2px solid #ffffff; padding: 1%;"> <strong>Question:</strong> <br>{{ post.topic_question}}</td>
</tr>
<tr>
<td style = "border: 2px solid #ffffff; padding: 1%;"> <strong>Answer:</strong> <br>{{ post.topic_answer }}</td>
</tr>
</tbody>
</table>
{% endfor %}

{% endblock content %}

提前谢谢您!

最佳答案

在您的 view.py 中,您想要应用在 TopicForm 中所做的选择,正确吗?所以需要这个方向的东西。您需要仔细检查 TopicForm 的 cleaned_data 的具体内容。

from django.shortcuts import render, render_to_response, redirect
from .choices import *
from django.views.generic import TemplateView
from home.models import Topic, Image, Question, Answer
from home.forms import TopicForm


class QuizView(TemplateView):
template_name = "index.html"

def get(self, request):
form = TopicForm()
args = {"form": form}
return render(request, self.template_name, args)

def post(self, request):
form = TopicForm(request.POST)
posts = Question.objects.all()
if form.is_valid():
# Apply the selected topic as filter on all the posts.
# TODO: Figure out what the exact value is you get from the form.
posts = posts.filter(question_topic__topic_name=form.cleaned_data['topic_name'])
args = {"form": form, "posts": posts}
return render(request, self.template_name, args)

关于python - 如何让Django中的选择字段不显示所有数据,而仅显示与选择字段相关的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54861494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com