如何将 request.user 导入 Django 中的 TemplateView
How to import request.user into TemplateVew in djnago
我需要知道如何使用渲染页面的 TemplateView 方法检查用户是否经过身份验证。
我已将其添加到我的上下文处理器中:
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
]
我的 views.py 目前看起来像:
from django.shortcuts import render
from CreateVuln.forms import *
from django.views.generic import TemplateView
from django.template import RequestContext
from pages.decorators import *
vulnform = vulnform
class Dashboard(TemplateView):
template_name = 'vuln_pages/Create_Vuln.html'
def get(self, request):
Outform = {
'vulnform': vulnform,
}
return render(request, self.template_name, Outform)
def post(self, request):
forminput = vulnform(request.POST or None)
if forminput.is_valid():
forminput.save()
forminput = vulnform()
inoutform = {
'vulnform': forminput,
}
return render(request, self.template_name, inoutform, )
else:
inoutform = {
'vulnform': vulnform,
}
return render(request, self.template_name,inoutform )
# Create your views here.
class ViewVulns(TemplateView):
template_name = 'vuln_pages/Create_Vuln.html'
def get(self, request):
return render(request, self.template_name)
我想使页面无法通过 GET 请求查看,也无法通过 POST 请求更新。
我试过使用 RequestContext。但是文档看起来很复杂,我似乎无法理解如何使用它。
你可以试试这个:if request.user.is_authenticated:
If a view is using this mixin, all requests by non-authenticated users will be redirected to the login page or shown an HTTP 403 Forbidden error, depending on the raise_exception
parameter.
<b>from django.contrib.auth.mixins import LoginRequiredMixin</b>
class Dashboard(<b>LoginRequiredMixin,</b> TemplateView):
# your code
我需要知道如何使用渲染页面的 TemplateView 方法检查用户是否经过身份验证。
我已将其添加到我的上下文处理器中:
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
]
我的 views.py 目前看起来像:
from django.shortcuts import render
from CreateVuln.forms import *
from django.views.generic import TemplateView
from django.template import RequestContext
from pages.decorators import *
vulnform = vulnform
class Dashboard(TemplateView):
template_name = 'vuln_pages/Create_Vuln.html'
def get(self, request):
Outform = {
'vulnform': vulnform,
}
return render(request, self.template_name, Outform)
def post(self, request):
forminput = vulnform(request.POST or None)
if forminput.is_valid():
forminput.save()
forminput = vulnform()
inoutform = {
'vulnform': forminput,
}
return render(request, self.template_name, inoutform, )
else:
inoutform = {
'vulnform': vulnform,
}
return render(request, self.template_name,inoutform )
# Create your views here.
class ViewVulns(TemplateView):
template_name = 'vuln_pages/Create_Vuln.html'
def get(self, request):
return render(request, self.template_name)
我想使页面无法通过 GET 请求查看,也无法通过 POST 请求更新。 我试过使用 RequestContext。但是文档看起来很复杂,我似乎无法理解如何使用它。
你可以试试这个:if request.user.is_authenticated:
If a view is using this mixin, all requests by non-authenticated users will be redirected to the login page or shown an HTTP 403 Forbidden error, depending on the
raise_exception
parameter.
<b>from django.contrib.auth.mixins import LoginRequiredMixin</b>
class Dashboard(<b>LoginRequiredMixin,</b> TemplateView):
# your code