Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get user profile in django

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()
like image 783
nim4n Avatar asked Nov 19 '12 19:11

nim4n


People also ask

How can I see profile in Django?

Open users app urls.py and add the route for profile view. Create the template for the view inside the users app template directory. We will modify this template later to display profile of the user, but first there are a couple of things we need to do.

What is User Profile in Django?

Perhaps one of the most common way (and also less intrusive) is to extend the User model using a one-to-one link. This strategy is also known as User Profile. One of the challenges of this particular strategy, if you are using Django Admin, is how to display the profile data in the User edit page.

How do I change user details in Django?

Add "accounts" at the bottom of "INSTALLED_APPS". And add the "AUTH_PROFILE_MODULE" config at the bottom of the entire file. We will add "accounts" app in settings.py and use the "AUTH_USER_MODEL" config to tell Django to use our another model. And our another model will UserProfile.


1 Answers

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
like image 50
César Avatar answered Oct 15 '22 22:10

César