Talha Murtaza
Talha Murtaza

Reputation: 344

Request error in django view when setting up user

I have an app which lets the currently logged in user to create a Person object. That person object has the current logged in user as it's owner. But as I save the form it give me the following error

global name 'request' is not defined

I am trying to achieve this using Django CreateView

the error trackback

Traceback:

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\edit.py" in post
  217.         return super(BaseCreateView, self).post(request, *args, **kwargs)

File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\edit.py" in post
  183.             return self.form_valid(form)

File "C:\Users\Bitswits 3\Desktop\Maala\MaalaWeddings\userfiles\views.py" in form_valid
  120.         obj.user_relation = request.user

Exception Type: NameError at /Personadd/
Exception Value: global name 'request' is not defined

Views.py

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from django.shortcuts import *
    from django.contrib.admin.views.decorators import staff_member_required
    from .forms import *
    from django.shortcuts import *
    from .models import *
    from django.contrib.auth.forms import *
    from django.http import *
    from datetime import *
    from django.contrib.auth import *
    from django.contrib.auth.decorators import login_required
    from django.contrib.auth.models import User
    from django.views.generic import CreateView, ListView, DetailView
    from django.views import generic
    from .models import *
    import requests


        class PersonCreate(CreateView):
            model = PersonData
            template_name = "userfiles/add-person.html"

            fields = [
                'person_first_name',
                'person_last_name',
                'person_address',
                'person_email',
                'person_contact_number',
                'person_members'             ]



            def form_valid(self, form):
                obj = form.save(commit=False)
                obj.user_relation = request.user
                obj.save()
                return HttpResponse("DONE")

models.py

class PersonData(models.Model):

    user_relation = models.ForeignKey(
        User, on_delete=models.CASCADE)

    person_first_name = models.CharField("First Name", max_length=25)

    person_last_name = models.CharField("last Name", max_length=25)

    person_address = models.CharField("Address", max_length=512)

    person_email = models.EmailField("Email", max_length=512)

    person_contact_number = models.PositiveIntegerField("Phone Number", blank=True, null=True)

    person_members = models.PositiveSmallIntegerField(
        "Family members", blank=True, null=True)

I am unable to understand as to why is this error even occurring in the first place, as I thought that request is used everywhere in Django

Upvotes: 0

Views: 505

Answers (1)

Exprator
Exprator

Reputation: 27523

well as you are using a Class Based View

all the parameters or variable you want to use needs self to be attached to it

so ,

obj.user_relation = self.request.user

self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class. It binds the attributes with the given arguments.

Upvotes: 1

Related Questions