Django个人资料图片不上传

发布于 2025-01-26 18:39:18 字数 2885 浏览 2 评论 0原文

这个问题被问到我不知道多少,但没有给出我的答案
我要制作的应用程序是一个来自教程的小博客WebApp(对于Django进行了一些培训,这是我第一次使用它,所以...)
有我的代码:

# models
from django.db import models
from django.contrib.auth.models import User
from PIL import Image

# Create your views here

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

嗯,这还不是全部:

@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(
            request.POST,
            instance=request.user
        )
        p_form = ProfileUpdateForm(
            request.POST,
            request.FILES,
            instance=request.user.profile
        )
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    context = {
        'title': 'Profile',
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

目前,我更改了所有设置,我做了一些HTML代码:

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <div class="media">
            <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
            <div class="media-body">
                <h2 class="account-heading">{{ user.username }}</h2>
                <p class="text-secondary"></p>
            </div>
        </div>
        <!-- FORM HERE -->
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group" enctype="multipart/form-data">
                <legend class="border-bottom mb-4">Profile Info</legend>
                {{ u_form|crispy }}
                {{ p_form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Update</button>
            </div>
        </form>
    </div>
{% endblock %}

没有任何错误,帖子请求效果很好,我不知道该怎么办...

This question was asked I don't know how much but it doesn't give my answers
The app I was gonna make is a little blog webapp from a tutorial (for some training in Django, it's the first time I use it so...)
There is my code:

# models
from django.db import models
from django.contrib.auth.models import User
from PIL import Image

# Create your views here

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

Well, that's not all:

@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(
            request.POST,
            instance=request.user
        )
        p_form = ProfileUpdateForm(
            request.POST,
            request.FILES,
            instance=request.user.profile
        )
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    context = {
        'title': 'Profile',
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

At this time, I changed all my settings and I did some html code:

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <div class="media">
            <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
            <div class="media-body">
                <h2 class="account-heading">{{ user.username }}</h2>
                <p class="text-secondary"></p>
            </div>
        </div>
        <!-- FORM HERE -->
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group" enctype="multipart/form-data">
                <legend class="border-bottom mb-4">Profile Info</legend>
                {{ u_form|crispy }}
                {{ p_form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Update</button>
            </div>
        </form>
    </div>
{% endblock %}

Not any errors, the post request works very well, I don't know what to do so...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文