Python 登录所需的装饰程序,包含许多可能的HTML';同一个URL的名称
我想限制对django通用视图提供的URL的访问。我已经研究了登录所需的decorator,但在让它工作方面只取得了部分成功,因为我有一个文档中没有提到的复杂问题(或者至少我找不到它) 在添加装饰器之前,在URL.py中,我有以下内容:Python 登录所需的装饰程序,包含许多可能的HTML';同一个URL的名称,python,django,login-required,Python,Django,Login Required,我想限制对django通用视图提供的URL的访问。我已经研究了登录所需的decorator,但在让它工作方面只取得了部分成功,因为我有一个文档中没有提到的复杂问题(或者至少我找不到它) 在添加装饰器之前,在URL.py中,我有以下内容: url(r'^search/(?P<search_type>\w+)', search) //在URL.py中 from views import AboutView url(r'^search/(?P<search_type>\w+
url(r'^search/(?P<search_type>\w+)', search)
//在URL.py中
from views import AboutView
url(r'^search/(?P<search_type>\w+)',
login_required(AboutView.as_view(template_name_1, template_name_2,), search)),
从视图导入关于视图
url(r'^search/(?P\w+),
需要登录(关于视图作为视图(模板名称1,模板名称2),搜索)),
但我得到的错误是模板名称1和模板名称2不存在
非常感谢您的帮助。与类视图一起使用
从django.views.generic导入模板视图
class AboutView(TemplateView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectedView, self).dispatch(*args, **kwargs)
template_name_1 = "search_form.html"
template_name_2 = "search_form_manual.html"
template_name_3 = "search_results.html"
template_name_4 = "tag_search_results.html"
不管decorator的用法如何,如果需要使用多个模板,我看不出如何将基于
search()
函数的视图替换为单个TemplateView
。url(r'^search/(?P\w+),需要登录(search))
有什么问题?在设置需要登录之前,哪个视图正在处理您的搜索功能请求?这是你应该装饰的视图。需要登录(搜索)很好-谢谢。我不知道我能做到。
from django.views.generic import TemplateView
class AboutView(TemplateView):
template_name_1 = "search_form.html"
template_name_2 = "search_form_manual.html"
template_name_3 = "search_results.html"
template_name_4 = "tag_search_results.html"
from views import AboutView
url(r'^search/(?P<search_type>\w+)',
login_required(AboutView.as_view(template_name_1, template_name_2,), search)),
class AboutView(TemplateView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectedView, self).dispatch(*args, **kwargs)
template_name_1 = "search_form.html"
template_name_2 = "search_form_manual.html"
template_name_3 = "search_results.html"
template_name_4 = "tag_search_results.html"