Django开发BUG汇总

使用版本知悉

limengjiedeMacBook-Pro:~ limengjie$ python --version

Python 3.6.4 :: Anaconda, Inc.

limengjiedeMacBook-Pro:~ limengjie$ python -m django --version

1.11.13 

工作需求是1.11.13(安装时,pip版本较低,2.0以下版本安装会超时,2.0以上版本安装正常)

当前系统环境的端口情况查看:

# windows
# 查看8000端口
netstat -aon | findstr "8000"

# 假若此时8000端口被进程号为15312的进程占用,继续执行下面命令查看对应的进程:
tasklist | findstr "15312"

# 假若查看结果,是被test.py给占用了,如果不想这个程序继续占用,可以结束该进程:
taskkill /f /t /im test.py
或者直接根据进程号PID杀死该进程
taskkill /f /PID 15312


#mac
# 查看端口:
sudo lsof -i:8000

# 查看结果:
COMMAND PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
python  934 limengjie    4u  IPv4 0xf90e8048abbcccff      0t0  TCP localhost:irdmi (LISTEN)

# 根据进程PID杀进程
sudo kill -9 934
查看系统端口情况

目录

  1. mac环境下,安装Django 2.0 以下版本报错
  2. 列表存进MySQL数据库后再读取时需转换类型
  3. 上传与下载
  4. Django调试工具:debug_toolbar

 

 

 

 

 

 

 

 

一、mac环境下,安装Django 2.0以下版本报错

使用pycharm安装异常截图

使用命令行安装,异常如下

提示安装超时,并建议更新pip版本号

Last login: Thu Jun 14 16:59:22 on ttys000
limengjiedeMacBook-Pro:~ limengjie$ pip install Django==1.11.13
Collecting Django==1.11.13
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x10ae0ebe0>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/25/4d/c8228419346a0e84aec202a43e181afc6572b861d38f8a0306dbce6abef0/Django-1.11.13-py2.py3-none-any.whl
  Downloading https://files.pythonhosted.org/packages/25/4d/c8228419346a0e84aec202a43e181afc6572b861d38f8a0306dbce6abef0/Django-1.11.13-py2.py3-none-any.whl (6.9MB)
    14% |████▊                           | 1.0MB 8.9kB/s eta 0:11:03Exception:
Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 232, in _error_catcher
    yield
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 314, in read
    data = self._fp.read(amt)
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 60, in read
    data = self.__fp.read(amt)
  File "/anaconda3/lib/python3.6/http/client.py", line 449, in read
    n = self.readinto(b)
  File "/anaconda3/lib/python3.6/http/client.py", line 493, in readinto
    n = self.fp.readinto(b)
  File "/anaconda3/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/anaconda3/lib/python3.6/ssl.py", line 1009, in recv_into
    return self.read(nbytes, buffer)
  File "/anaconda3/lib/python3.6/ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
  File "/anaconda3/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/anaconda3/lib/python3.6/site-packages/pip/commands/install.py", line 335, in run
    wb.build(autobuilding=True)
  File "/anaconda3/lib/python3.6/site-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/anaconda3/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/anaconda3/lib/python3.6/site-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 882, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 603, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/anaconda3/lib/python3.6/site-packages/pip/utils/hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 571, in written_chunks
    for chunk in chunks:
  File "/anaconda3/lib/python3.6/site-packages/pip/utils/ui.py", line 139, in iter
    for x in it:
  File "/anaconda3/lib/python3.6/site-packages/pip/download.py", line 560, in resp_read
    decode_content=False):
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 357, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 324, in read
    flush_decoder = True
  File "/anaconda3/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/anaconda3/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 237, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pip install django==1.11.13 报错源码

解决方案:

You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

按照提示,更新pip版本后,安装正常

limengjiedeMacBook-Pro:~ limengjie$ pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 23kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-10.0.1
limengjiedeMacBook-Pro:~ limengjie$ pip install Django==1.11.13
Collecting Django==1.11.13
  Downloading https://files.pythonhosted.org/packages/25/4d/c8228419346a0e84aec202a43e181afc6572b861d38f8a0306dbce6abef0/Django-1.11.13-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 30kB/s 
Requirement already satisfied: pytz in /anaconda3/lib/python3.6/site-packages (from Django==1.11.13) (2017.3)
Installing collected packages: Django
  Found existing installation: Django 2.0.6
    Uninstalling Django-2.0.6:
      Successfully uninstalled Django-2.0.6
Successfully installed Django-1.11.13

安装成功验证:

方法一:

>>> import django
>>> print django.VERSION # Python2的格式  
>>> print(django.VERSION) # python3版本
(1, 11, 13, 'final', 0)

方法二:(也是通过查看版本号,python -m django --version)

limengjiedeMacBook-Pro:~ limengjie$ python -m django --version
1.11.13

 


 

二、列表存进MySQL数据库后再读取需重新转换为列表

问题截图

此处功能部分源码:

关键是使用eval()函数实现str类型和list,tuple或dict之间的转换

class UploadFile(View):

    def get(self, request):
        file_list = models.FileObj.objects.all()
        # 将数据库中的str对象转为list对象
        for file_obj in file_list:
            file_obj.fileMsg = eval(file_obj.fileMsg)
        return render(
            request,
            "upload_file.html",
            {"file_list": file_list}
        )

    def post(self, request):
        pass

 


 

 

三、上传与下载

下载zip压缩包文件异常截图

下载文件后,无法正常打开

需手动加后缀名修改文件格式方可正常打开
进而能得知文件内容正常
如下图,给“下载”文件加上后缀即可得到正常下载的内容

 

打开网页检查,如下图

发现是Content-Disposition出错

接下来,就针对这个属性在网上搜集资料,解决步骤如下:

1、导入模块

from django.utils.encoding import escape_uri_path

2、重写该属性

response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(the_file_name)) # 正确写法

完整下载功能源码:

注:其中BUG1和BUG2普通解决方案不是最佳解决途径

from django.http import StreamingHttpResponse

# 切片读取文件
def file_iterator(filename,chunk_size=512):
    with open(filename,'rb') as f:
        while True:
            c=f.read(chunk_size)
            if c:
                yield c
            else:
                break

# 下载功能
def download_file(request):
    the_file_name = models.FileObj.objects.get(id=request.GET.get("id")).fileName  # 显示在弹出对话框中的默认的下载文件名
    print(the_file_name)
    file_path = os.path.join(file_dir,the_file_name) # 要下载的文件路径
    response = StreamingHttpResponse(file_iterator(file_path))
    response['Content-Type'] = 'application/octet-stream' # #设定文件头,这种设定可以让任意文件都能正确下载,而且已知文本文件不是本地打开
    # response['Content-Disposition'] = 'attachment;filename="download.zip"' # BUG1:给出一个固定的文件名,且不能为中文,文件名写死了
    # response['Content-Disposition'] = 'attachment;filename={0}'.format(the_file_name.encode("utf-8")) # BUG2:中文会乱码
    response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(the_file_name)) # 正确写法
    return response

 正常下载zip文件的效果图:

 正常显示下载zip文件实例

上传并解压rar压缩包异常截图

 

参阅RAR官方文档:http://python-unrar.readthedocs.io/en/v0.3/

得知第三方库unrar依赖于RAR库,解决思路就是先导入RAR库,然后再导入第三方unrar库即可,具体执行步骤如下:

因为win和linux系统的差异,需分别进行操作:

从python-unrar开源项目官网https://github.com/matiasb/python-unrar/blob/master/README.md

得知了Python下的unrar还依赖RAR官方的库。 那么,接下来就分Windows和Linux两个系统来介绍解决步骤。 Win:
1. 先到RARLab官方下载库文件,http://www.rarlab.com/rar/UnRARDLL.exe ,然后安装; 2. 安装最好选择默认路径,一般在 C:\Program Files (x86)\UnrarDLL\ 目录下; 3. 然后重要的一步,就是添加环境变量,此电脑(我的电脑)右键,属性,找到 高级系统设置,高级 选项卡下点击 环境变量,在系统变量(注意不是用户变量)中 新建,变量名输入 UNRAR_LIB_PATH ,必须一模一样,变量值要特别注意!如果你是64位系统,就输入 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll,如果是32位系统就输入 C:\Program Files (x86)\UnrarDLL\UnRAR.dll ,这个从unrar安装目录的内容也能看出来它是区分64和32位的。 4. 确定保存环境变量后,重启你的PyCharm,代码不变,再运行就不会出错了。这个时候依赖库已经添加到系统环境中。 Linux需要自己手动编译生成so文件,稍微麻烦一点: 1. 同样的,先去下载源文件,不过这就不像Win那样给你封装好了,你需要下载的是源代码:http://www.rarlab.com/rar/unrarsrc-5.4.5.tar.gz ,也就是RARLab官网下载列表中的 UnRAR Source,可以下载到最新版本; 2. 下载完后解压,得到unrar目录,cd unrar 后,使用 make lib 命令将会自动编译库文件,哗啦啦编译完成后,再使用 make install-lib 命令产生 libunrar.so 文件(一般在 /usr/lib 目录下面); 3. 最后,你仍然需要设置Linux系统的环境变量,找到 /etc 目录下的 profile 文件,当然你可以直接使用 vim /etc/profile 命令来编辑(有WinSCP这种远程访问目录的工具更方便),在 profile 文件末尾加上 export UNRAR_LIB_PATH=/usr/lib/libunrar.so ,别把我这句话的逗号加进去了。成功保存后再使用 source /etc/profile 命令使变量生效。 4. 这样一来,再运行py文件,就不会出错了,至少不会提示找不到unrar库了。 友情提示:如果 make 命令用不了的,请自行安装g++编译器,命令:sudo apt-get install g++

上传并解压功能主要源码:【tar,zip,rar三种压缩文件】

from unrar import rarfile
import shutil

UPLOAD_FILE_TYPE = ["zip", "tar", "gztar","bztar","xztar","rar"] # 上传功能 class Uploading(View): def get(self, request): return render(request, "uploading.html", ) def post(self, request): # 拿到上传文件对象 file_obj = request.FILES.get("user_file") # 将上传文件的文件名字,从右边按'.'切割一次,返回值是列表[文件名,类型] filename, suffix = file_obj.name.rsplit(".", maxsplit=1) # 校验上传文件类型,不匹配的类型返回错误提示 if suffix not in UPLOAD_FILE_TYPE: return HttpResponse("上传文件格式不正确") # 拼接得到上传文件的全路径,并且统一上传文件放在upload_file文件夹中 file_name = os.path.join(file_dir, file_obj.name) # 新建一个和上传文件同名的文件 with open(file_name, "wb") as f: # 从上传文件对象一点一点读取数据,避免过大文件溢出内存 for chunk in file_obj.chunks(): f.write(chunk) # 对上传的文件做处理 upload_path = os.path.join(file_dir, "files") # 解压文件至指定文件夹 if suffix == "rar": # 读取rar文件 rar = rarfile.RarFile(file_name) os.chdir(upload_path) # 通过索引获取压缩文件中的文件 rar.extractall() else: shutil.unpack_archive(file_name, extract_dir=upload_path) # getFile(upload_path) # 初始化总行数和单个文件信息属性 totalline = 0 fileinfo_list = [] # 遍历解压后的文件列表,统计单个文件的行数并汇总 for filelist in filelists: fileinfo_list.append(countLine(filelist)[0]) totalline = totalline + countLine(filelist)[1] # 解压压缩文件,并获取代码行数属性 file_info = fileinfo_list total_line = totalline # 单个文件进行文件对象实例化,文件名,文件大小,代码行数 models.FileObj.objects.create( fileName=file_obj.name, fileSize=file_obj.size, fileMsg=file_info, fileLineCount=total_line ) return redirect("/upload_file/")

补:上述代码中的遍历文件和读取代码行数源码:

import platform
import os
# 获取当前项目运行的系统类型,返回字符串Windows,linux等
sys_str = platform.system()


# 定义全局变量,存储上传解压后的文件列表
filelists = []

# 指定想要统计的文件类型
whitelist = ['py']


# 遍历文件, 递归遍历文件夹中的所有
def getFile(basedir):
    global filelists
    for parent, dirnames, filenames in os.walk(basedir):
        # for dirname in dirnames:
        #    getFile(os.path.join(parent,dirname)) #递归
        # MAC环境下略过__MACOSX文件夹
        if "__MACOSX" in dirnames:
            pop_index = dirnames.index("__MACOSX")
            dirnames.pop(pop_index)
        for filename in filenames:
            ext = filename.split('.')[-1]
            # 只统计指定的文件类型,略过一些log和cache文件
            if ext in whitelist:
                filelists.append(os.path.join(parent, filename))


# 统计一个文件的行数
def countLine(fname):
    count = 0
    single_quotes_flag = False
    double_quotes_flag = False
    with open(fname, 'rb') as f:
        for file_line in f:
            file_line = file_line.strip()
            # print(file_line)
            # 空行
            if file_line == b'':
                pass

            # 注释 # 开头
            elif file_line.startswith(b'#'):
                pass

            # 注释 单引号 ''' 开头
            elif file_line.startswith(b"'''") and not single_quotes_flag:
                single_quotes_flag = True
            # 注释 中间 和 ''' 结尾
            elif single_quotes_flag == True:
                if file_line.endswith(b"'''"):
                    single_quotes_flag = False

            # 注释 双引号 """ 开头
            elif file_line.startswith(b'"""') and not double_quotes_flag:
                double_quotes_flag = True
            # 注释 中间 和 """  结尾
            elif double_quotes_flag == True:
                if (file_line.endswith(b'"""')):
                    double_quotes_flag = False

            # 代码
            else:
                count += 1

        # 单个文件行数
        # print(fname, '----count:', count)
        # return count
        if sys_str == "Windows":
            fileinfo = str(fname.split('\\')[-1] + ",count:----" + str(count))
        else:
            fileinfo = str(fname.split('/')[-1] + ",count:----" + str(count))
        return fileinfo, count

四、Django调试工具:debug_toolbar

介绍

django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息。

github地址

文档地址

 

安装

pip3 install django-debug-toolbar

配置

1. settings.py中

将 debug_toolbar 添加到 INSTALL_APPS 中

INSTALLED_APPS = [
    …
    'debug_toolbar',
]

2. urls.py中

from django.conf import settings
from django.conf.urls import include, url


# django 调试工具路由
if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'__debug__/',include(debug_toolbar.urls)),
    ] + urlpatterns

3. settings.py中

在中间件中加入DebugToolbarMiddleware

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #
    'debug_toolbar.middleware.DebugToolbarMiddleware',# django 调试工具
    #
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

4. 如果是本机调试,还在将127.0.0.1加入 INTERNAL_IPS

在settings.py中加入以下配置项:

# django调试工具配置项:
INTERNAL_IPS = ['127.0.0.1', ] # 本机调试

5.  配置jQuery的URL

django-debug-toolbar 默认使用的是Google的地址,默认配置如下:

JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'

国内用不了的话可以在settings.py中配置一下,例如(我这里特意选用了和原作者相同版本的jQuery):

# 如果在Django项目中使用了jquery的话就可以直接将这一项置为空,那么django-debug-toolbar 就会使用你项目中用到的jquery:
DEBUG_TOOLBAR_CONFIG = {
    # "JQUERY_URL": '',
    "JQUERY_URL": '//cdn.bootcss.com/jquery/2.2.4/jquery.min.js',
}

使用

访问具体路径的时候在页面右侧有各项配置面板,点击即可查看各种调试信息。

 

 

 

(续)

posted @ 2018-06-14 17:42  MJay_Lee  阅读(1610)  评论(0编辑  收藏  举报