Shikhar Upadhyay
Shikhar Upadhyay

Reputation: 1

Django: Undefined variable `request`

Why is this showing an undefined variable request.

views.py

from django.shortcuts import render,redirect
from F_UI import models

def blocks():
    if request.method=="GET":
        return render(request,"blocks.html")

Upvotes: 0

Views: 719

Answers (2)

Ahmed Tarek
Ahmed Tarek

Reputation: 1

You need to add a request parameter in a view.

Like this:


def blocks(request):
   pass

Django will pass the HttpRequest object when calling it.

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.

Documentation link

Upvotes: 0

Lewis
Lewis

Reputation: 2798

Add request as positional parameter, like so:

def blocks(request, *args, **kwargs):
    if request.method=="GET":
        return render(request,"blocks.html")

Upvotes: 1

Related Questions