一、 使用 Nginx 为web应用绑定多个域名
如 绑定abc.com 和 cde.com:
server
{
listen 80; #监听端口设为 80。
server_name abc.com; #绑定您的域名。
index index.htm index.html index.php; #指定默认文件。
root /home/www/server110.com; #指定网站根目录。
include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}
server
{
listen 80; #监听端口设为 80。
server_name cde.com; #绑定您的域名。
index index.htm index.html index.php; #指定默认文件。
root /home/www/server110.com; #指定网站根目录。
include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}
二、 django处理Ajax跨域访问
-
添加中间件 django-cors-headers
(1)安装 pip install django-cors-headers
(2)在settings.py文件中INSTALLED_APPS = ( ... 'corsheaders', ...) 添加中间件 MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware',# 注意顺序 ... ] #跨域增加忽略 CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_WHITELIST = ( ) CORS_ALLOW_METHODS = ( 'DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'VIEW', ) CORS_ALLOW_HEADERS = ( 'XMLHttpRequest', 'X_FILENAME', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', 'Pragma', )
2、修改views.py中对应API的实现函数,允许其他域通过Ajax请求数据:
class Query(View):
@staticmethod
def get(request):
response = JsonResponse(todo_list, safe=False)
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
return response
@staticmethod
def post(request):
print(request.POST)
return HttpResponse()
---------------------
##### 三、 nginx 反向代理做域名转发简单配置
server
{
listen 80;
server_name abc.com;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.16.10.87:80;
}
access_log logs/abccom.log;
}
这样在python中:通过request.get_host(),就可以获取请求的真实域名
三、 django model使用相关
-
django获取字段列表(values/values_list/flat)
values(fields)函数的fields参数是限定输出结果的哪些列,如果不加参数,输出全部列。Blog.objects.values() <QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]> Blog.objects.values('id', 'name') <QuerySet [{'id': 1, 'name': 'Beatles Blog'}]>
values_list()函数的结果是元组,不带key
>>> Entry.objects.values_list('id', 'headline') <QuerySet [(1, 'First entry'), ...]> 甚至可以是去掉元组的数组 >>> Entry.objects.values_list('id').order_by('id') <QuerySet[(1,), (2,), (3,), ...]> >>> Entry.objects.values_list('id', flat=True).order_by('id') <QuerySet [1, 2, 3, ...]>
-
合并两个django的queryset
通过自带的方法a1 = User.objects.filter(id__gt=8) a2 = User.objects.filter(id__lt=4) a3 = a1 | a2 # 注:这种方式合并的结构还是一个queryset,相当于a3把a1和a2的条件合并了,推荐,因为可以用order_by等函数了 # 只能合并同种model对象的数据
合并不同的model对象数据,或者不是QuerySet集合,(通过get(),last()函数获取的单个对象)
a=[] a.extend(queryset1) a.extend(queryset2)
解决nginx+uwsgi部署Django的所有问题
Django上传文件
1、将Debug设置为False时访问不到图片和静态文件
setttings.py中加入
# 上传图片
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
在url.py中加入
from django.conf.urls.static import serve
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),