from django.shortcuts import render
def my_view(request):
#you can check user here with request.user
#example
if request.user.is_superuser:
return render('your_template_for_admin.html', {})
return render('your_template_for_basic_user.html', {})
from django.views import View
class MyView(View):
def get_context_data(self, **kwargs):
#example code assuming that we have a relation between schools and admin A
context = super().get_context_data(**kwargs)
context['schools'] = School.objects.filter(admin_id=self.request.user__id)
return context
每个view函数都接受一个
request
参数,因此无论您在何处定义视图函数,它都可能如下所示:编辑:如果您使用的是基于类的视图,那么可以像这样重写它的get方法:
^{pr2}$根据注释进行编辑:您可以使用get_context_data(),而不是@Daniel Roseman在评论中所说的
get()
。在然后可以在模板中使用
schools
queryset。在相关问题 更多 >
编程相关推荐