Django 管理员 super_user 和普通用户权限
Django admin super_user and normal user permissions
我做了一个视图,在管理页面中显示所有 Transactions
model
。所以基本上我在管理页面中添加了一个按钮,它将我重定向到自定义 html 视图,其中包含所有 Transactions
.
我的views.py
def pdfs(request, *args, **kwargs):
transactions = Transaction.objects.all()
template_path = 'report.html'
context = {"transactions":transactions}
response = HttpResponse(content_type='Application/pdf')
response['Content-Disposition'] = 'filename="reportss.pdf'
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(html, dest=response)
if pisa_status.err:
return HttpResponse('we had some errors' + html )
return response
models.py
class Transaction(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
chp_reference = models.CharField(max_length=50, unique=True)
rent_effective_date = models.DateField(null=True, blank=True)
income_period = models.CharField(max_length=11)
group = models.OneToOneField(Group,on_delete=models.CASCADE,null=True)
def __str__(self):
return str(self.chp_reference)
@property
def print_pdf(self):
if self.complete:
view_link = reverse('pdfss')
mark_safe_param = '<a href= {} target="_blank" >Print Report</a>'.format(view_link)
return mark_safe(mark_safe_param)
return 'Insufficient Info.'
这是我的 html
但我会保持简短
{% for transaction in transactions %}
{% if transaction.complete %}
<th style="color:#3531FF">Report For Tenant ( {{ transaction.chp_reference }} )</span>
<tr>
<td class="tg-0pky" colspan="9">CHP Reference</td>
<td class="tg-0pky" colspan="3">{{transaction.chp_reference}}</td>
</tr>
<tr>
<td class="tg-0pky" colspan="9">Rent Effective From (dd/mm/yyyy)</td>
<td class="tg-0pky" colspan="3">{{transaction.rent_effective_date}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">CRA Fortnightly Rates valid for 6 months from</td>
<td class="tg-0lax" colspan="3">{{transaction.cra_rate_from}}</td><hr>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Market Rent of the Property</td>
<td class="tg-0lax" colspan="3">{{transaction.property_market_rent}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Number of Family Group(s)</td>
<td class="tg-0lax" colspan="3">{{transaction.number_of_family_group}}</td>
</tr>
</tbody>
</table>
{% if transaction.complete %}
<table class="tg" style="undefined;table-layout: fixed; width: 714px">
<colgroup>
<col style="width: 86px">
<col style="width: 201px">
<col style="width: 109px">
<col style="width: 63px">
<col style="width: 121px">
<col style="width: 134px">
</colgroup>
<thead>
<tr>
{% for f in transaction.family_groups.all %}
<th style="text-align:left">Family No</th>
<th style="text-align:left">Income Type</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Rent %</th>
<th style="text-align:left">Weekly Income</th>
<th style="text-align:left"><span ">Rent Component</span></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-4erg">{{f.name}}</td>
<td >{% for m in f.family_members.all %}{{ m.num_of_family_members }}<br>{% endfor %}</td>
<td >{% for m in f.family_members.all %} {{m.name}} <br> {% endfor %}</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.effective_rent_percentage}} <br> {% endfor %}
</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.income}} <br> {% endfor %}</td>
<td class="tg-1qbe">{% for m in f.family_members.all %} {{m.income_component|stringformat:".2f"}} <br> {% endfor %}</td>
最后我将这些添加到 Transaction
中的 list_display =
。我想做的是我只想向 A 组用户显示 Transation
个对象。因此一个组中的用户将只能Print pdf
由该组创建。甚至是用户级别。我想知道如何实现这一目标。谢谢!
您可以使用 @permission_required("transaction.view")
装饰器来装饰您的视图,如 documented here。然后,您可以将权限(从模型自动创建)分配给一个组并将用户添加到该组,或者直接将权限授予用户。 Django 的超级用户身份意味着用户拥有所有权限,而无需明确授予它们。装饰器将始终评估为 True。
def pdfs(request, *args, **kwargs):
if request.user.is_superuser:
transactions = Transaction.objects.all()
else: transactions =
Transaction.objects.filter(user__groups=request.user.groups.first())
template_path = 'report.html'
context = {"transactions":transactions}
response = HttpResponse(content_type='Application/pdf')
response['Content-Disposition'] = 'filename="reportss.pdf'
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(html, dest=response)
if pisa_status.err:
return HttpResponse('we had some errors' + html )
return response
我做了一个视图,在管理页面中显示所有 Transactions
model
。所以基本上我在管理页面中添加了一个按钮,它将我重定向到自定义 html 视图,其中包含所有 Transactions
.
我的views.py
def pdfs(request, *args, **kwargs):
transactions = Transaction.objects.all()
template_path = 'report.html'
context = {"transactions":transactions}
response = HttpResponse(content_type='Application/pdf')
response['Content-Disposition'] = 'filename="reportss.pdf'
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(html, dest=response)
if pisa_status.err:
return HttpResponse('we had some errors' + html )
return response
models.py
class Transaction(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
chp_reference = models.CharField(max_length=50, unique=True)
rent_effective_date = models.DateField(null=True, blank=True)
income_period = models.CharField(max_length=11)
group = models.OneToOneField(Group,on_delete=models.CASCADE,null=True)
def __str__(self):
return str(self.chp_reference)
@property
def print_pdf(self):
if self.complete:
view_link = reverse('pdfss')
mark_safe_param = '<a href= {} target="_blank" >Print Report</a>'.format(view_link)
return mark_safe(mark_safe_param)
return 'Insufficient Info.'
这是我的 html
但我会保持简短
{% for transaction in transactions %}
{% if transaction.complete %}
<th style="color:#3531FF">Report For Tenant ( {{ transaction.chp_reference }} )</span>
<tr>
<td class="tg-0pky" colspan="9">CHP Reference</td>
<td class="tg-0pky" colspan="3">{{transaction.chp_reference}}</td>
</tr>
<tr>
<td class="tg-0pky" colspan="9">Rent Effective From (dd/mm/yyyy)</td>
<td class="tg-0pky" colspan="3">{{transaction.rent_effective_date}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">CRA Fortnightly Rates valid for 6 months from</td>
<td class="tg-0lax" colspan="3">{{transaction.cra_rate_from}}</td><hr>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Market Rent of the Property</td>
<td class="tg-0lax" colspan="3">{{transaction.property_market_rent}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Number of Family Group(s)</td>
<td class="tg-0lax" colspan="3">{{transaction.number_of_family_group}}</td>
</tr>
</tbody>
</table>
{% if transaction.complete %}
<table class="tg" style="undefined;table-layout: fixed; width: 714px">
<colgroup>
<col style="width: 86px">
<col style="width: 201px">
<col style="width: 109px">
<col style="width: 63px">
<col style="width: 121px">
<col style="width: 134px">
</colgroup>
<thead>
<tr>
{% for f in transaction.family_groups.all %}
<th style="text-align:left">Family No</th>
<th style="text-align:left">Income Type</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Rent %</th>
<th style="text-align:left">Weekly Income</th>
<th style="text-align:left"><span ">Rent Component</span></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-4erg">{{f.name}}</td>
<td >{% for m in f.family_members.all %}{{ m.num_of_family_members }}<br>{% endfor %}</td>
<td >{% for m in f.family_members.all %} {{m.name}} <br> {% endfor %}</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.effective_rent_percentage}} <br> {% endfor %}
</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.income}} <br> {% endfor %}</td>
<td class="tg-1qbe">{% for m in f.family_members.all %} {{m.income_component|stringformat:".2f"}} <br> {% endfor %}</td>
最后我将这些添加到 Transaction
中的 list_display =
。我想做的是我只想向 A 组用户显示 Transation
个对象。因此一个组中的用户将只能Print pdf
由该组创建。甚至是用户级别。我想知道如何实现这一目标。谢谢!
您可以使用 @permission_required("transaction.view")
装饰器来装饰您的视图,如 documented here。然后,您可以将权限(从模型自动创建)分配给一个组并将用户添加到该组,或者直接将权限授予用户。 Django 的超级用户身份意味着用户拥有所有权限,而无需明确授予它们。装饰器将始终评估为 True。
def pdfs(request, *args, **kwargs):
if request.user.is_superuser:
transactions = Transaction.objects.all()
else: transactions =
Transaction.objects.filter(user__groups=request.user.groups.first())
template_path = 'report.html'
context = {"transactions":transactions}
response = HttpResponse(content_type='Application/pdf')
response['Content-Disposition'] = 'filename="reportss.pdf'
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(html, dest=response)
if pisa_status.err:
return HttpResponse('we had some errors' + html )
return response