django学习001

create backend account

python manage.py createsuperuser

Username (leave blank to use ‘sh’): <admin登陆账号>
Email address:  <你的邮箱地址>
Password: <密码>
Password (again): <确认密码>

html 中引用js等静态资源

settings.py 文件尾部添加

STATIC_URL = '/static/'
HERE = os.path.dirname(os.path.abspath(__file__))
HERE = os.path.join(HERE, '../')
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(HERE, 'static/'),
)

html中引用

<script src="../static/js/main.js"></script>

RESTful api service

pip install djangorestframework

Declare it in INSTALED_APPS on settings.py

INSTALLED_APPS = [
#...
'rest_framework'
]

Install django-rest-framework-mongoengine Dependencies

pip install mongoengine
pip install django-rest-framework-mongoengine

使用 mongodb

安装 djongo 来操作
pip install djongo

settings.py 里面修改如下。

DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'mongoTest',
'HOST':'mongodb://admin:admin@localhost/mongoTest',
'USER':'admin',
'PASSWORD':'admin'

}
}

// 这里必须,否则会出错
import mongoengine
mongoengine.connect('mongoTest', host='localhost:27017')

建完库之后,更新一下表结构

python manage.py migrate
python manage.py createsuperuser