简体   繁体   中英

Python Django How to get value of logged user in views.py?

I have CustomUser class for logging and another class coach where I have information about my users. Every user are linked to coach class. I would like to create template where my users if they have completed their profile will see their information and if they didn't completed their they will see a message. I'am learning Python Django and I don't know for example how to get Adresse of my logged users from class coach and check if it is empty or not. Any idea how to fix this?

My views.py

d

def Profile(request):
    u = request.user.username
    x = u.coach.Adresse

    if len(x)!= 0:
        completed = "profile completed"
        return render(request, 'Profile.html', {'completed': completed})
    else:
        notcompleted = "please complete your profile"
        return render(request, 'Profile.html', {'notcompleted': notcompleted})

My models.py

class coach(models.Model):
    user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
    Adresse = models.TextField(max_length=140, default='DEFAULT VALUE')
    Telephone = models.IntegerField(null=True, max_length=140, default='111')

The least painful and indeed Django-recommended way of doing this is through a OneToOneField(User) property.

Extending the existing User model …

If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.

That said, extending django.contrib.auth.models.User and supplanting it also works...

Substituting a custom User model Some kinds of projects may have authentication requirements for which Django's built-in User model is not always appropriate. For instance, on some sites it makes more sense to use an email address as your identification token instead of a username.

[Ed: Two warnings and a notification follow, mentioning that this is pretty drastic.]

Instead of Writting your own UserClass just extends the existing user model..

Your request is added to the templates automatically which means that you have access to it in the template and there is no need to pass it in the context. It is passed to the template by render function.

So write this in your Profile.html template:

{% if request.user.is_authenticated %}
    <p>
        {% if request.user.coach.Adresse != 'DEFAULT VALUE' %}
            Profile Completed
        {% else %}
            Please complete your profile
        {% endif %}    
    </p>
{% endif %}

And change the Profile method to

def Profile(request):
    return render(request, 'Profile.html')

But I think you should change the Adresse field because I see no reason why would you use a default value. Just delete the default value and allow it to be blank and null as follows:

Adresse = models.TextField(max_length=140, blank=True, null=True)

And in template instead of {% if request.user.coach.Adresse != 'DEFAULT VALUE' %} write {% if request.user.coach.Adresse %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM