, 나는 다음과 같이 사용자의 다른 유형을 가지고 :한 종류의 사용자가 django의 URL을 통해 다른 사용자의보기에 액세스하는 것을 거부하는 방법은 무엇입니까? 내 프로젝트에서
User |-Client |-Employee |-Technical Manager |-Service Manager |-Production Manager
없음 사용자가 url
하여 다른 사용자의보기에 액세스 할 수 없습니다. 클라이언트는 기술 관리자의 프로필에 지정된 URL /officer/profile
을 통해 기술 관리자의 프로필에 액세스 할 수 없습니다. 그런 다음 @permission_required(lambda u: u.has_perm('authentication.view_client'))
:이 같은 장식을 사용하여 각 뷰에 대해 다음
class Client(models.Model): class Meta: permissions = ( ('view_client', 'view_Client'), )
다음 models.py
내 Client class
에서 이렇게하기 위해
, 나는이 코드를 사용 기술 관리자로 로그인하여이 URL로 액세스하려고합니다. /client/profile
그런 다음 오류 function object is not iterable
이 있습니다.
authentication
이라는 앱에서 내 사용자를 만들고있었습니다. models,py
은 다음과 같습니다 를 '
@python_2_unicode_compatible
class Profile(models.Model):
user = models.OneToOneField(User)
user_sex = (('MALE', 'Male'), ('FEMALE', 'Female'))
sex = models.CharField(max_length=6, default='Male', choices=user_sex)
address = models.CharField(max_length=250, null=True, blank=True)
city = models.CharField(max_length=250, null=True, blank=True)
state = models.CharField(max_length=250, null=True, blank=True)
country = models.CharField(max_length=250, null=True, blank=True)
phone = PhoneNumberField(blank=True)
zip = models.IntegerField(null=True, blank=True)
about = models.CharField(max_length=250, null=True, blank=True)
email_confirmed = models.BooleanField(default=False)
account_type = models.IntegerField(default=-1)
class Meta:
db_table = 'auth_profile'
class Employee(models.Model):
user = models.OneToOneField(User)
manager = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
designation = models.CharField(max_length=6)
class Meta:
db_table = 'auth_employee'
class Client(models.Model):
user = models.OneToOneField(User)
class Meta:
db_table = 'auth_client'
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
if instance.profile.account_type == 0:
if not Client.objects.filter(user=instance).exists():
Client.objects.create(user=instance)
instance.client.save()
else:
instance.client.save()
if instance.profile.account_type == 1 or instance.profile.account_type == 2 or instance.profile.account_type == 3:
if not Employee.objects.filter(user=instance).exists():
Employee.objects.create(user=instance)
instance.employee.save()
else:
instance.employee.save()
'
프로필 클래스가 좀 큽니다. 그것을 무시하십시오. 이제 내가 원했던 것은 다른 사용자가 모든 유형을 만들 때마다 권한을 할당한다는 것입니다.
오류를 해결하는 방법과이를 수행하여 한 유형의 사용자가 URL을 입력하여 다른 사용자의보기에 액세스 할 수 없게하려면 어떻게해야합니까?
@login_required(login_url='/login/')
@permission_required(lambda u: u.has_perm('authentication.view_client'))
def contact(request):
user = request.user
if request.method == 'POST':
# form = ContactForm()
form = ContactForm(request.POST)
if form.is_valid():
user.profile.address = form.cleaned_data.get('address')
user.profile.city = form.cleaned_data.get('city')
user.profile.state = form.cleaned_data.get('state')
user.profile.country = form.cleaned_data.get('country')
user.profile.phone = form.cleaned_data.get('phone')
user.profile.zip = form.cleaned_data.get('zip')
user.save()
messages.add_message(request,
messages.SUCCESS,
'Your contact was successfully edited.')
else:
form = ContactForm(instance=user, initial={
'address': user.profile.address,
'city': user.profile.city,
'state': user.profile.state,
'country': user.profile.country,
'phone': user.profile.phone,
'zip': user.profile.zip,
})
return render(request, 'client/client_contact.html', {'form': form})
그리고 내가 가진 오류 :
TypeError at /client/picture
'function' object is not iterable
Request Method: GET
Request URL: http://127.0.0.1:8000/client/picture
Django Version: 1.11.6
Exception Type: TypeError
Exception Value:
'function' object is not iterable
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py in has_perms, line 285
Python Executable: /usr/bin/python2.7
Python Version: 2.7.12
Python Path:
['/home/shamsad/PycharmProjects/OpenGMS',
'/home/shamsad/PycharmProjects/OpenGMS',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/shamsad/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0']
Server time: Sun, 29 Oct 2017 16:21:25 +0000
당신이를 게시 할 수있는 예를 들어, 아마도 당신은 단지
Client
개체와 연관된 사용자에게 표시 할 뷰가 오류에 대한 전체 추적 및보기? 보기는 주어진 요청이 요청한 콘텐츠에 액세스 할 수 있는지 여부를 확인하는 곳입니다. – souldeux내 질문을 편집했습니다. – sphoenix
또한 추적을 게시했습니다 – sphoenix