type error issue

23 views
Skip to first unread message

jon stan

unread,
Jun 12, 2017, 9:49:16 AM6/12/17
to Django users
So i keep getting this error every time i try to pass a variable in http.

in my template i have a link that contains the variable of a county in a state:  

   template.html
   <li><a href="/main/region/{{cnty}}/">{{ cnty }}</a></li>

when you click the link i guess it goes to the urls file:
 
   urls.py
   url(r'^region', views.region, name='region'),

and then it gets picked up by the views file but i keep getting an error for some reason:

   views.py
   def region(request, cnty):
     
       return render(
           request, 'region.html', context={'cnty':cnty},
       )

the error says "region() takes 2 arguments 1 given", but i dont understand how im only giving 1 argument?

Andréas Kühne

unread,
Jun 12, 2017, 10:16:36 AM6/12/17
to django...@googlegroups.com
In your urls.py file you have to add the variable you want to add to the URL....

You currently have : 

url(r'^region', views.region, name='region'),

But it should be:

url(r'^region/(?P<cnty>[\w-]+)/$', views.region, name='region'),

Otherwise the url doesn't know where to get the cnty variable from and therefore you get the error.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7762d655-1ae3-49fa-887a-0ade6bd3c00d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jon stan

unread,
Jun 19, 2017, 10:02:00 PM6/19/17
to Django users
That was it. thanks Andreas.
Reply all
Reply to author
Forward
0 new messages