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 4538370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:48:09+00:00 2026-05-21T14:48:09+00:00

I have a site whose URLs look like http://www.example.com/NY-2010/ http://www.example.com/NY-2010/location/ http://www.example.com/NY-2010/something-else/ http://www.example.com/Washington-2009/ http://www.example.com/Washington-2009/location/ http://www.example.com/Washington-2009/something-else/

  • 0

I have a site whose URLs look like

http://www.example.com/NY-2010/

http://www.example.com/NY-2010/location/

http://www.example.com/NY-2010/something-else/

http://www.example.com/Washington-2009/

http://www.example.com/Washington-2009/location/

http://www.example.com/Washington-2009/something-else/

and so on. There are various pages (like location) for various editions (like NY). I use URLconfs like

url(r'^(?P<edition>[\d]+\-[\w]+)/$', views.home),
url(r'^(?P<edition>[\d]+\-[\w]+)/location/$', views.location),

In each of the views I have to fetch the current edition. The fact is, if the edition name is wrong, I want to redirect to the latest edition. So I do something like

def home(request, edition):
    try:
        event = Edition.objects.get(name=edition)
    except ObjectDoesNotExist:
        return redirect(home, edition=Edition.latest())
    # If event was found I go on here

def location(request, edition):
    try:
        event = Edition.objects.get(name=edition)
    except ObjectDoesNotExist:
        return redirect(home, edition=Edition.latest())
    # If event was found I go on here

and so on. Of course there is some duplication that I’d like to minimize. I can think of two ways:

  • use get_objects_or_404() and customize the 404 view, or
  • abstract the common part in a function.

The problem with both ways is that they do not allow me to do a proper redirect, that is, the URL will remain the same even if the view was changed. Is there a better way to handle these redirects?

EDIT It seems my question is not clear. In particular it is not clear what I mean by abstract the common part in a function. So, what I could do is the following

def get_edition_or_current(edition):
    try:
        event = Edition.objects.get(name=edition)
    except ObjectDoesNotExist:
        event = Edition.latest()
    return event

def home(request, edition):
    event = get_edition_or_current(edition)
    # I go on with a valid event here

def location(request, edition):
    event = get_edition_or_current(edition)
    # I go on with a valid event here

In this way I can display the view for a proper event, but I cannot change the URL. To change the URL, the view must return a redirect. I cannot set the return value for the view from inside get_edition_or_current.

So, how does Django implements get_object_or_404? Well, it is simple, it raises an Http404 exception, and catches it later. But of course this only works for Http404 exceptions, because Django is instructed to catch them.

  • 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-21T14:48:10+00:00Added an answer on May 21, 2026 at 2:48 pm

    I think the simplest way to do this would be to create new utility function called get_object_or_redirect in the same vein as get_object_or_404. You could probably even copy the contents of get_object_or_404 from django.shortcuts as a starting point for your implementation, or just extract out what you have above.

    EDIT: as noted in the comments, a redirect cannot be done via raising an “exception,” so this really can’t work the same as get_object_or_404.

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

Sidebar

Related Questions

I know how to make URL's rewrite, for example: www.example.com/index.php?id=1&cat=3 to www.example.com/1/3/ (or whatever).
I have a project whose core domain is dramatically changing. It's possible to use
I have a very dynamic (social networking) site running smarty that I want to
I have a single template file which renders pages that look very similar, but
We are rewriting an old site for a client, from Classic ASP to ASP.NET
I am working on a Wordpress site that displays a number of Projects stored
I am sure this is a basic answer, but my search powers are not
I've been searching the whole evening for a solution/approach for my problem with my
I am learning how to setup a shared hosting web server. As a server

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.