python - Django:如何制作一个ListView,它将采用参数在不同的模板上构建问题列表?
问题描述
我正在从事的项目的目的是根据用户想要的选择来获取物理问题。第一个选择是想要的问题属于什么主题,另一个是问题属于什么类型的问题。然而,在不同在线资源的帮助下,我创建了表格,它们看起来很棒。但是现在我想从这些表单中获取结果,我知道我必须view.py
为结果创建一个并将action
我的HTML
文件中的GET
. 但我不知道视图会是什么样子。
这是model.py
from django.db 导入模型 from home.choices import *
# Create your models here.
class Topic(models.Model):
topic_name = models.IntegerField(
choices = question_topic_name_choices, default = 1)
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):
questions_type = models. IntegerField(
choices = questions_type_choices, default = 1)
question_topic = models.ForeignKey( 'Topic',
on_delete=models.CASCADE,
blank=True,
null=True)
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_type
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
这是form.py
包含选择字段的,我想从这些选择字段中获得结果。
from django import forms
from betterforms.multiform import MultiModelForm
from .models import Topic, Image, Question, Answer
from .choices import questions_type_choices, question_topic_name_choices
class TopicForm(forms.ModelForm):
topic_name = forms.ChoiceField(
choices=question_topic_name_choices,
widget = forms.Select(
attrs = {'class': 'home-select-one'}
))
class Meta:
model = Topic
fields = ['topic_name',]
def __str__(self):
return self.fields
class QuestionForm(forms.ModelForm):
questions_type = forms.ChoiceField(
choices= questions_type_choices,
widget = forms.Select(
attrs = {'class': 'home-select-two'},
))
class Meta:
model = Question
fields = ['questions_type',]
def __str__(self):
return self.fields
class QuizMultiForm(MultiModelForm):
form_classes = {
'topics':TopicForm,
'questions':QuestionForm
}
def save(self, commit=True):
objects = super(QuizMultiForm, self).save(commit=False)
if commit:
topic_name = objects['topic_name']
topic_name.save()
questions_type = objects['question_type']
questions_type.topic_name = topic_name
questions_type.save()
return objects
这是我views.py
的表格。
from django.shortcuts import render, render_to_response
from django.views.generic import CreateView
from home.models import Topic, Image, Question, Answer
from home.forms import QuizMultiForm
def QuizView(request):
if request.method == "POST":
form = QuizMultiForm(request.POST)
if form.is_valid():
pass
else:
form = QuizMultiForm()
return render(request, "index.html", {'form': form})
这是html
文件
{% extends 'base.html' %}
{% block content %}
<form method="GET" action="views_results.py">
{% csrf_token %}
{{ form.as_p }}
</form>
{% endblock content %}
这是我的urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('ask/', ask_page_url, name = 'ask_page'),
path('home/', home_page_url, name = 'home_page'),
path('download/', download_url, name = 'download_page'),
]
谢谢!
解决方案
您可以将变量从模板传递给模板
在主模板中
{% for question in questions %}
{% include "main/q.html" with question=question %}
{% endfor %}
并在 q.html
<h1>{{ question }}</h1>
推荐阅读
- node.js - AWS JS SDK 不从环境变量加载凭证
- node.js - nodejs - 压力测试 zeromq 套接字
- git - 将补丁集从 Gerrit 拉入当前分支
- c# - WPF:当 FontStyle 为 Oblique 时,剪切文本块中最后一个字母的部分
- javascript - 价值保持不变
- mysql - 如何根据mysql中的user_id对列的值进行加减运算
- java - Jbehave ExamplesTable 数组/列表作为参数
- python - Python sort() 负值问题
- python - lxml.html 以不同的方式处理两个相似的输入?
- python - SQLAlchemy 查询按月统计数据库中的所有记录