2

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

*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. 鉴于我的实施,我需要注意任何安全注意事项吗?
感谢大家。

4

1 回答 1

2

因此,在找到一个非常简单的解决方案之前,我已经蚕食了我的代码一段时间。我按照django 文档对 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 是微不足道的。

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

于 2014-03-09T10:48:47.730 回答