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)

ivbtar
  • 799
  • 11
  • 29

3 Answers3

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].

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 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\\w+)/(?P\\w+)/(?P\\w+)/(?P\\w+)/$'] Exception Location: /home/minimus/Env/minimus/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508 – ivbtar Oct 24 '18 at 11:43
  • @ivbtar: I forgot the hyphen in the pattern for the `doctype`, so `[\w-]+` instead of `\w+`. – Willem Van Onsem Oct 24 '18 at 11:45
  • 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\\w+)/(?P\\w+)/(?P\\w+)/(?P\\w+)/(?P[\\w-]+)/$'] – ivbtar Oct 25 '18 at 07:12
  • @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](https://stackoverflow.com/questions/427102/what-is-a-slug-in-django) are used. – Willem Van Onsem Oct 25 '18 at 07:13
  • Thanks you so much Villiem. It worked again. Let me also check slugs. – ivbtar Oct 25 '18 at 07:19
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'
)
Daniel Kilanko
  • 110
  • 1
  • 11
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]+)
Tono Kuriakose
  • 718
  • 6
  • 19