Jane Liu
Jane Liu

Reputation: 61

Django "request" object

I just started learning Django and Python a few weeks ago and have been tasked with a project to manage form processing using a Django/Python/MySQL combination. My background is in C++, so if there are any C++ analogies in Python/Django syntax please feel free to reference them.

So far I understand what the HTTPRequest objects do, but can't understand this snippet of code:

@login_required(login_url="/some_directory/")
def xyz(request):
    item1 = request.GET['item1']
    user = request.user
    page = Page.objects.get(title = item1)
    item1info = {}
    perm_all = get_perms(user,page)
    item1info["industry"] = page.industry.split(',')

For the first line what does "@" do? Is "@login_required" a Django command or was was it defined by the coder already?

I know "def xyz(request)" defines a function, but is the parameter "request" something that's been pre-defined in another file (urls.py)?

What does request.GET['item1'] do? Is it retrieving the value of the element "item1" from the query string?

Upvotes: 1

Views: 6617

Answers (1)

rafaponieman
rafaponieman

Reputation: 1630

  • "@" is a Decorator. Login required is a decorator provided by Django that requires the current user (in request.user) to be logged in to visit this view.

  • The "request" parameter is passed to the View function when its called, by Django itself. Any valid view function must receive the request as a paramete

  • Request.GET is a python dictionary that contains all the parameters passed in the request by GET method (as part of the URL querystring).

Upvotes: 4

Related Questions