# 0. 修改port ???? 很简单。 runserver + 8888
# 1. use postgresql as backends
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'pythonxxxx', # db name
'USER': '00000',
'PASSWORD': '000000',
'HOST': '127.0.0.1',
'PORT': '5432',
},
}
# 2. some models 的写法:
def get_absolute_url(self):
return reverse('readbook:book_detail', args=[self.id])
def increase_views(self):
self.total_views += 1
self.save(update_fields=['total_views'])
# 3. 系统变量
import os
# print(os.environ) #查看所有的系统变量
# print(os.getenv('path'))
# 4. .gitignore的写法:
'''
__pycache__/
*.py[cod]
.env
venv/
# SQLite database files
*.sqlite3
'''
# 5.启动 supervisor
# sudo systemctl enable supervisor
# sudo systemctl start supervisor
# 6.linux 下安装python 虚拟环境
# sudo apt-get install python3-venv
# 创建虚拟环境和windows是一样的
# 启动虚拟环境:
# sudo source my_venv/bin/activate
# 7. postgres url:
# postgres://db_user:db_password@db_host:db_port/db_name.
# postgre://mm:lovenba123@localhost:5432/testpython
# 8. message 的正确用法
# views.py
from django.contrib import messages
def some_view(request):
messages.add_message(request, messages.INFO, 'Hello!'
messages.info(request, 'Hello!')
<!-- base.html -->
...
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
9. 这里使用数据库处理的也很好啊:
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
if os.getenv('DJANGO_ENV') == 'prod':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'rezepte',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
备用设置:for 'djrichtextfield',: app-name
DJRICHTEXTFIELD_CONFIG = {
'js': ['//cdn.ckeditor.com/4.11.2/standard/ckeditor.js'],
'init_template': 'djrichtextfield/init/ckeditor.js',
'settings': {
'toolbar': [
{'items': ['Format', '-', 'Bold', 'Italic', '-',
'RemoveFormat']},
{'items': ['Link', 'Unlink', 'Image', 'Table']},
{'items': ['Source']}
],
'format_tags': 'p;h1;h2;h3',
'width': 700
}
}
网友评论