简体   繁体   中英

how can i save an image for any user

In my website user can upload a image to edit it so i should save his image in model and to access again to image i should save username of user for any image. I want when user go to home page his image be deleted. But i have problem. This error: OperationalError at / no such column: imgProcess_image.username

# model
    class Image(models.Model):
        username = models.CharField(unique=True, max_length=30)
        image = models.ImageField(upload_to='user_images')


# form
    class Upload(forms.ModelForm):
        username = Image.username

        class Meta:
            model = Image
            fields = ['image']

        def set_user(self, un):
            self.username = un


# views
    def delete_image_of_user(request):
        Image.objects.get(username=request.user.username).delete()

    def home(request):
        delete_image_of_user(request)
        hash = {}
        comments = CommentModel.objects.all()
        hash["comments"] = comments
        return render(request, "imgProcess/home.html", hash)

Your database has unapplied migrations if you see in your database you wont find username column in Image table. You should build and apply migrations. Run python manage.py makemigrations then python manage.py migrate to apply them.

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