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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:02:24+00:00 2026-05-15T22:02:24+00:00

I have a web application which will return a user id based on the

  • 0

I have a web application which will return a user id based on the first segment of the url, much like Twitter:

http://www.myapplication.com/user-name-goes-here/

It can go deeper too, like so:

http://www.myapplication.com/user-name-goes-here/news/article_1/

In order to break the site down, I am using the following URL routing technique:

(r'^(?P<slug>\w+)/', include('myapp.sites.urls')),

This will then further route the user to the correct page, but as it stands I am having to query the database in every view in order to obtain the user_id based on the first url segment. I was hoping to somehow automate this so I don’t have to bloat my views with the same code each time… my solution was to create some middleware which checks the url segment and returns a 404 if its not found:

    from django.http import Http404

class DomainMiddleware(object):
    def process_request(self, request):
        from myapp.sites.models import Sites

        dname = request.path.split('/')[1]
        if not dname:
            return
        try:
            d = Sites.objects.get(domain__exact=dname)
        except Sites.DoesNotExist:
            raise Http404

        return

This works, but it’s trying to parse EVERY request, even those to images, favicons etc.

My question is thus; Is there a way to run this query on every page load without clogging up my views with extra code? If middleware is the solution, how can I modify my code so that it doesn’t include EVERY request, only those to successfully routed URLs?

Hope someone can help!

  • 1 1 Answer
  • 2 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-15T22:02:24+00:00Added an answer on May 15, 2026 at 10:02 pm

    The Django server shouldn’t be processing requests for static content URLs – certainly not in production anyway, where you’d have a different web server running to handle that, so this shouldn’t be an issue there.

    But if you say you’d like this to run for only sucessfully routed URLs, maybe you’d be better of using process_view rather than process_request in your middleware? http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-view

    process_view works at view level rather than request level, and provides a view_func argument which you can check so that your code doesn’t run when it’s the django.views.static.serve view used for serving static media during development.

    Whatever happens you should defs be caching that database call if it’s going to be used on every view.

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

Sidebar

Ask A Question

Stats

  • Questions 480k
  • Answers 480k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would recommend using UserControls to break up your functionality.… May 16, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer Our solution was in similar situations to create a dynamic… May 16, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer If you still need the eventTextField. You should create a… May 16, 2026 at 6:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.