成功后重定向URL

时间:2017-12-14 14:50:43

标签: python django url redirect

import re
DEFAULT_SUCCESS_URL = re.compile(r'^(?P<clientcode>[0-9]{9})/dashboard/')
url(r'^(?P<clientcode>[0-9]{9})/order/$',
    OrderFormView.as_view(success_url=settings.DEFAULT_SUCCESS_URL),
    name='zoneclient-order'),

到目前为止,由于以下错误,该技术无效:

TypeError: unsupported operand type(s) for %: '_sre.SRE_Pattern' and 'dict'

事实上,我希望success_url成为

  # Dashboard
  url(r'^(?P<clientcode>[0-9]{9})/dashboard/', login_required(
      DashboardView.as_view()), name='zoneclient-dashboard'),

如何修改热门代码以便与r'^(?P<clientcode>[0-9]{9})/dashboard/'配合使用?

P.S。请注意,我正在使用Django 1.7和DEFAULT_SUCCESS_URL多次使用。

更新

class OrderFormView(CustomerViewMixin, CreateView, SendTextEmailMixin):
    template_name = 'zoneclient/order-form.html'
    form_class = OrderForm

    email_subject_template = 'zoneclient/emails/order_form_subject.txt'
    email_body_template = 'zoneclient/emails/order_form_body.txt'
    email_recipients = settings.EMAIL_NOTIFY_ORDERS

    def get_form(self, form_class):
        kwargs = self.get_form_kwargs()
        kwargs['user'] = self.get_customer()
        return form_class(**kwargs)

    def form_valid(self, form):
        self.object = form.save()
        self.send_email()
        log.info('%s\' order #%d was successufully processed.' % (\
            usr(self.request), self.object.id))
        messages.success(
            self.request,
            _('Your order has been placed, thank you!'),
            fail_silently=True)
        return HttpResponseRedirect(self.get_success_url())

    def get_email_context(self):
        context = super(OrderFormView, self).get_email_context()
        context['user'] = self.get_customer()
        context['object'] = self.object
        return context

1 个答案:

答案 0 :(得分:0)

以下是显示如何覆盖get_success_url()的示例。您尚未提供视图类的实现,因此我假设它是标准FormView

 class OrderFormView(FormView):

    def form_valid(self, form):
        # form handling logic
        super(OrderFormView, self).form_valid(form)

    def get_success_url(self):
        clientcode = self.kwargs['clientcode']
        url = '/' + clientcode + '/dashboard/'
        return url

您可以参考Django文档了解详细信息。