1

I'm having a bit of trouble configuring the following url. I want it to be able to match a pages which start off with a category and then finish with a slug, examples:

/category1/post1/
/category2/post2/
/category3/post3/
/category1/post4/
/category2/post5/

I've tried many different methods with no success... I always get an "is not a valid regular expression" error. This is how I thought it should work:

url(r'^(?P<category1|category2|category3>[\w\-]+)/(?P<slug>[\w\-]+)/$', blog_post, name = 'blog_post'),

I am fairly new to regex and trying to learn so any help on this one with an explanation would be much appreciated :)

1
  • Change category1|category2|category3 to category and it'll work. Commented Sep 2, 2014 at 14:14

1 Answer 1

1

Your pattern is incorrect; you are putting the alternative values in the wrong place. You put them in the name of the group:

(?P<category1|category2|category3>...)

Put them in the part the name is supposed to match instead:

(?P<category>category1|category2|category3)

Making the full registration:

url(r'^(?P<category>category1|category2|category3)/(?P<slug>[\w\-]+)/$', blog_post, name='blog_post'),

I'm assuming your blog_post callable looks something like:

def blog_post(category, slug):
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.