文章关键字 ‘django’

[python]django里flatpages其实应该这样安装

2007年11月13日,星期二

Django官方文档关于flatpages上写到:

Installation
To install the flatpages app, follow these steps:

Add 'django.contrib.flatpages' to your INSTALLED_APPS setting.
Add 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' to your MIDDLEWARE_CLASSES setting.
Run the command manage.py syncdb.

这里官方文档写得不完整,其实应该有5步:

  1. 在settings.py里添加'django.contrib.flatpages' 到 INSTALLED_APPS
  2. 把 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' 添加到 MIDDLEWARE_CLASSES
  3. 找到当前调试用的域名,修改settings.py里SITE_ID的值
  4. 在urls.conf最后添加上 (r'', include('django.contrib.flatpages.urls')),
  5. 运行 manage.py syncdb

缺少了任何一步,flatepages都不会成功。

原来lighttpd 1.4.x不支持Expect: 100-continue

2007年10月7日,星期日

今天跟同学写一个WebService,我这边是用lighttpd+mod_fastcgi+django做的一个WebService服务,同学那边用C#来调用这个WebService。但是每次在Visual Studio 2005里调试的时候都显示

未处理 WebException
远程服务器返回错误: (417) Expectation Failed。

我用Python的urllib2来POST这个WebService都是成功的,但是.NET里几个HTTP请求对象都不能调用。仔细对比抓包了2种语言的请求包,发现.NET的请求头多了一个Expect: 100-continue

起初我还怀疑是django不支持,后来查看了HTTP 1.1的文档,最后发现,lighttpd 1.4版本更本不会支持Expect: 100-continue这个HTTP头。或者装 lighttpd 1.5.x 的开发版本,或者换apache。

结合在CSDN上的相关问题,最终的解决办法是:在Http请求之前加上这一句:System.Net.ServicePointManager.Expect100Continue = false;

终于折腾出来了djangoproject.com源码里update_feeds.py的玩法

2007年10月6日,星期六

这几天一直在玩弄 djangoproject.com 的源码,对里边的那个RSS Aggregator很感兴趣,一直想知道它的实现原理,结果是crontab每小时执行一次 @.@ 在Windows下要跑起来似乎对于我这种菜鸟有点困难,折腾了几天终于摸清楚了这个 update_feeds.py 的玩法。

简介

  • Django是一个免费的开源Python Web框架
  • djangoproject.com 是Django的官方网站,网站源码可以在 http://code.djangoproject.com/svn/djangoproject.com/下载到。
  • djangoproject.com源码里有一个Community栏目,是一个RSS Aggregator,可以同步多个RSS源,每小时更新一次。
  • djangoproject.com用的是PostgreSQL,我把它修改成了Python 2.5默认支持的SQLite3,数据库放在E:\Python\reference\djangoprojectwebsite\django_website\djangoproject
  • 我把SVN Checkout的源码放在 E:\Python\reference\djangoprojectwebsite,那个自动更新RSS聚合的python在 E:\Python\reference\djangoprojectwebsite\django_website\apps\aggregator\bin\update_feeds.py

No module named django_website.apps.aggregator.models

起初没弄成功,出现的错误提示是:

Traceback (most recent call last):
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in 
    update_feeds()
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 13, in update_feeds
    From django_website.apps.aggregator.models import Feed, FeedItem
ImportError: No module named django_website.apps.aggregator.models

这个错误的原因是没有在系统环境变量里定义 PYTHONPATH 指向当前工程的路径。

Environment variable DJANGO_SETTINGS_MODULE is undefined.

Traceback (most recent call last):
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in 
    update_feeds()
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 13, in update_feeds
    From django_website.apps.aggregator.models import Feed, FeedItem
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\..\\django_websit
e\\apps\\aggregator\\models.py", line 1, in 
    From django.db import models
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\__init__.py", line 7, in 
    if not settings.DATABASE_ENGINE:
  File "C:\\Python25\\Lib\\site-packages\\django\\conf\\__init__.py", line 28, in __ge
tattr__
    self._import_settings()
  File "C:\\Python25\\Lib\\site-packages\\django\\conf\\__init__.py", line 53, in _imp
ort_settings
    raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMEN
T_VARIABLE
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

这里需要定义 DJANGO_SETTINGS_MODULE 系统环境变量,也可以通过 update_feeds.py --settings=django_website.settings 来指定。

no such table: aggregator_feeds

Traceback (most recent call last):
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in 
    update_feeds()
  File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 14, in update_feeds
    for feed in Feed.objects.filter(is_defunct=False):
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 108, in _
_iter__
    return iter(self._get_data())
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 470, in _
get_data
    self._result_cache = list(self.iterator())
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 183, in i
terator
    cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join
(select) + sql, params)
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\backends\\util.py", line 12, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\\Python25\\Lib\\site-packages\\django\\db\\backends\\sqlite3\\base.py", line
93, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: aggregator_feeds

这个错误困扰我时间最长。最后发现是当前路径的问题:不能直接到 bin 目录去执行 update_feeds.py,需要到数据库所在的路径,通过相对路径来执行 update_feeds.py 才能成功执行。

正确执行 update_feeds.py 的步骤

  1. 首先在cmd里用cd命令切换到django_website目录,也就是djangoproject.com的源码跟目录
    E:\>cd /d E:\Python\reference\djangoprojectwebsite\django_website
  2. 然后设置 PYTHONPATH 和 DJANGO_SETTINGS_MODULE 环境变量
    E:\Python\reference\djangoprojectwebsite\django_website>set PYTHONPATH=E:\Python\reference\djangoprojectwebsite
    E:\Python\reference\djangoprojectwebsite\django_website>set DJANGO_SETTINGS_MODULE=django_website.settings
  3. 最后执行 update_feeds.py,注意用相对路径启动 update_feeds.py
    E:\Python\reference\djangoprojectwebsite\django_website>apps\aggregator\bin\update_feeds.py

终于开始更新了,嘎嘎

[django]AttributeError: 'str' object has no attribute '_meta'

2007年09月20日,星期四

A very common error among django learner is like this:
From django.db import models
From django.contrib.auth.models import User

# Create your models here.

class DLUser(models.Model):
xxxx = models.xxxxField()
user = models.OneToOneField('CustomUser', User)
...

When execute python manage.py validate you'll get
Traceback (most recent call last):
File "manage.py", line 13, in
execute_manager(settings)
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1672, in
execute_manager
execute_from_command_line(action_mapping, argv)
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1569, in
execute_from_command_line
action_mapping[action]()
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1152, in
validate
num_errors = get_validation_errors(outfile)
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1018, in
get_validation_errors
for r in rel_opts.get_all_related_objects():
File "C:\Python25\Lib\site-packages\django\db\models\options.py", line 125, in
get_all_related_objects
if f.rel and self == f.rel.to._meta:
AttributeError: 'str' object has no attribute '_meta'

The error message is misleading. Infact you just made a mistake here

user = models.OneToOneField('CustomUser', User)

Correct that to

user = models.OneToOneField(User, 'CustomUser')

Or, according to official defination of verbose-field-names, ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class, so use the verbose_name keyword argument:

user = models.OneToOneField(User, verbose_name='CustomUser')

And the problem is solved.