python views.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python views.py相关的知识,希望对你有一定的参考价值。

class AccountForm(forms.ModelForm):
    class Meta:
        model = Account
        fields = ['first_name', 'last_name', 'email', 'password']

    def __init__(self, *args, **kwargs):
        self.university = kwargs.pop('university')
        self.high_school = kwargs.pop('high_school')
        self.department = kwargs.pop('department')
        
    def save(self, *args, **kwargs):
        data = self.cleaned_data
        kwargs['commit'] = False
        instance = super(AccountForm, self).save(*args, **kwargs)
        instance.university = self.university
        instance.high_school = self.high_school


class AccountCreate(generic.CreateView):
    form_class = AccountForm
    model = Account
    template_name = "account/update.html"

    def get_success_url(self):
        return reverse('account:index')

    def get_context_data(self, **kwargs):

        context = super(AccountCreate, self).get_context_data(**kwargs)
        context['action'] = reverse('account:create')

        return context

    def get_form_kwargs(self, **kwargs):
        form_kwargs = super(AccountCreate, self).get_form_kwargs(**kwargs)
        form_kwargs['university'] = self.request.POST['univ_cmb_box']
        form_kwargs['department'] = self.request.POST['department_cmb_box']
        form_kwargs['high_school'] = self.request.POST['hs_cmb_box']
        return form_kwargs

以上是关于python views.py的主要内容,如果未能解决你的问题,请参考以下文章

python 基本的django views.py函数

在views.py中获取url - python django

python学习3-python views.py的返回值

python 示例django-plotly-dash views.py

django中将views.py中的python方法传递给html模板文件

Django之views.py视图函数学习