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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:37:25+00:00 2026-05-23T21:37:25+00:00

Someone inputs www.example.com/3/ I have a model with a charfield that I can slugify.

  • 0

Someone inputs www.example.com/3/

I have a model with a charfield that I can slugify.

class Mymodel(models.Model):
    title = models.CharField(max_length=200)

How can I rewrite the url as www.example.com/3/title-of-selected-id in Django?

Do I do it in the urls.py or in the views.py? All I could think of was making 2 patterns like this

(r'^mymodels/(?P<mymodel_id>\d+)/(?P<name_slug>[\w-]+)$', 'app.views.detail'),
(r'^mymodels/(?P<mymodel_id>\d+)/$', 'app.views.rewrite'),

And then in my app.views.rewrite I would get the mymodel title, slugify it and redirect to the first url. But this seems awfully dirty

  • 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-23T21:37:26+00:00Added an answer on May 23, 2026 at 9:37 pm

    Through #django irc someone told me that the redirect method wasn’t bad, and we couldn’t think of another easy way so I just coded something up which seems to be working.

    #urls.py
    (r'^mymodels/(?P<mymodel_id>\d+)/$', 'app.views.detail'),
    (r'^mymodels/(?P<mymodel_id>\d+)/(?P<name_slug>[\w-]+)/$', 'app.views.detail'),
    
    #views.py
    def detail(request, mymodel_id, name_slug=None):
        mymodel = get_object_or_404(Mymodel, pk=mymodel_id)
    
        slug = slugify(mymodel.title)
    
        if slug == name_slug:
            return HttpResponse("model number %s." % mymodel_id)
        else:
            return redirect(reverse('app.views.detail', args=[mymodel_id, slug]), permanent=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone told me that it's faster to concatenate strings with StringBuilder. I have changed
I really hope someone can help me with this. Basically i have an issue
Someone told me about a C++ style difference in their team. I have my
Someone told me about swamp diagrams explaning that they were useful to predict code
Someone posted a great little function here the other day that separated the full
Can someone help me with this? I'm feeling like I've been hitting my head
I have a list of 3 radio buttons with the 1st radio that's selected
I currently have a perl script that imports HTML and converts it to plain
Having trouble setting up a website for my wedding. Hope someone can find out
Could someone please explain what the for loop in this class does? Specifically the

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.