0

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?

2
  • Hi! Welcome to Stack Overflow. The best place to find that would be in the django docs. Have you worked through the tutorial yet? Commented Jul 10, 2017 at 11:27
  • I have but I can't find the problem. @The_Cthulhu_Kid Commented Jul 10, 2017 at 11:30

2 Answers 2

3

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.

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

Comments

0

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

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.