I am trying to pass some arguments with a link url href in a template to a view.
In my template :
<a href="/print-permission-document/ studentname={{studentinfo.0}} studentsurname={{studentinfo.1}} studentclass={{studentinfo.2}} doctype=doctype-studentlatepermission">Print</a>
So i am trying to pass 4 arguments to my view.
My view is :
def print_permission_document(request, studentname, studentsurname, studentclass, doctype):
file_write(studentname.encode('utf-8')+" "+studentsurname.encode('utf-8')+" "+studentclass+" "+doctype)
return response
My urls.py is :
url(r'^print-permission-document/.+$', print_permission_document, name='print-permission-document')
But i get below error :
Exception Type: TypeError
Exception Value:
print_permission_document() takes exactly 5 arguments (1 given)
答案 0 :(得分:1)
这不是不是,您如何在URL中指定多个参数,通常是在URL中编写这些参数,例如:
url(
r'^print-permission-document/(?P<studentname>\w+)/(?P<studentsurname>\w+)/(?P<studentclass>\w+)/(?P<doctype>[\w-]+)/$',
print_permission_document, name='print-permission-document'
)
然后您使用以下方法生成相应的URL:
<a href="{% url 'print-permission-document' studentname=studentinfo.0 studentsurname=studentinfo.1 studentclass=studentinfo.2 doctype='doctype-studentlatepermission' %}">Print</a>
这将生成一个类似于以下内容的URL:
/print-permission-document/somename/someclass/doctype-studentlatepermission
通常,路径中不不包含键值对,如果包含,则您需要自己对它们进行“解码”。
您还可以生成一个 querystring (在问号之后),然后您可以在request.GET
[Django-doc]中访问这些内容。
答案 1 :(得分:1)
您错误地传递了URL。并且模板中的URL也被错误地声明。
尝试
<a href="{% url 'print-permission-document' studentinfo1, studentinfo2, ... %}">Print</a>
url(
r'^print-permission-document/(?P<studentname>\w+)/(?P<studentsurname>\w+)/(?P<studentclass>\w+)/(?P<doctype>\w+)/$',
print_permission_document, name='print-permission-document'
)
答案 2 :(得分:0)
我有同样的错误,我通过以下方式纠正了它:
url(r'^auth_app/remove_user/(?P<username2>[-\w]+)/$', views.remove_user, name="remove_user"),
使用此模式传递字符串
(?P<username2>[-\w]+)
此为整数值
(?P<user_id>[0-9]+)