1

Help me please to fix urls.py People suggested this way, but it does't work for me.....

#urls.py
  (r'^/user/(?P<username>)/subject/([\w|\W]+)/$', subject),

#template
 {% for subject in subjects %}
    <li><a href="/user/{{ user.username }}/subject/{{ subject.name }}">{{ subject.name }}</a> {{ del_form.delete }}</li>
 {% endfor %}

#error
PAGE NOT FOUND
Request URL:    http://127.0.0.1:8000/user/root/subject/Math%20140
....
....
^/user/(?P<username>)/subject/([\w|\W]+)/$
4
  • I don't know about django, but your regex requires the URL to end in a /, and your test URL (127.0.0.1....MATH%20140) does not end in a /. You could make it optional or remove it. Also, [\w|\W]+ should be [\w\W]+ since | doesn't mean anything in the []. (Why not just do .+ then? You are including new lines in [\w\W] whereas . does not) Commented Jul 23, 2012 at 23:36
  • To some extent, that also depends on whether or not the multiline flag (m) is set, which I don't think it is. A secondary issue might be that the URL is url-encoded, whereas the subject is probably not. Commented Jul 23, 2012 at 23:38
  • Django by default adds trailing slashes to URLs. The url-encoding issue would probably be best handled by adding a subject slug that doesn't include spaces etc, or using the slugify filter (and attempting to invert in the view - probably best to just add an actual SlugField to avoid difficulties there). Commented Jul 23, 2012 at 23:43
  • @Dougal that's a nice suggestion! Commented Jul 23, 2012 at 23:55

1 Answer 1

7

You have an error in your regular expression. You should use a regex builder if you are new to this:

http://ryanswanson.com/regexp/ (Perl)

http://www.pyregex.com/ (Python)

I think you want something like this:

^user/(?P<username>.+)/subject/([\w|\W]+)/

But you might want to change the '.+' to something more restrictive:

^user/(?P<username>[^/]+)/subject/([\w|\W]+)/

Note also that you probably don't want that leading slash - due to the way Django feeds the initial URL to the URL dispatcher.

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

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.