我的问题与View permissions in Django不一样,因为那个解释了如何处理Django 2.1及更高版本中的问题,当我使用Django 1.1时,它具有“查看”权限的概念。没有。
Django 1.11
我有一个群组用户,他们只能对网站上的所有内容进行只读访问。对字段,模型和空间数据没有限制,只能对它们执行的操作(只读)。我知道可能的实现建议“逐场”(或“使所有字段只读”)和“逐个模型”解决方案。我很好奇,如果有一种方法可以更清洁,在用户组级别,或至少在用户级别。
我的views.py到目前为止是默认的:
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
"""View function for home page of site."""
# Render the HTML template index.html with the data in the context variable
return render(request, 'home.html')
理想情况下,我希望能够用夹具做到这一点。目前在夹具中,我的组定义如下:
{
"model": "auth.group",
"fields": {
"name": "some_group",
"permissions": [
[
"add_somemodel",
"myproject",
"somemodel"
],
[
"change_somemodel",
"myproject",
"somemodel"
],
[
"delete_somemodel",
"myproject",
"somemodel"
]
]
}
}
在Django 2.2中,我可以做到
{
"model": "auth.group",
"fields": {
"name": "some_group",
"permissions": [
[
"view_somemodel",
"myproject",
"somemodel"
]
]
}
}
但是在Django 1.11中,我只有“添加”,“删除”和“更改” - 没有“查看”选项(根据文档enter link description here)。那么,有没有办法创建一个夹具,创建一个只具有读取权限的组?
在您的视图中,您需要这样的内容(请注意,如果您属于正确的访问组,这是如何查看帖子的示例):
def post_detail(request, slug=None):
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
instance = get_object_or_404(Post, slug=slug)
share_string = quote_plus(instance.content)
context = {
"title": instance.title,
"instance": instance,
"share_string": share_string,
}
return render(request, "post_detail.html", context)
注意:
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
以下是一些可以帮助您的文档链接:
All attributes to django.contrib.auth
编辑:我现在看到了你的代码,所以你想要实现的就是这样
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
"""View function for home page of site."""
# With that way although a user might be logged in
# but the user might not have access to see the page
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
# Render the HTML template index.html with the data in the context variable
return render(request, 'home.html')
这样,用户可能会登录,但如果不是工作人员或超级用户,则可以访问该页面。
谢谢大家回复。我没有找到使用Django 1.11在db中只使用user / group / permissions配置的方法,也许它不存在。这就是我最终得到的结果(非常类似于我在4小时前开始研究时遇到的第一个建议,代码更改最少)
[
{
"model": "auth.group",
"fields": {
"name": "<my_new_group>",
"permissions": [
[
"change_<somemodel1>",
"<myproject>",
"<somemodel1>"
],
[
"change_<somemodel2>",
"<myproject>",
"<somemodel2>"
]
]
}
,
{
"model": "auth.user",
"fields": {
"password": "<my_password>",
"last_login": null,
"is_superuser": false,
"username": "<my_username>",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": true,
"is_active": true,
"date_joined": "2019-04-01T14:40:30.249Z",
"groups": [
[
"<my_new_group>"
]
],
"user_permissions": []
}
}
],
这照顾了第一部分:现在当我以这个用户身份登录时,对于我的用户的任何模型,我没有“添加新...”或“删除”按钮。
admin.py
中,我在其自定义的def changeform_view
中添加了:if request.user.groups.filter(name='<my_new_group>'):
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
extra_context['show_save'] = False
这使得所有模型的3个“保存”按钮消失,并使所有字段都是只读的。现在这个新用户无法添加,删除或编辑任何模型的任何内容,但他们可以看到所有内容,就像我想要的那样。
看起来在较新的Django中,从2.1开始,这可以做得更好。