如何转移到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]))
现在,我想重写它们使用 direct_to_template 快捷方式的视图代码。
views.py:
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)?
我也尝试:
return direct_to_template(request, 'article.html', {'text': text}, 'car_list': car_list)
这也不起作用。谢谢。
I used this code and it worked.
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]))
Now, I want to rewrite the views code that they use direct_to_template shortcut.
views.py:
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})
Why not work? How I can transfer frequently used objects (from _custom_proc()_) to view(article()) and template (article.html)?
Also I try:
return direct_to_template(request, 'article.html', {'text': text}, 'car_list': car_list)
and this is not work too. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以看到两个选择。
如果您想将汽车列表添加到每个模板上下文中,请将您的处理器添加到
TEMPLATE_CONTEXT_PROCESSORS
设置中。请参阅文档更多信息。如果您只想将其添加为模板的子集,请使用
custom_proc
的结果,就像在第二个示例中使用text
一样。I can see two options.
If you want to add a list of cars to every template context, add your processor to the
TEMPLATE_CONTEXT_PROCESSORS
setting. See the documentation for more info.If you just want to add it a subset of your templates, use the result of
custom_proc
as you are usingtext
in your second example.这两者怎么可能起作用呢?您没有在任何地方调用
custom_proc
。如果您想要模板中函数的结果,则需要调用它并将结果包含在上下文字典中。How could either of these work? You're not calling
custom_proc
anywhere. If you want the result of a function in the template, you need to call it and include the result in the context dictionary.