Q: " local variable 'response' referenced before assignment " error in djnago ##   while creating views 'add_car' method i tried to assign value to 'response' variable but it shows errors.If i type ' response =None '  the output i get is None
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

import json
from .models import Car

def index(request):
    response=json.dumps([{}])
    return HttpResponse(response,content_type='text/json')
def get_car(request,car_name):
    if request.method == 'GET':
        try:
            car=Car.objects.get(name=car_name)
            response=json.dumps([{'Car':car.name,'Top speed':car.top_speed}])
        except:
            response=json.dumps([{'Error':'No car with that name'}])
    return HttpResponse(response,content_type='text/json')
@csrf_exempt
def add_car(request):
    #response=None
    if request.method =='POST':
        payload=json.loads(request.body)
        car_name=payload['car_name']
        top_speed=payload['top_speed']
        car=Car(name=car_name,top_speed=top_speed)
        try:
            car.save()
            response=json.dumps([{'Success':'Car added succesfully'}])
        except:
            response=json.dumps([{'Error':'Car could not ne added'}])
    return HttpResponse(response,content_type='text/json')


A: Probably, you don't call this view with the POST method.
The if request.method =='POST': hasn't executed, so return HttpResponse(response,content_type='text/json') can't find response variable.
