view user profile access restriction

18 views
Skip to first unread message

sum abiut

unread,
Jan 28, 2018, 8:00:28 PM1/28/18
to django...@googlegroups.com
Hi,
i have a django app that i want the users to be able to view only their user profile once they have login. currently any user that login is able to view other users profile as well. Appreciate is you could point me to the right direction.

cheers,

Dylan Reinhold

unread,
Jan 28, 2018, 9:04:05 PM1/28/18
to django...@googlegroups.com
There are a bunch of ways to do it.
Show us your view, if it's a function based view, just do your select on (username=request.user)

Dylan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y7ZZx0BfQe0jwZu8-h%2Bo7nU%3DZYg%3Dgr2nxYeU%2BjskVTCtw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

sum abiut

unread,
Jan 28, 2018, 11:18:34 PM1/28/18
to django...@googlegroups.com
Thanks heaps that worked. But then how to i retrieve the rest of the profile info from the second table? i have a one-to-one relationship. i mange to extract data from the user table which is first name, last name, email. but i am having difficulty figuring out accessing information from the second table (Profile)

view.py

ef loggin(request):
    username=None
    if request.user.is_authenticated:
        info=request.user
        return render(request,'dashboard.html',locals())




my model.py

class Profile(models.Model):
    user=models.OneToOneField(User, on_delete=models.CASCADE)
    bio=models.TextField(default='',blank=True)
    sex=(
            ('Male','Male'),
            ('Female','Female'),

    )

    Gender=models.CharField(max_length=100,choices=sex,blank=True,default='')
    Phone=models.CharField(max_length=20,blank=True,default='')
    island=models.CharField(max_length=100,blank=True,default='')
    city=models.CharField(max_length=100,blank=True,default='')
    country=models.CharField(max_length=100,blank=True,default='')
    organization=models.CharField(max_length=100,blank=True,default='')
   

    account_number=models.CharField(max_length=100,blank=True,default='')
    bank_phone=models.CharField(max_length=100,blank=True,default='')


def create_profile(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        user_profile = Profile(user=user)
        user_profile.save()
post_save.connect(create_profile, sender=User)








sum abiut

unread,
Jan 29, 2018, 12:57:55 AM1/29/18
to django...@googlegroups.com
I manage to fixed it. I have created two instances (profile_info and info) in my view, i use the first instance to access the information from my Profile model and the second instance to access th User model. I also set the AUTH_PROFILE_MODULE = 'Profile' in my settings.py.

my updated view.py


def loggin(request):
  
    if request.user.is_authenticated:
        profile_info=request.user.profile

        info=request.user
        return render(request,'dashboard.html',locals())


cheers,

Daniel Roseman

unread,
Jan 29, 2018, 4:27:18 AM1/29/18
to Django users
On Monday, 29 January 2018 05:57:55 UTC, suabiut wrote:
I manage to fixed it. I have created two instances (profile_info and info) in my view, i use the first instance to access the information from my Profile model and the second instance to access th User model. I also set the AUTH_PROFILE_MODULE = 'Profile' in my settings.py.


The first part is fine, but AUTH_PROFILE_MODEL hasn't done anything since Django 1.7 - there's no point in setting it.
-- 
DR.

sum abiut

unread,
Jan 29, 2018, 5:11:14 AM1/29/18
to django...@googlegroups.com
Thanks heaps , I noticed that. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages