Home > Enterprise >  local variable 'name' referenced before assignment in Django
local variable 'name' referenced before assignment in Django

Time:07-20

My views.py

def index(request):
if request.method=="POST":
    name=request.POST.get('name')
    email=request.POST.get('email')
    phone=request.POST.get('phone')
    message=request.POST.get('message')
contact=Contact(name=name, email=email, phone=phone, message=message)
contact.save()
return render(request, 'home/index.html')

Error

C:\Django venv\CodingSchool\home\views.py, line 12, in index

    contact=Contact(name=name, email=email, phone=phone, message=message)

CodePudding user response:

Your name variable is outside of if tag. So take it in:

def index(request):
    if request.method=="POST":
        name=request.POST.get('name')
        email=request.POST.get('email')
        phone=request.POST.get('phone')
        message=request.POST.get('message')
        contact=Contact(name=name, email=email, phone=phone, message=message)
        contact.save()
    return render(request, 'home/index.html')
  • Related