How to change is_staff when I click href?

Asked

Viewed 48 times

1

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

I want when I click the Authorize link to switch 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 %}

py views.

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)

1 answer

1


This view you posted is related to url (register:authorize)? It seems not. So I’ll show you what a view would look like 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 we will add:

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

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

The way to pass 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
  • Thank you, very practical the way you showed me, when I inspect the page every authorize button is getting the right id, but when I click nothing happens

  • Well there are several ways to test and identify the error. One of them (and the simplest) would be to put the url in the browser: localhost:8000/register/update/5 (if you are in a local environment) and see the error it will return. Remember to keep an eye on the Django console.

  • When I access the url it returns the name of the person, that is to say it is picking up, right? only the href link that is not working even

  • When you click on the link appears what? the page url changes? It may be useful to open another ask and post a print.

  • Nothing happens like there’s no link

  • seems a simple mistake in href

  • I did, it was the <a> closing in the wrong place, it was like this: <a href="{% url 'register:authorize' item.id %}" class="btn-floating btn-large red"> <i class="large material-icons">mode_edit</i> </a>

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.