‘WSGIRequest’ object has no attribute: can’t get a value for a var

  django, python, python-3.x

I am trying to add a functionality – an inquiry form, everything else works fine, but upon submission I can’t get a value for my user_id variable. Any help and/or comments is much appreciated.

models.py:

from django.db import models
from datetime import datetime

# Create your models here.
class Contact(models.Model):
    listing = models.CharField(max_length=200)
    listing_id = models.IntegerField()
    name = models.CharField(max_length=200)
    email = models.CharField(max_length=100)
    phone = models.CharField(max_length=100)
    message = models.TextField(blank=True)
    contact_date = models.DateTimeField(default=datetime.now, blank=True)
    user_id = models.IntegerField(default=0, blank=True)
    def __str__(self):
        return self.name

views.py:

from django.shortcuts import render, redirect
from django.contrib import messages

from . models import Contact

# Create your views here.


def contact(request):
    if request.method == 'POST':
        listing_id = request.POST['listing_id']
        listing = request.POST['listing']
        name = request.POST['name']
        email = request.POST['email']
        phone = request.POST['phone']
        message = request.POST['message']
        if 'user_id' in request.POST:
            user_id = request.POST['user_id']
        else:
            user_id = False
        #user_id = request.POST['user_id']
         realtor_email = request.POST['realtor_email']

        # check if already made inquiry
    
    

    if request.user.is_authenticated:
      user_id = request.user_id         # problematic
      has_contacted = Contact.objects.all().filter(listing_id=listing_id, user_id=user_id)
      if has_contacted:
          messages.error(request, "Inquiry already made,")
          return redirect('/listings/'+listing_id)

    contact = Contact(listing=listing, listing_id=listing_id, name=name, email=email, phone=phone, message=message, user_id=user_id,)

    contact.save()

    messages.success(request, 'Your request has been submitted, a realtor will get back to you soon,')
    return redirect('/listings/'+listing_id)

admin.py:

from django.contrib import admin
from . models import Contact
# Register your models here.



class ContactAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'listing', 'email', 'contact_date')
    list_display_links = ('id', 'name')
    search_fields = ('name', 'email', 'listing')
    list_per_page = 25





admin.site.register(Contact, ContactAdmin)

directories and relevant html:

enter image description here

Error:

AttributeError at /contacts/contact
‘WSGIRequest’ object has no attribute ‘user_id’
Request Method: POST
Request URL: http://127.0.0.1:8000/contacts/contact
Django Version: 3.1.7
Exception Type: AttributeError
Exception Value:
‘WSGIRequest’ object has no attribute ‘user_id’
Exception Location: C:Devbtre_projectbtrecontactsviews.py, line 35, in contact
Python Executable: C:Devbtre_projectvenvScriptspython.exe
Python Version: 3.8.0
Python Path:
[‘C:Devbtre_projectbtre’,
‘C:UsersHPAppDataLocalProgramsPythonPython38-32python38.zip’,
‘C:UsersHPAppDataLocalProgramsPythonPython38-32DLLs’,
‘C:UsersHPAppDataLocalProgramsPythonPython38-32lib’,
‘C:UsersHPAppDataLocalProgramsPythonPython38-32’,
‘C:Devbtre_projectvenv’,
‘C:Devbtre_projectvenvlibsite-packages’]

C:Devbtre_projectbtrecontactsviews.py, line 35, in contact
      user_id = request.user_id …
▼ Local vars
Variable    Value
email   
'[email protected]'
listing 
'187 Wookrow St.'
listing_id  
'3'
message 
'kuytrddtyuiop'
name    
'test1'
phone   
'2223334444'
realtor_email   
'[email protected]'
request 
<WSGIRequest: POST '/contacts/contact'>
user_id 
False

Source: Python-3x Questions

LEAVE A COMMENT