我有一個條目很少的模型。我想只展示其中的幾個并保護其他人......例如:
class Squadra(models.Model):
...
tipo = models.IntegerField(choices=TIPO_SQUADRA, default=1)
...
然后我有一個 ListView 使用過濾器只回傳幾個 Squadra 物件:
all_squadre = Squadra.objects.filter(tipo=3)
和一個 url 檔案,其中包括:
path('squadra_table/<int:squadra>/', views.SquadraTableListView.as_view() ),
我的問題是有人可以訪問 Squadra 頁面,然后他可以嘗試隨機更改 url 中的 ID <int:squadra> 并訪問我想保護的其他條目......我該怎么做?
感謝您的幫助
阿蒂利奧
uj5u.com熱心網友回復:
為了保護您的視圖,您需要覆寫調度方法并根據您的需要放置一些邏輯,例如:
class ProtectedView(TemplateView):
template_name = 'secret.html'
def dispatch(self, request, *args, **kwargs):
entity = get_object_or_404(Entity, pk=args[0])
if not check_permission(request, entity):
raise Http404
return super(MyView, self).dispatch(request, *args, **kwargs)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511552.html
標籤:django列表显示
