Milan
Milan

Reputation: 182

Django python AttributeError Exception Value: 'WSGIRequest' object has no attribute 'shop'

i am writing module of Django shopping site user can add shop and products

when user fill the form for adding product and hit submit it django give this error, it would be very helpful if someone can put some light on the problem, thank you.

Error

'WSGIRequest' object has no attribute 'shop'

Request Method:     POST
Request URL:    http://127.0.0.1:8000/15/add_product/
Django Version:     2.1.2
Exception Type:     AttributeError
Exception Value:    

'WSGIRequest' object has no attribute 'shop'

Exception Location:     C:\Users\MILAN\PycharmProjects\DjangoProject\shopsurfer\views.py in form_valid, line 191
Python Executable:  C:\Users\MILAN\PycharmProjects\DjangoProject\venv\Scripts\python.exe
Python Version:     3.7.0
Python Path:    

['C:\\Users\\MILAN\\PycharmProjects\\DjangoProject',
 'C:\\Users\\MILAN\\PycharmProjects\\DjangoProject\\venv\\Scripts\\python37.zip',
 'C:\\Users\\MILAN\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\MILAN\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\MILAN\\AppData\\Local\\Programs\\Python\\Python37',
 'C:\\Users\\MILAN\\PycharmProjects\\DjangoProject\\venv',
 'C:\\Users\\MILAN\\PycharmProjects\\DjangoProject\\venv\\lib\\site-packages',
 'C:\\Users\\MILAN\\PycharmProjects\\DjangoProject\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg']

Server time:    Tue, 12 Feb 2019 14:37:12 +0000

product_form.html

this is where all the html code which is generating the form form the input

{% extends 'shopsurfer/base.html' %}
{% block title %}Add a New Product{% endblock %}
{% block album_active %}active{% endblock %}

{% block body %}
    <div class="container=fluid">
        <div class="row">
            <div class="col-sm-12 col-md-7">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                            {% csrf_token %}


{% for field in form %}
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <span class="text-danger small">{{ field.errors }}</span>
        </div>
        <label class="control-label col-sm-2" for="song_title">{{ field.label_tag }}</label>
        <div class="col-sm-10">{{ field }}</div>
    </div>
{% endfor %}

                            <div class="form-group">
                                <div class ="col-sm-offset-2 col-sm-10">
                                    <button type="submit" class="btn btn-success">submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

Models.py file contain Shop, Product, User model which is stored in sqlite default database

class Product(models.Model):
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    category = models.CharField(max_length=32)
    lot = models.DecimalField(max_digits=16, decimal_places=0)
    specs = models.CharField(max_length=5120)
    price = models.FloatField(max_length=16)
    product_logo = models.FileField(default='')


    def get_absolute_url(self):
        return reverse('shopsurfer:detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.name

Forms.py this is form field which is used to create form for the model

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name', 'category', 'lot', 'specs', 'price', 'product_logo']

Views.py here is the code for the AddProduct which is i thing contain error but i am not able to find what.

class AddProduct (LoginRequiredMixin, CreateView):
    login_url = '/login_user'
    redirect_field_name = 'redirect_to'
    model = Product
    fields = ['name', 'category', 'lot', 'specs', 'price', 'product_logo']

    def form_valid(self, form):
        object_product = form.save(commit=False)
        object_product.shop = self.request.shop
        object_product.save()
        return super(AddProduct, self).form_valid(form)

Upvotes: 0

Views: 525

Answers (1)

Milan
Milan

Reputation: 182

using kwargs we can get vaule of the primary key and we can use that value to get the object Shop

urls.py

 url(r'^(?P<pk>[0-9]+)/add_product/$', views.AddProduct.as_view(), name='add_product'),

views.py

pk = self.kwargs['pk']
object.shop = get_object_or_404(Shop, pk=pk)

and by doing this we can able to store the value to the database.

by the way thanks for helping @malberts

Upvotes: 1

Related Questions