URL模式中的可选(非随机)参数

时间:2017-02-21 10:42:22

标签: django

如何在url模式中传递可选参数(非随机)?

/新/

/新的/保存并添加新/

url(
    regex=r'^~new/[save-and-add-new]/$',
    view=views.BudgetCreateView.as_view(),
    name='create'
),

我需要在:

中使用它
def get_success_url(self):
   if save_and_add_new_argument_in_url:
       return HttpResponseRedirect(reverse('item:new'))
   return HttpResponseRedirect(reverse('item:list'))

1 个答案:

答案 0 :(得分:1)

url(
regex=r'^~new/(?P<param>\w+)/$',
view=views.BudgetCreateView.as_view(),
name='create'
),
url(
regex=r'^~new/$',
view=views.BudgetCreateView.as_view(),
name='create'
),

然后

def get_success_url(self, param=None):
   if param is not None:
       return HttpResponseRedirect(reverse('item:new'))
   return HttpResponseRedirect(reverse('item:list'))