我在Django中更新个人资料照片时遇到问题

jdzmm42g  于 2023-01-14  发布在  Go
关注(0)|答案(1)|浏览(150)

我正在尝试更新Django的个人资料照片
根据yt教程,我创建了更新照片的形式:

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = User_Model
        fields = ["profile_photo"]

这是我的类来改变视图中的照片:

class UpdatePhoto(TemplateView):
    template_name = "Messenger/update_photo.html"
    profile_form = UserProfileForm

    def post(self, response):
        data = response.POST
        file_data = response.FILES
        profile_form = UserProfileForm(data, file_data, instance=response.user.profile)
        if profile_form.is_valid():
            profile_form.save()
            return redirect("/")
        print("rere")
        context = self.get_context_data(profile_form=profile_form)
        return self.render_to_response(context)

我的模型:

class User_Model(models.Model):
user = models.OneToOneField(
    User, null=True, on_delete=models.CASCADE, related_name="profile"
)
profile_photo = models.ImageField(
    upload_to="images/", default="default_photo.jpg", null=True, blank=True
)
chats = models.ManyToManyField(User, related_name="chats", blank=True)
blocked_list = models.ManyToManyField(User, related_name="blocked_list", blank=True)

我尝试使用和不使用'response.post',结果是一样的,它不更新。然而,当我尝试打印'file_data'时,我得到了这样的结果:

<MultiValueDict: {'Browser': [<InMemoryUploadedFile: thumb.jpg (image/jpeg)>]}>

主要网址:Settings.py:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = "static/"

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

以及主网站www.example.comurls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("Messenger.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

应用程序URL:

from django.urls import path
from django.contrib.auth.decorators import login_required
from . import views

urlpatterns = [
    path(
        "",
        login_required(views.Chat_list_Usernames.as_view(), login_url="/login"),
        name="home",
    ),
    path(
        "change_status/",
        login_required(views.change_status, login_url="/login"),
        name="change_status",
    ),
    path("login/", views.login_page, name="login_page"),
    path(
        "logout/", login_required(views.logout_user, login_url="/login"), name="logout"
    ),
    path("register/", views.register_page, name="register_page"),
    path(
        "update_photo/",
        login_required(views.UpdatePhoto.as_view(), login_url="/login"),
        name="update_photo",
    ),
    path(
        "change_password/",
        login_required(views.ChangePassword.as_view(), login_url="/login"),
        name="change_password",
    ),
    path(
        "send_message/",
        login_required(views.SendMessage.as_view(), login_url="/login"),
        name="send_message",
    ),
]

我的模板是:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/update_photo.css'%}">
<div class="main-content">
    <div class="heading">Change</div>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="photo">
            <div class="button">
                <input id="files" required type="file" name="Browser"
                    accept="image/png, image/jpeg, image/jpg, image/webp">
            </div>
            <div class="buttons">
                <div class="button">
                    <a href="{% url 'home'%}"><input type="button" name="Back" , value="Back"></a>
                </div>
                <div class="button">
                    <input type="submit" name="Change" , value="Change">
                </div>
            </div>
    </form>
</div>

此外,我需要提到,我可以更新照片,虽然管理面板,但它是如何工作的。
我不知道我做错了什么,也不知道该如何改正。

hgqdbh6s

hgqdbh6s1#

应该使用UpdateView而不是TemplateView,因为它只呈现给定的模板,上下文包含URL中捕获的参数。

相关问题