我正在使用Django 1.9,我需要为用户添加权限,使其只能在管理面板中查看数据。目前,我唯一的选择是添加,更改和删除。我不需要将此方法应用于所有用户。理想情况下,我希望它能够分配用户(或组)查看权限,就像在Django 2.2中一样。
有人知道我该怎么做吗?
我真的不知道该怎么做。
答案 0 :(得分:0)
我已经从事过类似的项目,这就是完成的方式
在 Views.py
中from django.shortcuts import *
from .models import *
from .forms import *
def viewstudents(request):
all_info = DataStudent.objects.all()
context={'all_info' :all_info}
return render(request, 'accounts/Students/viewstudents.html', context)
在您的 urls.py
中from django.urls import path
from django.conf.urls import url, include
from . import views
urlpatterns=[
url(r'^viewstudents', views.viewstudents, name="viewstudents")
]
在您的 viewstudents.html
中{% extends 'base.html' %}
{% block body %}
<div class="container" style="margin-left: 200px;font-size: 20px; padding: 0px 10px;">
<br><h4>Manage Students</h4>
<nav class="nav navbar-expand" style="float:left">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#"><button class="btn btn-default">List of Students</button></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'addstudent' %}"><button class="btn btn-success">Add Students</button></a>
</li>
</ul>
</nav>
<br>
<div class="container" style="margin-left:2px; font-size:15px; padding:3px">
<table class="table table-hover" border="2">
<thead class="table-success">
<tr>
<td>id</td>
<td>Photo</td>
<td>Name</td>
<td>Class</td>
<td>Stream</td>
<td>Admision No</td>
<td>Action</td>
</tr>
</thead>
<tbody>
{% if all_info %}
{% for item in all_info %}
<tr>
<td>{{ item.id}}</td>
<td>{% if item.Student_Photo %}<img src="{{ item.Student_Photo.url}}" width="50">{% endif %}</td>
<td>{{ item.name}}</td>
<td>{{ item.Class}}</td>
<td>{{ item.stream}}</td>
<td>{{ item.admission_no}}</td>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<td><a class="btn btn-primary a-btn-slide-text" href="{% url 'singlestudentdetails' pk=item.id %}"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
<span><strong>View</strong></span></a>
<form id='delete-student' method="POST" action="{% url 'deletestudent' pk=item.id %}">
{% csrf_token %}
<a class="btn btn-danger a-btn-slide-text" href="{% url 'deletestudent' pk=item.id %}"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<span><strong>Delete</strong></span> </a>
</form>
</td>
{% endfor %}
</tr>
{% endif %}
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
window.mytest = function()
{
var isValid = confirm('Are you sure ?');
if (!isValid) { event.preventDefault();
alert("It wont delete. Yay!");}}
</script>
{% endblock %}
这是我的 model.py
from django.db import models
from django.utils import timezone
from datetime import datetime
class DataStudent(models.Model):
sex = (('female','female'), ('male','male'))
reli = (('moslem','moslem'), ('Christian','Christian'),('Others','Others'))
relation = (('Brother','Brother'), ('Sister','Sister'),('Mother','Mother'),
('Father','Father'), ('Uncle','Uncle'),('Auntie','Auntie'))
school = models.ForeignKey(School, on_delete=models.PROTECT, blank=True, null=True)
religion = models.CharField(max_length=130, choices=reli, blank=False)
name = models.CharField(max_length=100, default="Student_name")
username = models.CharField(max_length=100)
gender = models.CharField(max_length=130, choices=sex, blank=False)
Class = models.ForeignKey(Classinformation, on_delete=models.PROTECT, blank=True, null=True)
stream = models.ForeignKey(Sectioninformation, on_delete=models.PROTECT, blank=True, null=True)
admission_no = models.CharField(max_length=130)
def __str__(self):
return self.name
为我的开发人员编码感到高兴