在django中获取用户个人资料 [英] get user profile in django

查看:514
本文介绍了在django中获取用户个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello我是新的python和django
我需要一个获取当前用户配置文件的视图我知道我从用户使用get_profile,但我不知道如何使用它。我读了django文件,并没有帮助我。
这是我从doc找到的:

  from django.contrib.auth.models import User 
profile = request.user.get_profile()


解决方案

Django的文档所有这一切,特别是存储关于用户的其他信息。首先,您需要在 models.py 中的某个位置定义一个模型,其中包含用户的其他信息的字段:



models.py

  from django.contrib.auth.models import User 

class UserProfile(models.Model):
#此字段是必需的。
user = models.OneToOneField(User)

#其他字段
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length = 20,default =Dragons)

然后,您需要指出这个模型( UserProfile )是在您的 settings.py 内设置 AUTH_PROFILE_MODULE 的用户配置文件: / p>

settings.py

  
AUTH_PROFILE_MODULE ='accounts.UserProfile'
...

您需要以您的应用程序的名称替换帐户。最后,您要每次通过注册一个 post_save 处理程序创建一个 User 实例时创建一个配置文件,这样每个创建用户的时间Django也会创建他的个人资料:



models.py

  from django.contrib.auth.models import User 

class UserProfile(models.Model):
#此字段为必填字段。
user = models.OneToOneField(User)

#其他字段
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length = 20,default =Dragons)


def create_user_profile(发件人,实例,创建,** kwargs):
如果创建:
UserProfile.objects.create = instance)

post_save.connect(create_user_profile,sender = User)



访问配置文件



要在视图中访问当前用户的配置文件,只需使用请求提供的用户实例,然后致电 get_profile

  def your_view(request):
profile = request.user.get_profile()
...
#你的代码


hello i'm new in python and django I need a view that get current user profile I know I shoud use get_profile from User but I don't know how to use it . i read the django document and It didn't help me. this is what I found from doc:

from django.contrib.auth.models import User
profile=request.user.get_profile()

解决方案

Django's documentation says it all, specifically the part Storing additional information about users. First you need to define a model somewhere in your models.py with fields for the additional information of the user:

models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    accepted_eula = models.BooleanField()
    favorite_animal = models.CharField(max_length=20, default="Dragons.")

Then, you need to indicate that this model (UserProfile) is the user profile by setting AUTH_PROFILE_MODULE inside your settings.py:

settings.py

...
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
...

You need to replace accounts with the name of your app. Finally, you want to create a profile every time a User instance is created by registering a post_save handler, this way every time you create a user Django will create his profile too:

models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    accepted_eula = models.BooleanField()
    favorite_animal = models.CharField(max_length=20, default="Dragons.")


def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

Accessing the Profile

To access the current user's profile in your view, just use the User instance provided by the request, and call get_profile on it:

def your_view(request):
    profile = request.user.get_profile()
    ...
    # Your code

这篇关于在django中获取用户个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆