Loading...
Navigation
Table of Contents

Django Douban Rating记录Code

bash命令

# 新建数据库
django-admin startproject project_name
# 新建app
python manage.py startapp app_name

# 更新了数据库
python manage.py makemigrations
python manage.py migrate

# 清除/重置数据库
# 删除APPNAME/migrations中除了__init__.py以外的文件
# 删除db.sqlite3
python manage.py flush

# 进入shell
python manage.py shell

# 启动服务
python manage.py runserver 8001
python manage.py runserver 0:8001 # 等价于0.0.0.0:8001

settings.py配置

ALLOWED_HOSTS = ['localhost', 'jiaxi.me']
INSTALLED_APPS = [
    'rating', # App name
    'corsheaders'  # 解决访问CORS问题
]
# 解决访问CORS问题
CORS_ORIGIN_WHITELIST = (
    'http://localhost:2368',
    'https://jiaxi.me'
)
CORS_ALLOW_CREDENTIALS = True # 解决访问CORS问题

# 静态文件配置
STATIC_URL = '/static/'  # 暴露在请求url中的虚假目录 https://jiaxi.me/static/...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'imgs'),  # 磁盘中的静态文件相对目录
)

数据库接口

假设app名字为rating,其中有MovieManage两个类

# file site_helper/rating/models.py
from django.db import models

class Movie(models.Model):
    uid = models.CharField(max_length=20)
    title = models.CharField(max_length=100)
    img = models.CharField(max_length=100)
    day = models.DateField()
    rating = models.IntegerField()
    comment = models.CharField(max_length=10000)
    url = models.CharField(max_length=100)
    intro = models.CharField(max_length=20)

    def __str__(self):
        return self.title


class User(models.Model):
    uid = models.CharField(max_length=20)
    update_time = models.DateTimeField()

    def __str__(self):
        return "{}:{}".format(self.uid, self.update_time)

update_or_create

from rating.models import Movie, User
from datetime import datetime, timezone, date, timedelta

#这里uid是query key,如果数据库中存在,会更新defaults中指定的值,否则创新一个新的uid
User.objects.update_or_create(
	uid="123456",
    defaults={"update_time": datetime.now(tz=timezone.utc)} #对应model.DateTimeField
)

#这里uid, title, day共同组成是query key,如果数据库中存在,会更新defaults中指定的值,否则创新一条新的数据
Movie.objects.update_or_create(
	uid="123",
	title="Interstellar",
	day=date(2019, 6, 1), # 对应model.DateField
	defaults={"comment": "Fantastic!", "rating": 5}
)

获取数据

#过滤方法1
Movie.objects.filter(uid="123", title="Interstellar").exists()
#过滤方法2
filters = {"uid": "123", "title": "Interstellar"}
Movie.objects.filter(**filters)
 
 #判断是否存在
 User.objects.get(uid="123")

Nginx转发

server {
	listen       80;
	server_name  localhost;
	location / {
    	proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:1234;
        proxy_redirect off;
    }

	location /helper/ {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_pass http://127.0.0.1:2345/;
    }
}

Last updated on Oct 16, 2019.