简体   繁体   English

Django python AttributeError异常值:'WSGIRequest'对象没有属性'shop'

[英]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 我正在编写Django购物网站模块,用户可以添加商店和产品

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. 当用户填写添加产品的表单并点击django提交该错误时,如果有人可以对问题进行一些说明,这将非常有帮助,谢谢。

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 product_form.html

this is where all the html code which is generating the form form the input 这是所有生成表单的html代码形成输入的地方

{% 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 Models.py文件包含存储在sqlite默认数据库中的Shop,Product,User模型

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 Forms.py,这是表单字段,用于为模型创建表单

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. Views.py这是AddProduct的代码,这是我东西包含错误,但是我找不到。

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)

using kwargs we can get vaule of the primary key and we can use that value to get the object Shop 使用kwargs可以获取主键的值,并且可以使用该值获取对象Shop

urls.py urls.py

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

views.py 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 顺便说一句,感谢您帮助@malberts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python / Django - 异常值:'WSGIRequest'对象没有属性'Meta' - Python / Django - Exception Value: 'WSGIRequest' object has no attribute 'Meta' django AttributeError 'WSGIRequest' object 没有属性 'get' - django AttributeError 'WSGIRequest' object has no attribute 'get' Django AttributeError: &#39;WSGIRequest&#39; 对象没有属性 &#39;user&#39; - Django AttributeError: 'WSGIRequest' object has no attribute 'user' Django: AttributeError at /update_item/ 'WSGIRequest' object 没有属性 'data' - Django: AttributeError at /update_item/ 'WSGIRequest' object has no attribute 'data' /Customer/Visit 'WSGIRequest' object 处的 AttributeError 在 django 中没有属性 'is_ajax' - AttributeError at /Customer/Visit 'WSGIRequest' object has no attribute 'is_ajax' in django Django testt:AttributeError:&#39;WSGIRequest&#39;对象没有属性&#39;user&#39; - Django testt: AttributeError: 'WSGIRequest' object has no attribute 'user' AttributeError: &#39;WSGIRequest&#39; 对象没有属性 &#39;app&#39; - AttributeError: 'WSGIRequest' object has no attribute 'app' "AttributeError:“WSGIRequest”对象没有属性“Get”" - AttributeError: 'WSGIRequest' object has no attribute 'Get' AttributeError:&#39;WSGIRequest&#39;对象没有属性&#39;user&#39; - AttributeError: 'WSGIRequest' object has no attribute 'user' AttributeError:“ WSGIRequest”对象没有属性“ getlist” - AttributeError: 'WSGIRequest' object has no attribute 'getlist'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM