pythondjangorequestdjango-viewshttp-request

Difference betweeng using Request and specific parameter names in Django


What is the key difference between view methods with just the request keyword or specifying exact parameter names?

For example, I see in code that there is view like this:

def get_accounts(request):
   in name in request.GET and request.GET['name']

And the other variant:

def get_accounts(request, name, some_custom_parameters):
   if name and some_custom_validations:
      some_expressions

My question regards the difference in usage. If I get a POST request from the front-end via jQuery or anything else; when (and why) do I have to use just the request keyword alone and when to specify concrete parameters as request can include all desired parameters within itself.


Solution

  • The extra arguments to your function are taken from your path configuration.

    You set URL patterns; if you include elements in the URL pattern those will be passed on to your view as extra arguments:

    urlpatterns = patterns('',
        url(r'^accounts/(?P<name>\w+)/(?P<some_custom_element>\w+)$', views. get_accounts),
        # ...
    )
    

    In the above example the view is configured to listen to any URL starting with accounts/ and including two more elements; those are passed to your view as arguments.

    In other words, it is not POST or GET request data that is being passed in to those arguments.

    You probably want to re-read the Django tutorial on this subject.

    You'd use extra parameters in your path to create friendly, bookmarkable URLs. The path /users/1553537/giorgi-tsiklauri is a lot more readable and friendly than /users?id=1553537.