0

In Django 1.7, how can I create only one url regex which matches all 3 following templates:

  1. /page/
  2. /page/{two characters}/
  3. /page.html

Many thanks.

3 Answers 3

2

try this

from django.views.generic import TemplateView

urlpatterns = patterns('',
   url(r'^page/$', yourviews.page),
   url(r'^page/(\w{2})/$', yourviews.page_detail),
   url(r'^page.html$', TemplateView.as_view(template_name="page.html")),
)

UPDATE: I added TemplateView for your page.html, so you dont need to write views for it. this is one of the beauties of Django.

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

1 Comment

I edited my question, do you have any suggestion for only one url which matches all three urls?
1

How about

url(r'^/page(\/\w{2}\/|\/|\.html)$', yourview)

Comments

1

urls.py

url(r'^page/$', views.page),
url(r'^page/(\w{2})/$', views.page),
url(r'^page\\.html$', views.page),

views.py

def page(request, two_chars=''):
    # ...

See this doc link.

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.