Django是否不使用Django Forms保存图像? [英] Django Save image without using Django Forms?

查看:35
本文介绍了Django是否不使用Django Forms保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在媒体文件夹中不使用Django表单的情况下保存图像.图片应保存在 media \ seller_profile 文件夹中,并且必须在 seller_profile \ abc.png 之类的数据库中输入其路径,但是以某种方式我只能得到 abc.png 缺少Seller_profile.

I am trying to save the image without using Django forms in the media folder. The image should save in the media\seller_profile folder and its path has to be entered in a database like seller_profile\abc.png but somehow I am getting only abc.png missing seller_profile.

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%}

通过引用链接来更新Models.py .根据此解决方案更新,请不要调用保存.

Updating Models.py by referring link. According to this solution update do not call save.

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'

推荐答案

在您的配置文件功能 def profile(request):

您要处理图片的地方

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

相反,您必须使用 request.FILES.get()

您不必仅输入图像名称

所以最后您的行看起来像这样

So at last your line will look like this

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

这篇关于Django是否不使用Django Forms保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆