灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
当前回答
PyCharm和Wingware IDE是很好的工具,如果你有钱支付许可证。
因为我是一个很差的开发人员,所以我在Eclipse中使用PyDev。
其他回答
django-admin文档:
如果你使用Bash shell,可以考虑安装Django Bash完成脚本,它存在于Django发行版中的extras/django_bash_completion中。它支持django-admin.py和manage.py命令的制表符补全,所以你可以,例如…
django-admin.py类型。 按[TAB]查看所有可用选项。 输入sql,然后输入[TAB],查看所有名称以sql开头的可用选项。
来自https://github.com/django-extensions/django-extensions的Django_extensions非常棒。
一些漂亮的。/manage.py命令:
shell_plus -自动导入所有INSTALLED_APPS中的模型 Show_urls -打印项目中所有应用程序中定义的所有url runscript -在项目环境中运行任何脚本(你可以使用模型和其他django相关模块)
在视图代码中添加assert False以转储调试信息。
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}
)
使用isapi-wsgi和Django -pyodbc在Windows上使用IIS和SQL Server运行Django !