I'm jumping into Python/Django, and have been hitting some speed bumps. I'm a somewhat novice programmer. I know the basics, but haven't done anything more than create a PHP webform, and build very simple websites. I have a thousand questions about Django (most centering around Views and customizing the Admin, specifically as they relate to querying the DB), but I'll start with this seemingly simple one, and hopefully someone can help me out.

In views.py, I begin with 'from django.http import HttpResponse'. I then create a boring function:

def hello(request)
    return HttpResponse("Hello world")

Seems simple enough, right?. While reading the django book, however, I learned that 'request' is "an instance of the class django.http.HttpRequest." This is where I get confused. I've never instantiated a class by simply typing a word. I have very little experience with OOP, but every time I've seen it and done it, it worked something like this:

from django.http import HttpRequest

request = HttpRequest()

That is how I thought an instance of the django.http.HttpRequest class woule be instantiated. There is so much of Django that is leaving me with questions that seem so simple, yet I can't wrap my head around. I'm humiliated that I can't get this. All I've read about the framework is that it's easy to pick up and quick to develop with. I tried to build a simple group-like site (posts and replies), but nearly smashed my computer because of how difficult it became. If I was using PHP and MySQL, it would have been slower to develop, maybe, but undoubtedly would have been completed. With Drupal, it would have taken all of 60 seconds.

Anyway, if someone can answer this first question, I would be grateful. And if the answer is plainly obvious, and you think I'm a moron, that's fine. Please, though, I'd still appreciate some help. Thanks in advance.

5 Years
Discussion Span
Last Post by JO_4

when you write a view function, it must have a request parameter (try writing one without it). This is because Django automatically passes the HttpRequest() to your view so you can use it. You don't have to use it, I have plenty of views that do absolutely nothing with the request (not on my part anyway). Django is instantiating the HttpRequest(), not you.

It lets you do things like this (not tested):

def my_search_view(request):

    # Retrieve GET/POST argument from http request
    GET_arg1 = request.REQUEST.get('search_query', None)

    # Validate arguments
    if GET_arg1 is None:
        # No argument found, tell the user
        response = HttpResponse("No search argument given!")
        # Use the argument we found to do something.
        response = HttpResponse("You are searching for: " + GET_arg1)

    return response

The request has alot of useful information in it like request.POST, request.GET, request.REQUEST (GET and POST combined), request.META (useful info about the request). I think Django needs to use the request itself anyway, so passing it to you is just a convenience. Just one of the many "magic" things Django does for you. I hope this helps.

Edited by chriswelborn: added explanation


but i want get request object in routers.py to choice a database and dont can get request object.
I need request.META.get('HTTP_HOST') to depend of domain to choice a database

from django.http import HttpRequest

class DatabaseRouter(object):

    def db_for_read(self, model, **hints):
        request = HttpRequest()
        if request.META.get('HTTP_HOST) == 'domain1':
            return 'database1'
        if request.META.get('HTTP_HOST) == 'domain2':
            return 'database2'
        return None

    def db_for_write(self, model, **hints):
        request = HttpRequest()
        if request.META.get('HTTP_HOST) == 'domain1':
            return 'database1'
        if request.META.get('HTTP_HOST) == 'domain2':
            return 'database2'
        return None

    def allow_syncdb(self, db, model):

        if db == 'database1' or db == 'database2':
            return model._meta.app_label == 'app'
        elif model._meta.app_label == 'app':
            return False

        return None

how can to do this ?
help me please

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.