我正在尝试制作一个视图,我可以在具有不同重定向网址的多个应用中使用:
父功能:
def create_order(request, redirect_url):
data = dict()
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
form.save()
return redirect(redirect_url)
else:
form = OrderForm()
data['form'] = form
return render(request, 'core/order_document.html', data)
儿童功能:
@login_required()
def admin_order_document(request):
redirect_url = 'administrator:order_waiting_list'
return create_order(request, redirect_url)
当我试图调用admin_order_document函数时,我得到了:
Traceback (most recent call last):
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: create_order() missing 1 required positional argument: 'redirect_url'
如果我从两个函数中删除redirect_url并手动添加'administrator:order_waiting_list'到redirect()它可以工作,但我需要重定向到多个网址。那么,为什么我会收到此错误?
答案 0 :(得分:3)
url(r'^orders/create/', views.create_order, name='create_order')
这显然不起作用,因为create_order
需要redirect_url
,但正则表达式redirect_url
中没有r'^orders/create/'
kwarg。
也许你想在这里使用admin_order_document
视图:
url(r'^orders/create/', views.admin_order_document, name='create_order')
请注意,您应该添加一个尾随的美元,即r'^orders/create/$'
,除非您想匹配orders/create/something-else
以及orders/create/
。
答案 1 :(得分:0)
如果您没有更改常规网址
urlpatterns = [
url(r'^admin/', admin_site.urls),
...
]
您的管理网站需要像这样调用您的函数:
@login_required()
def admin_order_document(request):
redirect_url = 'admin:order_waiting_list'
return create_order(request, redirect_url)
那应该解决你的问题。