A
A
alexkomp2019-07-21 15:16:03
Django
alexkomp, 2019-07-21 15:16:03

How to make a form visible only to a user from a certain group in Django?

How to make a form visible only to a user from a certain group in Django?
here is the form template on the site:

<!doctype  html>
<html  lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link  rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"  integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"  crossorigin="anonymous">
<title>Создать мерчь</title>
{% load staticfiles %}
<link rel="stylesheet" href="/static/login.css" type="text/css"/>
</head>
<body>

  <div  style='' class="container">
    <div  class="row justify-content-center">
      <div id='block'>
      <h1  class="text-center">Создать мерчь</h1>

{% block content %}
<div class="card">
<div class="card-body">
<h4 class="card-title"></h4>
  <form action='' method="post" enctype="multipart/form-data">
    {% csrf_token %}
      {% for field in form %}
      <p>
   
    {{ field.label_tag }}<br>
        {{ field }}
        {% if field.help_text %}
          <small style="display: none">{{ field.help_text }}</small>
        {% endif %}
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
      </p>
      {% endfor %}
  <button type="submit" class="btn btn-primary btn-block">Создать мерчь</button>
  </form>
  </div>
</div>
{% endblock %}
</div>
    </div>
  </div>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-07-21
@alexkomp

  1. Check for membership in the view and submit the form to the template context only for those users who are members of the correct group.
  2. Create permission, issue it to the group and check in the template that the current user has it.
  3. Write a filter to check membership.
  4. Add a membership checking method to the user model.

D
Denioo, 2019-07-21
@Denioo

Add groups, assign them to users, and add a check for the user's membership in the group, and if he is in the group, then show the table, otherwise give an error. So it seems to be correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question