灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
当前回答
Instead of using render_to_response to bind your context to a template and render it (which is what the Django docs usually show) use the generic view direct_to_template. It does the same thing that render_to_response does but it also automatically adds RequestContext to the template context, implicitly allowing context processors to be used. You can do this manually using render_to_response, but why bother? It's just another step to remember and another LOC. Besides making use of context processors, having RequestContext in your template allows you to do things like:
<a href="{{MEDIA_URL}}images/frog.jpg">A frog</a>
这是非常有用的。事实上,+1在一般的视图上。对于简单的应用程序,Django文档大多将它们作为快捷方式显示,甚至没有views.py文件,但你也可以在自己的视图函数中使用它们:
from django.views.generic import simple
def article_detail(request, slug=None):
article = get_object_or_404(Article, slug=slug)
return simple.direct_to_template(request,
template="articles/article_detail.html",
extra_context={'article': article}
)
其他回答
django.views.generic.list_detail。object_list——它为分页提供了所有的逻辑和模板变量(这是我已经写了上千次的苦差事之一)。对它进行包装可以使用您需要的任何逻辑。这个gem为我节省了很多时间调试“搜索结果”页面中的一个错误,并使视图代码在这个过程中更加清晰。
使用isapi-wsgi和Django -pyodbc在Windows上使用IIS和SQL Server运行Django !
在Django 1.2+中使用.exists(),在以前的版本中使用.count(),而不是计算整个queryset来检查是否返回任何结果。
exists()和count()都清除order by子句,并从DB中检索单个整数。然而exists()将总是返回1,而as count可能会返回更高的值,对这些值将手动应用限制。exists()中使用的has_result和count()中使用的get_count的源代码。
因为它们都返回一个整数,所以没有模型实例化,在内存中加载模型属性,也没有大的TextFields在你的DB和应用程序之间传递。
如果你已经计算了查询,.count()计算len(cached_result), .exists()计算bool(cached_result)
效率不高—例1
books = Books.objects.filter(author__last_name='Brown')
if books:
# Do something
效率不高——例2
books = Books.objects.filter(author__last_name='Brown')
if len(books):
# Do something
高效-例1
books = Books.objects.filter(author__last_name='Brown')
if books.count():
# Do something
高效-例2
books = Books.objects.filter(author__last_name='Brown')
if books.exists():
# Do something
这是一种非常简单的方法,永远不必在python shell中再次导入另一个模型。
首先,安装IPython(如果您不使用IPython,那么您有什么问题?)接下来,在django项目目录中创建一个python脚本ipythonrc.py,其中包含以下代码:
from django.db.models.loading import get_models
for m in get_models():
globals()[m.__name__] = m
#NOTE: if you have two models with the same name you'll only end up with one of them
然后,在你的~/。ipython/ipythonrc文件,将以下代码放在“要加载和执行的Python文件”部分:
execfile /path/to/project/ipythonrc.py
现在,每次启动IPython或运行./manage.py shell时,您都将导入所有模型并准备使用。不需要再导入另一个模型。
您还可以将经常执行的任何其他代码放在ipythonrc.py文件中,以节省时间。
使用“apps”文件夹来组织应用程序,而不需要编辑PYTHONPATH
当我想这样组织我的文件夹时,这个方法就很方便了:
apps/
foo/
bar/
site/
settings.py
urls.py
不用重写PYTHONPATH,也不用在每次导入时都添加应用程序,比如:
from apps.foo.model import *
from apps.bar.forms import *
在你的settings.py中添加
import os
import sys
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
你已经准备好了:-)
我在http://codespatter.com/2009/04/10/how-to-add-locations-to-python-path-for-reusable-django-apps/上看到了这个