Django_杂
1、url带一些GET参数"url?a=1&b=2",通过QueryDict以及其urlencode()进行处理
from django.http.request import QueryDict param_dict = QueryDict(mutable=True) if request.GET: param_dict["_changelistfilter"] = request.GET.urlencode() base_add_url = reverse("{2}:{0}_{1}_add".format(self.app_label,self.model_name,self.site.namespace)) add_url = "{0}?{1}".format(base_add_url,param_dict.urlencode())
QueryDict类型 page_param_dict = copy.deepcopy(request.GET) page_param_dict._mutable = True page_param_dict["k"] = "v"
2、在html引入static里的文件,不用写死
STATIC_URL = '/static/'
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'yg/plugins/bootstrap-3.3.7-dist/css/bootstrap-theme.css' %}" > </head>
3、获取request源ip
有些网站服务器会使用ngix等代理http,或者是该网站做了负载均衡,导致使用remote_addr抓取到的是1270.0.1,这时使用HTTP_X_FORWARDED_FOR才获得是用户的真实IP。 if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR']
4 django回退migrations
# 查看migrations python manage.py showmigrations ''' dms_srv [X] 0001_initial [X] 0002_event [X] 0003_auto_20191023_0622 [X] 0004_auto_20191023_0636 [X] 0005_event_status [X] 0006_deviceinfo_remark ''' # 回退到005,使用fake欺骗django,此时在migrations文件夹是能看见0006的 python manage.py migrate dms_srv 0005_event_status --fake ''' dms_srv [X] 0001_initial [X] 0002_event [X] 0003_auto_20191023_0622 [X] 0004_auto_20191023_0636 [X] 0005_event_status [] 0006_deviceinfo_remark ''' # 重新migrate python manage.py migrate dms_srv 完成