从模板django中的请求中获取数据
Get data from request in template django
我想知道从请求会话中获取数据是个坏主意还是将数据解析为字典上下文并呈现它更好(每个视图都需要这样做)?
通常的做法是通过视图中的上下文发送您需要的内容。
我觉得它可以让您对正在做的事情多一点 security/certainty 因为您可以将您的逻辑保持在它应该在的视图中,而不是在模板中进行任何检查以了解其中的内容请求。
编辑
仅当您很少做某事时,以上内容才适用。如果您定期将请求的元素添加到您的模板中,您确实应该像其他人建议的那样编写上下文处理器以使您需要的内容可用于所有视图。
看看文档; TEMPLATE_CONTEXT_PROCESSORS
也请阅读 django 书中的这一章,因为它会很有帮助; Chapter 9: Advanced Templates
具体这一段;
Guidelines for Writing Your Own Context Processors
Here are a few tips for rolling your own:
Make each context processor responsible for the smallest subset of functionality possible. It’s easy to use multiple processors, so you might as well split functionality into logical pieces for future reuse.
Keep in mind that any context processor in TEMPLATE_CONTEXT_PROCESSORS will be available in every template powered by that settings file, so try to pick variable names that are unlikely to conflict with variable names your templates might be using independently. As variable names are case-sensitive, it’s not a bad idea to use all caps for variables that a processor provides.
It doesn’t matter where on the filesystem they live, as long as they’re on your Python path so you can point to them from the TEMPLATE_CONTEXT_PROCESSORS setting. With that said, the convention is to save them in a file called context_processors.py within your app or project.
Django 为您提供了一种将数据放入每个模板的方法,它称为上下文处理器。
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
https://docs.djangoproject.com/en/1.7/ref/templates/api/
如果您经常访问模板中的请求对象(就像我对 URL 获取参数处理所做的那样),您可以将此添加到您的 TEMPLATE_CONTEXT_PROCESSORS。
"django.core.context_processors.request",
我想知道从请求会话中获取数据是个坏主意还是将数据解析为字典上下文并呈现它更好(每个视图都需要这样做)?
通常的做法是通过视图中的上下文发送您需要的内容。
我觉得它可以让您对正在做的事情多一点 security/certainty 因为您可以将您的逻辑保持在它应该在的视图中,而不是在模板中进行任何检查以了解其中的内容请求。
编辑
仅当您很少做某事时,以上内容才适用。如果您定期将请求的元素添加到您的模板中,您确实应该像其他人建议的那样编写上下文处理器以使您需要的内容可用于所有视图。 看看文档; TEMPLATE_CONTEXT_PROCESSORS
也请阅读 django 书中的这一章,因为它会很有帮助; Chapter 9: Advanced Templates
具体这一段;
Guidelines for Writing Your Own Context Processors
Here are a few tips for rolling your own:
Make each context processor responsible for the smallest subset of functionality possible. It’s easy to use multiple processors, so you might as well split functionality into logical pieces for future reuse.
Keep in mind that any context processor in TEMPLATE_CONTEXT_PROCESSORS will be available in every template powered by that settings file, so try to pick variable names that are unlikely to conflict with variable names your templates might be using independently. As variable names are case-sensitive, it’s not a bad idea to use all caps for variables that a processor provides.
It doesn’t matter where on the filesystem they live, as long as they’re on your Python path so you can point to them from the TEMPLATE_CONTEXT_PROCESSORS setting. With that said, the convention is to save them in a file called context_processors.py within your app or project.
Django 为您提供了一种将数据放入每个模板的方法,它称为上下文处理器。
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/ https://docs.djangoproject.com/en/1.7/ref/templates/api/
如果您经常访问模板中的请求对象(就像我对 URL 获取参数处理所做的那样),您可以将此添加到您的 TEMPLATE_CONTEXT_PROCESSORS。
"django.core.context_processors.request",