Z
Z
zigen2015-07-17 17:09:02
Django
zigen, 2015-07-17 17:09:02

Django how to query a user's profile in a template and check group membership?

Good afternoon. There is a rather trivial task to create two types of users on the site.
I originally, I guess. went the wrong way and simply extended the built-in User model with two profiles, and when registering one of the types of users, he added them to the group so that they could be separated.
Now I am creating personal accounts for users and I have encountered a seemingly not difficult problem.
How to understand in the template whether the user belongs to a certain group and, depending on the result, give a link to the profile. And the second - how to get the value of the user profile field in the template.

class VendorProfile(models.Model):
    user = models.OneToOneField(User, related_name='vprofile')
    org_name = models.CharField(max_length=50, blank=True)
    slug = models.SlugField(unique=True)
    #....

class UserProfile(models.Model):
    user = models.OneToOneField(User,related_name='uprofile')
    website = models.URLField(blank=True)
    avatar = models.ImageField(upload_to='profile_images', blank=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Dunayevsky, 2015-07-17
@zigen

In the article, I wrote how to make an extended user model: dunmaksim.blogspot.ru/2015/05/django-18.html
To solve your problem, you need:
We make it even simpler in the form (shown for ClassBasedView, mandatory authorization is also required, but you can finish it for yourself):

from os import path

from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.generic import View


class Login(View):

    def get(self, request):
        c = {}
        c.update(csrf(request))
                RequestContext(request, c)
        return render_to_response(
            path.join('login', 'index.html'),
            RequestContext(request, c)
        )


class LoginRequiredMixin(object):

    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        return login_required(view)


class Desktop(LoginRequiredMixin, View):

    def get(self, request):
        c = {}
        c.update(csrf(request))
        user = request.user
        if user.is_admin:
            return render_to_response(
                path.join('admin', 'index.html'),
                RequestContext(request, c)
            )
        if user.is_vendor:
            return render_to_response(
                path.join('vendor', 'index.html'),
                RequestContext(request, c)
            )
        return render_to_response(
            path.join('desktop', 'index.html'),
            RequestContext(request, c)
        )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question