Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:58:36+00:00 2026-05-22T16:58:36+00:00

In the documentation of Django is an example pattern for an URL of an

  • 0

In the documentation of Django is an example pattern for an URL of an article:

(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),

So, only 2011/05/23/ will match, but not 2011/5/23/

In another part of the docs, where the permalinks decorator is explained, the pattern is

(r'/archive/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', archive_view)

And the code for creating a permalink

@models.permalink
def get_absolute_url(self):
    return ('archive_view', (), {
        'year': self.created.year,
        'month': self.created.month,
        'day': self.created.day})

In particular, month has changed from \d{2} to \d{1,2}, so 2011/05/23/ and 2011/5/23/ will now both match; the get_absolute_url method will create the second link, without leading zero.

To create a permalink for the first regex (\d{2}), I could write str(self.created.month).zfill(2) in the method, but this seems a bit cumbersome and too redundant (if I change the URLconf, I will need to change the get_absolute_url method, too) to me.

Additionally, we have now multiple urls which all display the same content (2011/05/03/, 2011/5/03/, 2011/05/3/, etc.), could that be a problem, e.g. for search engines? At least it can result in inconsistent urls.

Is there a (simple) way to redirect all urls to the zero filled ones (2011/5/3/ › 2011/05/03/) and also automatically always build them zerofilled, so I don’t need to mess around in methods like get_absolute_url with str() and zfill and can just pass the number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:58:37+00:00Added an answer on May 22, 2026 at 4:58 pm

    There’s no way that I know of to get Django to automatically zero fill numbers passed in as parameters to a URL, other than the way you are already doing it.

    You could relax the regex to not require the zero, as you described, which would create a duplicate content issue. However, @Matt fails to consider that the content has to actually be exposed at both URLs for search engines to consider it duplicate. More likely than not, all URLs on your site would be composed from either reverse (or models.permalink decorator around get_absolute_url) or the {% url %} template tag. Therefore, all the URLs would be the same format, i.e. without zeros, and the zero-version would never even been seen by search engines.

    Additionally, you can make use of the canonical tag to let search engines know that the content is not duplicate, but merely available through multiple URLs.

    <link rel="canonical" href="http://domain.com/archive/2011/5/3/" />
    

    So search engines are nothing to be concerned about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have a quick url dispatcher example? I've looked at the Django documentation but
From the Django documentation... When you're only dealing with simple many-to-many relationships such as
I'm trying to get the hang of Django URL namespaces . But I can't
http://documentation.mailgun.net/quickstart.html contains some example code for a http handler in Django: # Handler for
According to Django documentation, if SESSION_EXPIRE_AT_BROWSER_CLOSE is set to True, Django will use browser-length
I looked on Django's documentation and Googled every varation of the phrase but I
This example is from the Django documentation . Given the (Django) database model: class
In Django documentation for setting timezone , the list of available choices for timezone
The permission/authentication documentation for Django 1.4 provides the following snippet for creating custom permissions
I've seen some nifty code on django-ratings documentation and like to create something similar.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.