灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
灵感来自问题系列的隐藏特征…,我很想听听你最喜欢的Django技巧或你知道的不太为人所知但有用的功能。
请在每个答案中只包含一个技巧。 添加Django版本要求(如果有的话)。
当前回答
使用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在他的博客上有一个相当完整的教程
其他回答
我喜欢使用Python调试器pdb来调试Django项目。
这是一个学习如何使用它的有用链接:http://www.ferg.org/papers/debugging_in_python.html
当你开始设计你的网站时,网页设计应用程序是非常有用的。一旦导入,你可以添加它来生成示例文本:
{% load webdesign %}
{% lorem 5 p %}
如果你还没读过《Unbreaking Django》。它包含了很多关于django陷阱的有用信息。
上下文处理器非常棒。
假设你有一个不同的用户模型,你想包括 在每个回应中。不要这样做:
def myview(request, arg, arg2=None, template='my/template.html'):
''' My view... '''
response = dict()
myuser = MyUser.objects.get(user=request.user)
response['my_user'] = myuser
...
return render_to_response(template,
response,
context_instance=RequestContext(request))
上下文过程使您能够将任何变量传递给您的 模板。我通常把我的放在'my_project/apps/core/context.py中:
def my_context(request):
try:
return dict(my_user=MyUser.objects.get(user=request.user))
except ObjectNotFound:
return dict(my_user='')
在settings.py中,将以下行添加到TEMPLATE_CONTEXT_PROCESSORS中
TEMPLATE_CONTEXT_PROCESSORS = (
'my_project.apps.core.context.my_context',
...
)
现在每次请求都会自动包含my_user键。
这也是胜利的信号。
几个月前我写了一篇关于这方面的博客文章,所以我只是要剪切和粘贴:
开箱即用的Django提供了几个信号 令人难以置信的有用。你有能力提前做好事情 发布保存,初始化,删除,甚至当请求正在进行时 处理。我们先不讲概念 演示如何使用这些。假设我们有一个博客
from django.utils.translation import ugettext_lazy as _
class Post(models.Model):
title = models.CharField(_('title'), max_length=255)
body = models.TextField(_('body'))
created = models.DateTimeField(auto_now_add=True)
所以你想要通知众多博客中的一个 服务我们已经做了一个新的帖子,重建最近 帖子缓存,并tweet关于它。你有信号 无需添加任何内容即可完成所有这些操作的能力 方法添加到Post类。
import twitter
from django.core.cache import cache
from django.db.models.signals import post_save
from django.conf import settings
def posted_blog(sender, created=None, instance=None, **kwargs):
''' Listens for a blog post to save and alerts some services. '''
if (created and instance is not None):
tweet = 'New blog post! %s' instance.title
t = twitter.PostUpdate(settings.TWITTER_USER,
settings.TWITTER_PASSWD,
tweet)
cache.set(instance.cache_key, instance, 60*5)
# send pingbacks
# ...
# whatever else
else:
cache.delete(instance.cache_key)
post_save.connect(posted_blog, sender=Post)
好了,通过定义这个函数并使用 post_init信号,将函数连接到Post模型 并在保存后执行它。
Virtualenv + Python =救生圈,如果你在多个Django项目中工作,而且它们可能都不依赖于同一个版本的Django/应用程序。