Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何转移到直接\u到\u模板?_Django_Django Views - Fatal编程技术网

Django 如何转移到直接\u到\u模板?

Django 如何转移到直接\u到\u模板?,django,django-views,Django,Django Views,我使用了这个代码,它工作了 views.py: from models import Car from django.shortcuts import render_to_response, get_object_or_404 from django.template.context import RequestContext def custom_proc(request): car_list = Car.objects.all()[0:5] return {'car_lis

我使用了这个代码,它工作了

views.py:

from models import Car
from django.shortcuts import render_to_response, get_object_or_404
from django.template.context import RequestContext

def custom_proc(request):
    car_list = Car.objects.all()[0:5]
    return {'car_list': car_list}

def article(request, slug):
    text = get_object_or_404(Article, slug=slug)
    return render_to_response('article.html', {'text': text},
     context_instance=RequestContext(request, processors=[custom_proc]))
from models import Car
from django.shortcuts import get_object_or_404
from django.template.context import RequestContext
from django.views.generic.simple import direct_to_template

def custom_proc(request):
    car_list = Car.objects.all()[0:5]
    return {'car_list': car_list}

def article(request, slug):
    text = get_object_or_404(Article, slug=slug)
    return direct_to_template(request, 'article.html', {'text': text})
现在,我想重写视图代码,它们使用direct-to-template快捷方式

views.py:

from models import Car
from django.shortcuts import render_to_response, get_object_or_404
from django.template.context import RequestContext

def custom_proc(request):
    car_list = Car.objects.all()[0:5]
    return {'car_list': car_list}

def article(request, slug):
    text = get_object_or_404(Article, slug=slug)
    return render_to_response('article.html', {'text': text},
     context_instance=RequestContext(request, processors=[custom_proc]))
from models import Car
from django.shortcuts import get_object_or_404
from django.template.context import RequestContext
from django.views.generic.simple import direct_to_template

def custom_proc(request):
    car_list = Car.objects.all()[0:5]
    return {'car_list': car_list}

def article(request, slug):
    text = get_object_or_404(Article, slug=slug)
    return direct_to_template(request, 'article.html', {'text': text})
为什么不工作?如何将经常使用的对象(从_custom_proc()_)传输到视图(article())和模板(article.html)

我还尝试:

直接返回到模板(请求'article.html',{'text':text},'car\u list':car\u list)


这也不是工作。谢谢。

这两种方法怎么行?您没有在任何地方调用
custom\u proc
。如果您想在模板中获得函数的结果,则需要调用它并将结果包含在上下文词典中。

这两种方法如何工作?您没有在任何地方调用
custom\u proc
。如果希望在模板中获得函数的结果,则需要调用它并将结果包含在上下文词典中。

我可以看到两个选项

如果要将车辆列表添加到每个模板上下文中,请将处理器添加到
template\u context\u PROCESSORS
设置中。有关更多信息,请参阅

如果您只想将其添加到模板的子集,请在第二个示例中使用
文本时使用
自定义过程的结果。

我可以看到两个选项

如果要将车辆列表添加到每个模板上下文中,请将处理器添加到
template\u context\u PROCESSORS
设置中。有关更多信息,请参阅


如果您只想将其添加到模板的子集,请在第二个示例中使用
文本时使用
自定义过程的结果。

它是如何“不起作用”的?你有模板错误吗?例外情况?它如何“不起作用”?你有模板错误吗?异常?他在RequestContext中使用custom_proc作为上下文处理器。他在RequestContext中使用custom_proc作为上下文处理器。第一个选项是我需要的。谢谢。第一种选择是我需要的。非常感谢。