我的工作对这个项目的目的是根据用户想要什么样的选择,以获取物理问题。第一个选择是有用的问题属于什么话题,另外一个是什么类型的问题,这些问题属于。然而,用不同的在线资源的帮助下,我创建了形式和他们看起来很棒。但现在我希望得到的结果出来的这些形式,我知道我必须创造一个结果的view.py
,使action
我HTML
文件到GET
。但我不知道该视图会什么样子。
这是django.db进口车型的model.py
从home.choices导入*
# 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>