如何根据 django 模板中的不同提交按钮在更新视图中自定义成功 url?

Posted

技术标签:

【中文标题】如何根据 django 模板中的不同提交按钮在更新视图中自定义成功 url?【英文标题】:How to have custom success url in update view based on different submit buttons in django template? 【发布时间】:2020-02-04 11:01:58 【问题描述】:

我有一个 CustomUser 模型、一个合作伙伴模型和一个学生模型,它们都与 CustomUser 具有 OneToOne 关系,如下所示:

class CustomUser(AbstractUser):
    username = None
    email = EmailField(verbose_name='email address', max_length=255, unique=True, )
    first_name = CharField(verbose_name='First Name', max_length=30, null=True, )
    middle_name = CharField(verbose_name='Middle Name', max_length=30, null=True, blank=True, )
    last_name = CharField(verbose_name='Last Name', max_length=30, null=True, )
    phone_number = CharField(verbose_name='Phone Number', max_length=30, null=True, )
    is_partner = BooleanField(default=False, )
    is_student = BooleanField(default=False, )

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email


class Partner(Model):

    user = OneToOneField(CustomUser, on_delete=CASCADE, related_name="partner")


class Student(Model):

    user = OneToOneField(CustomUser, on_delete=CASCADE, related_name='student')

我有一个视图,我想在其中更新用户的 first_name、last_name、middle_name 和 phone_number。这是一个简单的更新视图。但我想放两个提交按钮,在极端情况下,这两个按钮都可能在页面中可用:


<form method="post" novalidate>
    % csrf_token %
    % include 'includes/accounts/user_name_and_phone_update_view_form.html' with form=form %

    % if user.is_student %
        <button type="submit" name="student" id="student">Continue to Student Profile</button>
    % endif %

    % if user.is_partner %
        <button type="submit" name="partner" id="partner">Continue to Partner Profile</button>
    % endif %
</form>

我需要自定义我的视图如下(这是伪代码):

@method_decorator([login_required, ], name='dispatch')
class UserNameAndPhoneUpdateView(UpdateView):
    model = CustomUser
    fields = ('first_name', 'middle_name', 'last_name', 'phone_number',)
    template_name = 'accounts/account.html'

    if submit button id and name is student:
        success_url = reverse_lazy('student_general_information')
    elif submit button id and name is partner:
        success_url = reverse_lazy('partner', kwargs='partner_pk': self.request.user.partner.pk)


    def get_object(self):
        return self.request.user

如何正确处理 if 部分以及如何将 part_pk 传递给 elif 中的 reverse_lazy?

【问题讨论】:

【参考方案1】:

它是这样工作的:

@method_decorator([login_required, ], name='dispatch')
class UserNameAndPhoneUpdateView(UpdateView):
    model = CustomUser
    fields = ('first_name', 'middle_name', 'last_name', 'phone_number',)
    template_name = 'accounts/account.html'

    def get_success_url(self):
        if "student" in self.request.POST:
            return reverse_lazy('student_general_information')
        elif "partner" in self.request.POST:
            return reverse_lazy('partner', kwargs='partner_pk': self.request.user.partner.pk)

        return super(UserNameAndPhoneUpdateView, self).get_success_url()

    def get_object(self):
        return self.request.user

基于: https://***.com/a/2011023/11065874

【讨论】:

以上是关于如何根据 django 模板中的不同提交按钮在更新视图中自定义成功 url?的主要内容,如果未能解决你的问题,请参考以下文章

django 如何写表单提交

如何在Django中只在ModelForms表中正确提交一个表单?

在 django 模板中单击按钮后如何填充表格?

Django中相同形式的两个不同提交按钮

django 使用一个提交按钮提交两种不同的表单

django 模板:如何知道表单字段类型并根据字段类型添加任何按钮