How to change is_staff when I click on href?

0

I have a table with users in HTML with id and name, by default when registering a person it comes with is_staff = False.

I want when I click the Authorize link change to is_staff = True, how do I do this in the view?

 {% block content %}

         <p>Usuários</p>
        <table class="striped" id="cadastrados">
        <thead>
          <tr>
              <th>Código</th>
              <th>Nome</th>
              <th>Telefone</th>
              <th>Ramal</th>
              <th>É staff?</th>
              <th>Autorizar</th>
          </tr>
        </thead>
            <tbody>

          {% for item in lista_usuarios %}
             <tr>
              <td>{{ item.id }}</td>

                <td>{{ item.first_name }}</td>
                 <td>{{ item.telefone }}</td>
                  <td>{{ item.ramal }}</td>
                  <td> {{ item.is_staff }} </td>
                  <td> <a href="{% url 'cadastro:autorizar' %}"></a>  <a class="btn-floating btn-large red">
                        <i class="large material-icons">mode_edit</i></a> </td>
             </tr>
          {% endfor %}
       </tbody>
        </table>



        {% endblock content %}

views.py

def quantities(request, value=None):
    if value == "2":

        usuarios = Perfil.objects.filter(is_staff=False)
        context = {'lista_usuarios': usuarios}
    else:
        usuarios = Perfil.objects.filter(is_staff=True)
        context = {'lista_usuarios': usuarios}

    return render(request, 'quantities.html', context)
    
asked by anonymous 14.09.2017 / 19:16

1 answer

1

This view you posted is about url (register: authorize)? I do not think so. So I'm going to show you how it would look like a view from scratch.

The first step is to prepare your url to receive a parameter (which will be the id of the user you intend to "authorize"). Then in your urls.py let's add:

url(r'^cadastro/autorizar/(?P<user_id>[0-9]+)$', 'views.funcao_autorizar', name='autorizar')

Now in your urls file you have a url that expects for example: 'register / authorize / 5' and this theoretically will authorize the user of number 5.

The way to pass in the django url is:

<a href="{% url 'cadastro:autorizar' user_id=item.id %}">

And your view is:

def funcao_autorizar(request, user_id):

  try:
      usuario = User.objects.get(id=user_id)
  except Exception as e:
      # retorna erro para o usuario

  usuario.is_staff = True
  usuario.save()
  # retorna para a pagina de sequinte
    
18.09.2017 / 02:35