灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
当前回答
django.db.models。Get_model允许您在不导入模型的情况下检索模型。
James向我们展示了它有多方便:“Django提示:编写更好的模板标记-迭代4”。
其他回答
我就从我自己的一个建议开始吧:)
在settings.py中使用os.path.dirname()来避免硬编码的dirname。
如果你想在不同的位置运行你的项目,不要在你的settings.py中硬编码路径。如果你的模板和静态文件位于Django项目目录中,在settings.py中使用下面的代码:
# settings.py
import os
PROJECT_DIR = os.path.dirname(__file__)
...
STATIC_DOC_ROOT = os.path.join(PROJECT_DIR, "static")
...
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, "templates"),
)
工作人员:我从视频《Django from the Ground Up》中得到了这个提示。
如果您对模型进行更改
./manage.py dumpdata appname > appname_data.json
./manage.py reset appname
django-admin.py loaddata appname_data.json
dir()和提高评估误差()
为了在开发过程中调试/探索事物的状态,我使用了以下技巧:
...
to_see = dir(inspect_this_thing)
to_see2 = inspect_this_thing.some_attribute
raise ValueError("Debugging")
...
当你处理django中没有很好文档的部分时,这是特别有用的。changed_fields是我最近使用的一个)。
当地人()。
使用python内置的locals()命令为你创建一个字典,而不是为模板上下文写出每个变量:
#This is tedious and not very DRY
return render_to_response('template.html', {"var1": var1, "var2":var2}, context_instance=RequestContext(request))
#95% of the time this works perfectly
return render_to_response('template.html', locals(), context_instance=RequestContext(request))
#The other 4.99%
render_dict = locals()
render_dict['also_needs'] = "this value"
return render_to_response('template.html', render_dict, context_instance=RequestContext(request))
不要在本地主机上运行Django开发服务器,而是在一个合适的网络接口上运行。例如:
python manage.py runserver 192.168.1.110:8000
or
python manage.py runserver 0.0.0.0:8000
然后,您不仅可以轻松地使用Fiddler (http://www.fiddler2.com/fiddler2/)或其他工具,如HTTP调试器(http://www.httpdebugger.com/)来检查您的HTTP头,而且还可以从LAN上的其他机器访问您的开发站点进行测试。
确保你有防火墙保护,尽管开发服务器是最小的,而且相对安全。
使用djangorecipe管理你的项目
如果你正在编写一个新的应用程序,这个方法可以让你在项目之外非常容易地测试它 它允许你管理项目的依赖关系(例如,它应该依赖于哪个版本的应用程序)
你要做的就是这样开始:
Create a folder for your new website (or library) Create a buildout.cfg with following content in it: [buildout] parts=django [django] recipe=djangorecipe version=1.1.1 project=my_new_site settings=development Grab a bootstrap.py to get a local installation of buildout and place it within your directory. You can either go with the official one (sorry, Markdown didn't like part of the full link :-/ ) or with one that uses distribute instead of setuptools as described by Reinout van Rees. python bootstrap.py (or python bootstrap_dev.py if you want to use distribute). ./bin/buildout
就是这样。现在你应该有一个新的文件夹“my_new_site”,这是你新的django 1.1.1项目,在。/bin中你会找到django-script,它取代了正常安装的manage.py。
有什么好处?假设你想在你的项目中使用django-comment-spamfighter之类的东西。你所要做的就是把build - out.cfg修改成这样:
[buildout]
parts=django
[django]
recipe=djangorecipe
version=1.1.1
project=my_new_site
settings=development
eggs=
django-comments-spamfighter==0.4
请注意,我所做的只是添加了最后两行,表示django部分在0.4版中也应该有django-comments-spamfighter包。下次运行。/bin/buildout时,buildout将下载该包并修改。bin/django,将其添加到其PYTHONPATH中。
Djangorecipe也适用于用mod_wsgi部署你的项目。只需将wsgi=true设置添加到build - out.cfg中的django-part和“django. cfg”。Wsgi "将出现在你的。/bin文件夹中:-)
如果您将测试选项设置为应用程序列表,djangorecipe将为您创建一个漂亮的包装器,为项目中列出的应用程序运行所有测试。
如果你想在一个独立的环境中开发一个单独的应用程序进行调试等,Jakob Kaplan-Moss在他的博客上有一个相当完整的教程