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.

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.

This article has been dead for over six months. Start a new discussion instead.