gpt4 book ai didi

django - 在 Django 中创建用户个人资料页面

转载 作者:行者123 更新时间:2023-12-02 05:28:05 26 4
gpt4 key购买 nike

我是 Django 的初学者。我需要设置一个网站,其中每个用户都有一个个人资料页面。我见过 django 管理。用户的个人资料页面,应该存储一些只能由用户编辑的信息。谁能指出我这怎么可能?任何教程链接都会非常有帮助。另外,django 是否有任何模块可用于设置用户页面。

最佳答案

您只需要创建一个可供经过身份验证的用户使用的 View ,并在用户创建 GET 请求时返回配置文件编辑表单,或者在创建请求时更新用户的配置文件数据。 POST 请求。

大部分工作已经为您完成,因为有 generic views用于编辑模型,例如 UpdateView 。您需要扩展的是检查经过身份验证的用户并向其提供您想要提供编辑的对象。这是 MTV 三元组中的 View 组件,提供编辑用户个人资料的行为 - Profile 模型将定义 user profile并且模板将单独提供演示文稿。

因此,这里有一些行为可以作为简单的解决方案:

from django.contrib.auth.decorators import login_required
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import UpdateView
from django.utils.decorators import method_decorator

from myapp.models import Profile


class ProfileObjectMixin(SingleObjectMixin):
"""
Provides views with the current user's profile.
"""
model = Profile

def get_object(self):
"""Return's the current users profile."""
try:
return self.request.user.get_profile()
except Profile.DoesNotExist:
raise NotImplemented(
"What if the user doesn't have an associated profile?")

@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
"""Ensures that only authenticated users can access the view."""
klass = ProfileObjectMixin
return super(klass, self).dispatch(request, *args, **kwargs)


class ProfileUpdateView(ProfileObjectMixin, UpdateView):
"""
A view that displays a form for editing a user's profile.

Uses a form dynamically created for the `Profile` model and
the default model's update template.
"""
pass # That's All Folks!

关于django - 在 Django 中创建用户个人资料页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com