1

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)

2

3 Answers 3

2

This is not how you specify multiple parameters in a URL, typically you write these in the URL, like:

url(
    r'^print-permission-document/(?P<studentname>\w+)/(?P<studentsurname>\w+)/(?P<studentclass>\w+)/(?P<doctype>[\w-]+)/$',
    print_permission_document, name='print-permission-document'
)

Then you generate the corresponding URL with:

<a href="{% url 'print-permission-document' studentname=studentinfo.0 studentsurname=studentinfo.1 studentclass=studentinfo.2 doctype='doctype-studentlatepermission' %}">Print</a>

This will then generate a URL that looks like:

/print-permission-document/somename/someclass/doctype-studentlatepermission

Typically a path does not contain key-value pairs, and if it does, you will need to "decode" these yourself.

You can also generate a querystring (after the question mark), these you can then access in request.GET [Django-doc].

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Williem, now i get below error Exception Type: NoReverseMatch Exception Value: Reverse for 'print-permission-document' with arguments '()' and keyword arguments '{u'studentsurname': 'SURMAN', u'studentname': 'Aleksa', u'studentclass': '4N', u'doctype': u'doctype-studentlatepermission'}' not found. 1 pattern(s) tried: ['print-permission-document/(?P<studentname>\\w+)/(?P<studentsurname>\\w+)/(?P<studentclass>\\w+)/(?P<doctype>\\w+)/$'] Exception Location: /home/minimus/Env/minimus/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508
@ivbtar: I forgot the hyphen in the pattern for the doctype, so [\w-]+ instead of \w+.
Hi Villiem. Problem continues for one scenario. If there is an empty space in studentname it breaks. It works for "Jack" but if he has double name "Jack John" it fails with Reverse for 'print-permission-document' with arguments '()' and keyword arguments '{u'studentno': 20170224L, u'studentsurname': 'K\xc3\x87LAN', u'studentname': 'Jack John', u'studentclass': '12Y', u'doctype': u'doctype-studentlatepermission'}' not found. 1 pattern(s) tried: ['print-permission-document/(?P<studentname>\\w+)/(?P<studentsurname>\\w+)/(?P<studentclass>\\w+)/(?P<studentno>\\w+)/(?P<doctype>[\\w-]+)/$']
@ivbtar: then you can change the \w+s to [\w ]+s, etc. but spaces in URLs are typically not a good idea in general. Usually slugs are used.
Thanks you so much Villiem. It worked again. Let me also check slugs.
1

You are passing your URL wrongly. and URL in template is also declared wrongly.

Try this

<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'
)

Comments

0

I had the same error , i corrected it by :

url(r'^auth_app/remove_user/(?P<username2>[-\w]+)/$', views.remove_user, name="remove_user"),

Use this pattern for passing string

(?P<username2>[-\w]+)

This for interger value

(?P<user_id>[0-9]+)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.