Django 使用formview的基于类的视图的调用post函数

Django 使用formview的基于类的视图的调用post函数,django,django-class-based-views,formview,Django,Django Class Based Views,Formview,我对基于django类的视图还不熟悉,可能我的处理方式有点幼稚,因此如果您能提出更好的方法,我将不胜感激 所以我的问题是: 我的项目中有三种类型的用户。1.学生,2。老师,3。父母亲当用户以各自的形式请求设置页面时,我需要能够显示与每种类型用户相关的不同用户设置。此外,我需要能够在用户提交表单时将数据保存到相应的表中 我有一个基于类的视图(UserSettingsView): 使用pick_settings通用视图调用Usersettings视图的不同实例 url(regex=r'^profil

我对基于django类的视图还不熟悉,可能我的处理方式有点幼稚,因此如果您能提出更好的方法,我将不胜感激

所以我的问题是:

我的项目中有三种类型的用户。1.学生,2。老师,3。父母亲当用户以各自的形式请求设置页面时,我需要能够显示与每种类型用户相关的不同用户设置。此外,我需要能够在用户提交表单时将数据保存到相应的表中

我有一个基于类的视图(UserSettingsView):

使用pick_settings通用视图调用Usersettings视图的不同实例

url(regex=r'^profilesettings/',view=pick_settings,name='profilesettings'),
这是“拾取设置”视图:

def pick_settings(request):
    if request.user.is_authenticated():
        if request.method == 'GET':
            if request.user.profile.is_student:
                return UserSettingsView.as_view(form_class=StudentSettingsForm)(request)
            if request.user.profile.is_teacher:
                return UserSettingsView.as_view(form_class=TeacherSettingsForm)(request)
            if request.user.profile.is_parent:
                return UserSettingsView.as_view(form_class=ParentSettingsForm)(request)
        else:
            if request.method == 'POST':
                """
         return ***<--- I need to know what to pass here to be able to call the appropriate post function of the UserSettingsView?---->"""***
    else:
            return HttpResponseRedirect('/accounts/login/')
def pick_设置(请求):
if request.user.is_经过身份验证():
如果request.method==“GET”:
如果request.user.profile.is_学生:
返回usersetingsview.as\u视图(form\u class=studentsetingsform)(请求)
如果request.user.profile.is_教师:
返回usersetingsview.as\u视图(form\u class=TeacherSettingsForm)(请求)
如果request.user.profile.is_父项:
返回usersetingsview.as\u视图(form\u class=parentsetingsform)(请求)
其他:
如果request.method==“POST”:
"""
返回***“***
其他:
返回HttpResponseRedirect(“/accounts/login/”)
我需要能够调用UserSettingsView的post函数。可能正在使用get_上下文_数据?但我不知道怎么做


如果有人能提出更好的方法,那就太好了,因为我很确定这可能违反了干燥原则。尽管如此,只要工作完成了,我就不太在意这一点,因为我在赶最后期限。:)谢谢

FormView
有一个方法
get\u form\u class()
。它是从
get()
post()
调用的,因此
self.request
已经设置好了(self.request.user也将设置好)。因此,

class UserSettingsView(LoginRequiredMixin, FormView):
   [...]
   def get_form_class(self):
       # no need to check is_authenticated() as we have LoginRequiredMixin
       if request.user.profile.is_student:
            return StudentSettingsForm
       elif user.profile.is_teacher:
            return TeacherSettingsForm
       elif user.profile.is_parent:
            return ParentSettingsForm
当您为每个用户类型获得正确的表单时,这应该已经成为关键

如果还需要呈现不同的模板,请重写
get\u template\u names()

DRY可以通过在模板中使用适当的继承并结合常见模板片段来实现


为了避免我忘记(我已经忘记了):为了摆脱视图的
post()
方法中的if,只需重写表单的
save()
方法,我假设表单是
ModelForm
s,无论如何。

谢谢您的及时回复。这太棒了。我低估了FormView的优势,因为它能够使用
self.request.user
选择相应的用户设置表单,这就是为什么我编写了一个单独的片段来选择表单。过度使用save函数有点棘手,因为我在家长/老师和学生档案之间有一种条件连接。无论如何,我希望这也能对其他人有所帮助。无论如何,这都是不错的代码。我唯一想做的就是将保存直接移动到
form\u valid()
。这不会从
get()
调用,而且更容易从其他地方调用。
class UserSettingsView(LoginRequiredMixin, FormView):
   [...]
   def get_form_class(self):
       # no need to check is_authenticated() as we have LoginRequiredMixin
       if request.user.profile.is_student:
            return StudentSettingsForm
       elif user.profile.is_teacher:
            return TeacherSettingsForm
       elif user.profile.is_parent:
            return ParentSettingsForm
   def get_template_names(self):
       if request.user.profile.is_student:
            return ['myapp/settings/student.html']
       elif user.profile.is_teacher:
            return ['myapp/settings/teacher.html']
       elif user.profile.is_parent:
            return ['myapp/settings/parent.html']