Viet
Viet

Reputation: 18404

Adding arbitrary extensions to URLs generated by Django

Django has excellent URLConf and URL reverse mapping/matching. I'm looking for a tip/trick to add arbitrary extensions to URLs generated by Django. Sometimes it's nice to see extensions that suggest your brand.

Upvotes: 1

Views: 178

Answers (1)

Alexander Lebedev
Alexander Lebedev

Reputation: 6044

OK, let's assume I want to publish some documents which are available in HTML, PDF, DOC, etc formats. The pattern in urlconf would look like this:

(r"^/docs/(?P<doc_slug>[\w-]+).(?P<ext>\w+)$", myapp.views.view_doc),

and the view:

def view_doc(request, doc_slug, ext):
    if ext == "html":
        #...
    elif ext == "pdf":
        #...
    else:
        return Http404("Document not available in this format")

Upvotes: 1

Related Questions