[django]线上部署后,访问 admin 时出现“DoesNotExist at /admin/ Site matching query does not exist.”错误

将 django 项目部署到服务器后,访问 admin 时出现如下错误:

DoesNotExist at /admin/
Site matching query does not exist.

上网搜索后,参考 http://stackoverflow.com/questions/9736975/django-admin-doesnotexist-at-admin 修复了该问题,下面记录一下解决过程。

两种解决办法:

  • 第一种办法是:编辑 settings.py 文件,从 INSTALLED_APPS 配置项中移除 'django.contrib.sites',

  • 第二种办法:通过 python manage.py shell 为 Site model 添加一条记录(将网站的域名添加进去):

    $ python manage ...
more ...

[django] 创建后台超级用户时出现 TypeError: decode() argument 1 must be string, not None 错误

错误信息如下:

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django ...
more ...


[django]官方编码规范

请以最新的官方英文版为准:https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/

编码风格

当编写 Django 代码时,请遵循下列编码规范。

Python 风格

  • 除非另有规定,否则请遵循 PEP 8 风格指南。
    你可以使用类似 pep8 的工具检查你的代码, 但是,请记住 PEP 8 只是个指南,并非强制约束,因此应当以你所处项目/团队的风格为主。
    一个比较大的例外就是,PEP 8 规定代码行长最长不能超过 79 个字符。问题是现在是 21 世纪了,我们有高分辨率的显示器可以显示超过 79 个字符的代码行。如果这这个约定让代码看起来很丑或难以阅读的话,那么就没必要限制行长不超过 79 个字符 ...

more ...

[django]使用 apache + mod_wsgi 部署 django

本文测试环境:linux mint,python 2.7,django 1.4

安装依赖

$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi

设置 django

本例中项目名为 bbs:

$ pwd
/var/www/bbs

$ tree . -d
.
|-- bb  # 应用
|-- bbs  # settings.py wsgi.py
|-- static  # 静态文件
|   `-- css
`-- templates  # TEMPLATE_DIRS

配置 settings:

TEMPLATE_DIRS = (
    '/var/www/bbs/templates',  # 这里要是绝对路径
)

STATIC_ROOT = '/var ...
more ...