[英]I would like to know how to make class based views for my existing model forms. I need to redirect the form one model form to the other
from django.db import models
class Semester(models.Model):
S_OPTIONS = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
)
sem_type=models.CharField(max_length=1, choices=S_OPTIONS)
def __str__(self):
return self.sem_type
class Subject(models.Model):
S_OPTIONS = (
('Math','Math'),
('English','English'),
('C++','C++'),
('Thermo','Thermo'),
('Electronics', 'Electronics'),
)
sub_type = models.CharField(max_length=100, choices= S_OPTIONS)
sem = models.ForeignKey(Semester, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.sub_type
class Assesment(models.Model):
A_OPTIONS = (
('1','CT1'),
('2','CT2'),
('3','CT3'),
)
assess_type=models.CharField(max_length=1, choices=A_OPTIONS)
sub = models.ForeignKey(Subject, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.assess_type
class Paper(models.Model):
PA_OPTIONS = (
('1','1'),
('2','2'),
)
paper_type = models.CharField(max_length=1, choices=PA_OPTIONS,default=1)
assesment = models.ForeignKey(Assesment, on_delete=models.CASCADE, default=1)
# def get_absolute_url(self):
# return reverse('teacher:home',kwargs={'pk': self.pk})
def __str__(self):
return self.paper_type
class Part(models.Model):
P_OPTIONS = (
('A','A'),
('B','B'),
('C','C'),
)
part_type = models.CharField(max_length=1, choices=P_OPTIONS)
paper= models.ForeignKey(Paper, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.part_type
class Question(models.Model):
a = models.TextField()
part= models.ForeignKey(Part,on_delete=models.CASCADE, default=1)
def __str__(self):
return self.a
class Choice(models.Model):
c = models.TextField(null= True)
question = models.ForeignKey(Question, on_delete =models.CASCADE,default=1, null =True)
def __str__(self):
return self.c
from django import forms
from .models import Semester,Subject,Assesment,Paper,Part,Question,Choice
from django.contrib.auth.models import User
class SemesterForm(forms.ModelForm):
class Meta:
model = Semester
fields = ['sem_type',]
class SubjectForm(forms.ModelForm):
class Meta:
model = Subject
fields = ['sub_type',]
class AssesmentForm(forms.ModelForm):
class Meta:
model = Assesment
fields = ['assess_type',]
class PaperForm(forms.ModelForm):
class Meta:
model = Paper
fields = ['paper_type',]
class PartForm(forms.ModelForm):
class Meta:
model = Part
fields = ['part_type',]
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
fields = ['a',]
class ChoiceForm(forms.ModelForm):
class Meta:
model = Choice
fields = ['c',]
You can use FormView
, like the below example, for each form you want the user to fill. 可以像下面的示例一样使用FormView
来为用户要填写的每个表单使用。
from django.http import HttpResponseRedirect
from django.views.generic import FormView
from django.core.urlresolvers import reverse_lazy
from .forms import SemesterForm
class SemesterFormView(FormView):
success_url = reverse_lazy("<your next view>")
template_name = "<your template name>"
form_class = SemesterForm
def form_valid(self, form):
form.save() # or any other business logic you need
return HttpResponseRedirect(self.get_success_url())
This way you use the success redirect to send the user to the view of that renders the next form. 这样,您可以使用成功重定向将用户发送到呈现下一个表单的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.