HttpResponseRedirect不起作用,它显示错误,因为“上下文必须是字典而不是响应”
class FooterLinksView(TemplateView):
template_name = 'pages/footerlinks.html'
model = FooterLink
def get_context_data(self, **kwargs):
context = super(FooterLinksView,self).get_context_data(**kwargs)
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
if obj.link_type == 'page':
try:
context['page_obj'] = obj
return context
except:
pass
else:
pass
print(obj.url_name)
return HttpResponseRedirect(str(obj.url_name))
答案 0 :(得分:0)
您必须始终从dict
函数返回一个get_context_data
。如果要重定向,也应该覆盖dispatch
:
class FooterLinksView(TemplateView):
...
def dispatch(self, request, *args, **kwargs):
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
if obj.link_type != 'page':
return HttpResponseRedirect(str(obj.url_name))
return super(FooterLinksView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(FooterLinksView,self).get_context_data(**kwargs)
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
context['page_obj'] = obj
return context
答案 1 :(得分:0)
get_context_data()
是返回上下文的方法,因此它必须返回context
对象。如果要重定向,请在get()
中进行。
如下所示
FooterLinksView(TemplateView)类: template_name ='pages / footerlinks.html' 型号= FooterLink
def get(self, request, *args, **kwargs):
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
if obj.link_type != 'page':
return HttpResponseRedirect(str(obj.url_name))
else:
return super().get(*args, **kwargs)
# if python2
# return super(FooterLinksView, self).get(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(FooterLinksView,self).get_context_data(**kwargs)
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
if obj.link_type == 'page':
try:
context['page_obj'] = obj
return context
except:
pass
return context
或者您可以只在extra_context
中使用TemplateView
。
FooterLinksView(TemplateView)类: template_name ='pages / footerlinks.html' 型号= FooterLink
def get(self, request, *args, **kwargs):
string_name = self.kwargs['string']
obj = FooterLink.objects.get(link_url=string_name)
if obj.link_type != 'page':
try:
self.extra_context = {
'page_obj': obj
}
except:
pass
return HttpResponseRedirect(str(obj.url_name))
else:
return super().get(*args, **kwargs)
# if python2
# return super(FooterLinksView, self).get(*args, **kwargs)
答案 2 :(得分:0)
get_context_data
中的代码正在尝试检索单个对象并将其传递给上下文字典。此功能已经内置在DetailView
中,并且似乎比TemplateView
更合适。
from django.views.generic import DetailView
class FooterLinksView(DetailView):
model = FooterLink
template_name = 'pages/footerlinks.html'
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.link_type == 'page':
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
return HttpResponseRedirect(str(obj.url_name))
现在我们唯一需要添加的就是如何在FooterLink
方法中检索get_object
对象。如果您的link_url
的{{1}}字段是主键,我们可以在类的顶部简单地添加FooterLink
。否则,您应该使用pk_url_kwarg = 'link_url'
和slug_field
。
slug_url_kwarg
注意:您现在应该在模板中使用class FooterLinksView(DetailView):
model = FooterLink
pk = 'link_url' # if link_url is the primary key
slug_field = 'link_url' # if link_url is not the primary key
slug_url_kwarg = 'string' # if link_url is not the primary key
template_name = 'pages/footerlinks.html'
...
变量(而不是object
)或覆盖另一个选项:obj
。
答案 3 :(得分:-1)
class FooterLinksView(TemplateView):
template_name = 'pages/footerlinks.html'
model = FooterLink
def get(self, request, args, *kwargs):
context = self.get_context_data(**kwargs)
string_name = self.kwargs['string']
obj = get_object_or_404(FooterLink, link_url=string_name)
if obj.link_type == 'page':
context['page_obj'] = obj
return render(request, self.template_name, context)
else:
a = obj.url_name
return HttpResponseRedirect(a)