Reputation: 3
I'm new to Django and would like to know how to get a variable from a url. I have tried this:
url(r'^(?P<name>)/$', employeedetail, name='employeedetail'),
which goes to this view
def employeedetail(request, name):
return render(request, 'employee/detail.html')
but I get an error: employeedetail() missing 1 required positional argument: 'name'
Is the code wrong or do I need to type in the url in a particular way?
Upvotes: 0
Views: 476
Reputation: 4092
Try to use following solution
in your urls.py
For a string parameter in url use following syntax
url(r'^(?P<name>[\w\-]+)/$',employeedetail, name='employeedetail')
This will even allow the strings to pass in your url.
And access name parameter in your view
def employeedetail(request, name):
print name
return render(request, 'employee/detail.html')
Hope this will help you
Upvotes: 0
Reputation: 599590
Your regex is wrong; you haven't provided any characters to match. It should be something like:
r'^(?P<name>\w+)/$
which matches all alphanumeric characters.
Upvotes: 3