django - 如何在其他向导 View 的中间步骤中处理 login_required 以维护 Django 中的流程?

标签 django formwizard

我有一个向导 View ,要求在第三步登录。它可以工作,但登录后向导不会继续到第四步,而是返回到第一步。这样很不方便

*views.py

FORMS = [("amount", forms.AmountToSendForm),
     ("confirm_amount", forms.ConfirmAmountForm),
     ("receiver", forms.ReceiverForm),
     ("card", forms.CardPaymentForm),
     ("bank", forms.BankPaymentForm),]

...

def login_user(request):
    #login is as imported from django.contrib.auth.views
    return login(request, template_name='roja/login.html',  authentication_form=forms.LoginForm)

class PaymentWizard(SessionWizardView):
    ...
    def dispatch(self, *args, **kwargs):
        #initiate attributes of the dispatch method so that the .steps atrribute 
        #of the dispatch method can be exposed
        response = super(PaymentWizard, self).dispatch(*args, **kwargs) 

        if self.steps.current == 'receiver':
            @method_decorator(login_required) #as imported
            def inner_dispatch(self, *args, **kwargs):
                return super(PaymentWizard, self).dispatch(*args, **kwargs)
            return inner_dispatch(self, *args, **kwargs)               
        else:        
            return response

*登录.html

...
<form action="{{ app_path }}" method="post" id="login-form" class="panel-body wrapper-lg">{% csrf_token %}
 ...

所以:
1. 如何才能继续到第四步?
2. 在实现过程中,我需要注意哪些安全注意事项?
谢谢大家。

最佳答案

因此,在找到一个极其简单的解决方案之前,我已经对代码进行了一段时间的修改。我按照 django documentation 对 NamedUrlSessionWizardView 进行了子类化。

即而不是

class PaymentWizard(SessionWizardView):
    ...

我有:

# If you have different templates for each step add this
# and use as below 
TEMPLATES = {                                                               
"amount": "pay/amount.html",                                            
"confirm_amount": "pay/amount_calculated.html",                         
"receiver": "pay/receiver.html",                                        
"card": "pay/card_form.html",                                           
"bank": "pay/bank_success.html",                                        
}         

class PaymentWizard(NamedUrlSessionWizardView):
    ...
    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

然后在urls.py

from pay.views import PaymentWizard, FORMS

payment_wizard = PaymentWizard.as_view(FORMS, url_name='pay_step', done_step_name='finished')


urlpatterns = patterns('',
    ...,
    url(r'^(?P<step>.+)/$', payment_wizard, name='pay_step'),
)

由于各个步骤都有 django 识别的 url,因此在登录后重定向到引用 url 很简单。

仍然需要知道是否存在需要警惕的安全问题。

关于django - 如何在其他向导 View 的中间步骤中处理 login_required 以维护 Django 中的流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261376/

相关文章:

Django - 如何将字典传递给模板标签

javascript - Angular JS/Django REST 更改密码模块问题。浏览器干扰我的代码

当有多个单选按钮值时,jquery 单选按钮值不起作用

Django 表单向导,当我还没有完成时如何重置/清除向导?

python - 保护用户上传的文件 django

python - 迭代更新它们的 django 对象的最有效方法是什么?

python - Django pymssql 迁移

python - Django-formwizard 和 ModelFormSet 保存

django - 在 DB 中保存 Django 的表单向导 form_list 的最简单方法?