How to use permissions on a CreateView class?

166 views
Skip to first unread message

Fellipe Henrique

unread,
May 28, 2019, 7:54:38 AM5/28/19
to Django Users
Hello,

I have these class, based on CreateView class... and I only want allow user with these permissions to add record...

class ClienteCreateView(ERPbrViewMixin, CreateView):
template_name = 'cadastro/cliente/form.html'
permission_required = ('cliente.can_open', 'cliente.can_edit', 'cliente.can_add')
model = Cliente
form_class = ClienteForm
But, not working... user without these permission, when type the url show the form...

Any tips how to do that?

Cheers!


T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Joe Reitman

unread,
May 28, 2019, 12:55:17 PM5/28/19
to Django users
Fellipe,

Here is an example of decorating class based views from the documentation:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

class ProtectedView(TemplateView):
    template_name = 'secret.html'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

Jim Illback

unread,
May 28, 2019, 1:36:51 PM5/28/19
to Django users
I think you can also add to your class (right under your template_name… for example) this statement:

	permission_required = appname.permission_name'
This will limit to logged on users (as below), and also to users who possess this permission.
Jim


--
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...@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/060a2c59-bf39-456c-a686-bf6ba104e1f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fellipe Henrique

unread,
May 28, 2019, 5:54:39 PM5/28/19
to Django Users
Hi Jim, 

Tried that, as you can see on my code, but not working.. when user type the url, still see the template..

Any suggestions?

Regards,

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Ken Whitesell

unread,
May 29, 2019, 12:09:16 PM5/29/19
to Django users
Fellipe,

In addition to the permissions_required variable, I believe you also need to include the PermissionsRequired mixin in your class definition:


Ken
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To post to this group, send email to djang...@googlegroups.com.

--
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...@googlegroups.com.

Andrew C.

unread,
May 30, 2019, 7:26:16 PM5/30/19
to django...@googlegroups.com
Try clearing your browser history and info. Should also test in Incognito or Private browser so info is saved. I have a suspicion that your browser is saving login info and you’re just not realizing this.

Andrew C.

unread,
May 30, 2019, 7:26:41 PM5/30/19
to django...@googlegroups.com
Sorry. Info ISN’T saved on private browser

Fellipe Henrique

unread,
May 31, 2019, 8:18:30 AM5/31/19
to Django Users
Thanks guys!
The problem was the permission mixins on my class...

Thanks!

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Reply all
Reply to author
Forward
0 new messages