简体   繁体   中英

Uploading an image file to database

I am making a social media app and i am adding a edit profile function to it. But for some reason, the profile image which the user chooses is not being uploaded to the database. Please help me to do so.

My views.py

def edit_profile(request):
    user = request.user
    user_profile = profile.objects.get(user__username=user)
    Profile = user_profile
    myuser = User.objects.get(username=user)

    if request.method == 'POST':
        try:
            username = request.POST['username']
            name = request.POST['name']
            email = request.POST['email']
            image = request.FILES['img']

            myuser.username = username
            myuser.first_name = name
            myuser.email = email
            myuser.save()
            Profile.user = myuser.username
            Profile.name = myuser.first_name
            Profile.img = image
            Profile.save()

            return redirect('/')
        except:
            return redirect('/')

    else:
        context = {
            'user':user_profile,
            'myuser':myuser,
        }
        return render(request, 'edit_profile.html', context)

my tempateview

<form action="/users/edit_profile/" method="POST" enctype="multipart/form-data">
{% csrf_token %}

<center>
<img src="{{user.img.url}}" style="border-radius: 50%; widows: 150px;height:150px; margin-left:-50px" onclick="document.getElementById('image').click()"><br>

<span class="click" onclick="document.getElementById('image').click()">Change Profile Picture</span>
</center>
<input type="file" name="img" id="image" accept="image/*" style="display:none"><br><br>

<label> &nbsp;Username </label><br>
<input type="text" name="username" class="edit" value="{{user.user}}" spellcheck="false"><br><br>

<label> &nbsp;Name </label><br>
<input type="text" name="name" class="edit" value="{{user.name}}" spellcheck="false"><br><br>

<label> &nbsp;Email </label><br>
<input type="email" name="email" class="edit" value="{{myuser.email}}" spellcheck="false"><br><br><br>

<input type="submit" value="Save changes" style="width:40%; float:right; margin-right:120px">

</form>

my models.py

class profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    img = models.ImageField(default='default.png', upload_to='profile_pics')

    def __str__(self):
        return "Profile({})".format(self.user.username)

Everything is working. Only the image file cannot be uploaded. Please help. Thankyou in advance.

this was a problem for me too. i solved this by saving the object like so:

 from django.core.files import File 
 Profile.img.save(image.name  , File(image))
 Profile.save()

hope it helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM