繁体   English   中英

如何使用选择字段不显示所有数据,而只显示与 Django 中的选择字段相关的数据?

[英]How to let not all the data show up using a choice field but only the ones related to the choice field in Django?

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

这是我正在使用的模型:

    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

这是视图文件:

    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'

顺便说一下,我如何将这个class添加到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正确? 所以需要朝这个方向发展。 您需要仔细检查 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM