【上海校区】Django使用的注意事项 财富值98

2021-04-08 16:54发布

1.如何搭建虚拟环境? 安装虚拟环境的命令 :
  sudo pip install virtualenv
  sudo pip install virtualenvwrapper

2.安装完虚拟环境后,如果提示找不到mkvirtualenv命令,须配置环境变量:
  2.1 创建目录用来存放虚拟环境
   mkdir $HOME/.virtualenvs

  2.2 打开~/.bashrc文件,并添加如下:
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
   
3.运行
    source ~/.bashrc

4.创建虚拟环境的常用两种方式;
    4.1  mkvirtualenv venv_name -p python3
    4.2. mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv_name

5.djnango自动生成urls文件配置:
   5.1 在所用的虚拟环境中,例如django_py3/lib/python3.5/site-packages/django/conf下,将project_template/project_name/urls.py-tpl复制到app_template下即可

HTTP协议向服务器传参有4种途径:
1.URL路径参数
[Python] 纯文本查看 复制代码
# http://127.0.0.1:8000/users/index/2018/beijing  def index(request,year,city):      print("city={1},year={0}".format(year,city))      return HttpResponse("ok")


2.查询字符串(query string),形如key1=value1&key2=value2:
[Python] 纯文本查看 复制代码
# /qs/?a=1&b=2&a=3  def qs(request):      a = request.GET.get("a")      b = request.GET.get("b")      alist = request.GET.getlist("a")      print(a)  # 3      print(b)  # 2      print(alist)  # ["1", "3"]      return HttpResponse("OK")  

3.请求体传参:
[Python] 纯文本查看 复制代码
def get_body(request):      a = request.POST.get("a")      b = request.POST.get("b")      alist = request.POST.getlist("a")      print(a)      print(b)      print(alist)      return HttpResponse("OK")


4.请求头
[Python] 纯文本查看 复制代码
def get_headers(request):      print(request.META["CONTENT_TYPE"])      return HttpResponse("OK")