简体   繁体   English

我想知道如何为我现有的模型表单制作基于类的视图。 我需要将一个模型表单重定向到另一个模型表单

[英]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

These contains semester, subject, assesment etc, which are all linked to each other by foreign key 这些包含学期,科目,评估等,它们都通过外键相互链接

models.py models.py

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

after submitting each form, in the vews, they must redirect to next modelform 提交每个表格后,他们必须重新定向到下一个模型表格

forms.py 表格

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',]

how should I write the views such that I can get data and redirect to the next model form using a template? 我应该如何编写视图以便可以获取数据并使用模板重定向到下一个模型表单? please help. 请帮忙。 Thank you 谢谢

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.

相关问题 我想使用django模型表单生成4级下拉表单 - I would like to generate 4 levels dropdown form using django model forms 如何在Django的views.py中的一个类下显示模型和表单? - How can I display a model and a form under one class in views.py in Django? 我需要在Django模型中执行诸如form.save(commit = False)之类的操作 - I need to do something like form.save(commit=False) in my model in Django 如何使用基于类的模型在 django 管理表单中制作自动完成过滤器 - How to make autocomplete filter in django admin form with class based model 如何在登录用户的视图中保存 django Model 表单 - How can I save a django Model Form in views for the logged in user 我扩展了用户模型,但我不知道如何通过表单从其他用户那里获得价值 - I extended User Model and I don't know how to get value of it from other users through forms Django-summernote:如何使它基于模型形式工作? - Django-summernote : How can I make it work based on model form? 我已经创建了我的模型,但我对创建视图以将我的数据从提交的表单保存在数据库中感到困惑 - I have created my model but im confused with creating views to save my data from submitted form in database 如何制作 Django model 表单,其字段名称与 model 字段名称不同? - how can I make a Django model form with a field name in the form different from the model field name? 如何在基于 Django 类的 ListView 中生成模型表单中的 10 个唯一数字并传递表单上下文变量 - How can I Generate 10 Unique digits in Model Form and Pass Form Context Variable in Django Class Based ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM