【问题标题】:Django Save image without using Django Forms?Django 保存图像而不使用 Django 表单?
【发布时间】:2021-07-06 15:01:22
【问题描述】:

我正在尝试不使用媒体文件夹中的 Django 表单来保存图像。该图像应保存在 media\seller_profile 文件夹中,并且必须将其路径输入到 seller_profile\abc.png 之类的数据库中,但不知何故我只得到 abc .png 缺少卖家资料。

Models.py

class SellerProfile(models.Model):
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255, default=None, null=True)
    seller_profile_picture = models.ImageField(upload_to='seller_profile', default="seller_profile/empty_profile.png")

    def __str__(self):
        return self.email

Views.py

def profile(request):
    
    if request.method == "POST":
        
        profile_data = SellerProfile.objects.filter(email = str(request.user)).update(
                                                    first_name = request.POST.get("First name"),
                                                    seller_profile_picture = request.FILES["seller profile picture"],
                                                    )
        print("object File --->", request.FILES["seller profile picture"])
        return redirect("/seller/profile")
    else:
        user_data = SellerProfile.objects.filter(email=str(request.user))
        if len(user_data) == 0:
            SellerProfile.objects.create(email = str(request.user))
            data_entered = {"First name": "Value not Provided"}
        else:
            data_entered = dict()
            data_entered["First name"] = user_data[0].first_name

        return render(request, "seller/profile.html", {"data_entered":data_entered})

seller/profile.html

{% extends 'general/layout.html'%}

{% block body %}

<div class="container">
    <div class="jumbotron">
        <form method="post" enctype=multipart/form-data>
            {%  csrf_token %}
            {% for attribute, placeholder in data_entered.items %}
                <div class="form-group">
                    <dt><label for="{{attribute}}">{{attribute}}</label>
                    <dd><input class="form-control" id="{{attribute}}" name="{{attribute}}"
                               type="text" placeholder="{{placeholder}}" required>
                    </dd>

                </div>
            {% endfor %}

            <dt><label for="seller profile picture">Profile Picture</label>
                    <dd><input class="form-control" id="seller profile picture" name="seller profile picture"
                               type="file" required>
                    </dd>
            </dt>


            <div class="form-group text-center">
                <p>
                    <input class="btn btn-primary " type="submit" value="Update">
                </p>
            </div>

        </form>
    </div>
</div>

{% endblock%}

通过引用 link 更新 Models.py。根据这个解决方案更新不要调用保存。

def profile(request):
     if request.method == "POST":
            print("get ", SellerProfile.objects.get(email = str(request.user)))
            print("type get ", type(SellerProfile.objects.get(email = str(request.user))))
 
            profile_data = SellerProfile.objects.get(email = str(request.user))
            profile_data.email = str(request.user),
            profile_data.first_name = request.POST.get(seller_char_attribute[0]),
            profile_data.seller_profile_picture = request.FILES["seller profile picture"],
            profile_data.save()

但这给了我如下错误

 Virtialenv\lib\site-packages\django\db\models\fields\files.py",line 286, in pre_save
        if file and not file._committed:
    AttributeError: 'tuple' object has no attribute '_committed'

【问题讨论】:

  • 你是如何从数据库中获取 URL 的?
  • 我怎么没明白你的意思。您能否详细说明您的问题?

标签: python django post django-models django-forms


【解决方案1】:

您在为profile_data 对象赋值的某些行的末尾留下了尾随逗号。这些逗号意味着将分配元组而不是您想要的值:

profile_data.email = str(request.user),
profile_data.first_name = request.POST.get(seller_char_attribute[0]),
profile_data.seller_profile_picture = request.FILES["seller profile picture"],

从上面的行中删除尾随逗号应该可以解决。

【讨论】:

    【解决方案2】:

    在您的个人资料功能def profile(request):

    你在哪里处理图片

    profile_data.seller_profile_picture = request.FILES["seller profile picture"],
    

    你必须使用request.FILES.get()而不是这个

    不要在列表类型["seller profile picture"]中传递图像名称@你必须只给出图像名称

    所以最后你的行会变成这样

    profile_data.seller_profile_picture = request.FILES.get("seller profile picture")
    

    【讨论】:

    • 以及需要删除行尾的逗号。正如@Will Keeling 所建议的那样。
    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2014-05-19
    • 2017-04-18
    相关资源
    最近更新 更多