如何在向导视图的中间步骤处理login_required以保持Django中的流程?

时间:2014-03-07 21:50:40

标签: django formwizard

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

* 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

*的login.html

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

所以:
1.我怎样才能继续第四步? 2.鉴于我的实施,我是否需要注意任何安全方面的考虑因素? 谢谢大家。

1 个答案:

答案 0 :(得分: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是微不足道的。

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