How To Let Not All The Data Show Up Using A Choice Field But Only The Ones Related To The Choice Field In Django?
I have this website which has one purpose which is viewing data out of a database according to the related topic chosen by the user. I have managed to make the data show up, but all the data found in the database is showing up when I click on the viewing button paying no attention to the chosen topic. I am not sure if it is because of how I have organized the database or if the problem is with my forms.
This is the model that I am using:
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
This is the views file:
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)
This is the forms file:
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 theChoiceField
?
This is the html file:
{% 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 %}
Thank you in advance!
Answer
In your view.py, you want to apply the selection made in the TopicForm
correct?
So something in this direction is needed. You'll need to double check what the cleaned_data
of the TopicForm has exactly.
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)
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module