Home > Back-end >  How can I make a view with an admin and user?
How can I make a view with an admin and user?

Time:11-23

I'm making a crude in Django, and when I create it's shows and options depending if you are an administrator or a Jefe from the table. the administrator its from panel Django

class UsuarioCrear(SuccessMessageMixin, CreateView):
    model = Usuarios
    form = Usuarios
    if user.is_superuser:
        fields = "__all__" 
        success_message = 'usuario creado correctamente !'
        def get_success_url(self):
            return reverse('leer')
    else:
        fields = ['nombre', 'correo', 'contraseña', 'cedula']
        success_message = 'usuario creado correctamente !'
        def get_success_url(self):
            return reverse('leer')

So, I try with an if asking if is a superuser show al fields and if not is a super user show an specific fields, but i got an error when i do that

This is the model view

class Usuarios(models.Model): 
    id = models.BigAutoField(primary_key=True)
    nombre = models.CharField(max_length=30, blank=False)
    rol = models.CharField(max_length=30, choices=roles, default='seleccionar', blank=False)
    correo = models.CharField(max_length=30, blank=False)
    contraseña = models.CharField(max_length=30, blank=False)
    cedula = models.CharField(max_length=30, blank=False)

CodePudding user response:

Just simply import it.

from django.contrib.auth.models import User as user

Now that error will solve.

And also try this:

if user.is_superuser():
        fields = "__all__" 
        success_message = 'usuario creado correctamente !'
        user.save()
        def get_success_url(self):
            return reverse('leer')
else:
        fields = ['nombre', 'correo', 'contraseña', 'cedula']
        success_message = 'usuario creado correctamente !'
        def get_success_url(self):
            return reverse('leer')
  • Related