django踩坑记录


在django中使用bootstrap4报错:OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: ‘「frozen importlib._bootstrap」‘”

在aototest/settings.py 中加入如下内容:


INSTALL_APPS = (

#...

'bootstrap4',

#...

```

在product_manage.html中:

```

# {# Load the tag library #}

{% load bootstrap4 %}

# {# Load CSS and JavaScript #}

{% bootstrap_css %}

{% bootstrap_javascript jquery='full' %}


随后访问 http://127.0.0.1:8000/product_manage/

链接失败。

查看development server已自动退出,重启server:

C:\Users\Jhin\AppData\Local\Programs\Python\Python37\Scripts\autotest>python manage.py runserver

Watching for file changes with StatReloader

Exception in thread django-main-thread:

Traceback (most recent call last):

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner

    self.run()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run

    self._target(*self._args, **self._kwargs)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 53, in wrapper

    fn(*args, **kwargs)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\commands\runserver.py", line 109, in inner_run

    autoreload.raise_last_exception()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 76, in raise_last_exception

    raise _exception[1]

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\__init__.py", line 357, in execute

    autoreload.check_errors(django.setup)()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 53, in wrapper

    fn(*args, **kwargs)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\apps\registry.py", line 91, in populate

    app_config = AppConfig.create(entry)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\apps\config.py", line 90, in create

    module = import_module(entry)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "", line 1006, in _gcd_import

  File "", line 983, in _find_and_load

  File "", line 965, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'bootstrap'

Traceback (most recent call last):

  File "manage.py", line 21, in

    main()

  File "manage.py", line 17, in main

    execute_from_command_line(sys.argv)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\__init__.py", line 401, in execute_from_command_line

    utility.execute()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\__init__.py", line 395, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\base.py", line 328, in run_from_argv

    self.execute(*args, **cmd_options)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\commands\runserver.py", line 60, in execute

    super().execute(*args, **options)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\base.py", line 369, in execute

    output = self.handle(*args, **options)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\commands\runserver.py", line 95, in handle

    self.run(**options)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\core\management\commands\runserver.py", line 102, in run

    autoreload.run_with_reloader(self.inner_run, **options)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 599, in run_with_reloader

    start_django(reloader, main_func, *args, **kwargs)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 584, in start_django

    reloader.run(django_main_thread)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 299, in run

    self.run_loop()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 305, in run_loop

    next(ticker)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 345, in tick

    for filepath, mtime in self.snapshot_files():

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 361, in snapshot_files

    for file in self.watched_files():

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 260, in watched_files

    yield from iter_all_python_module_files()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 105, in iter_all_python_module_files

    return iter_modules_and_files(modules, frozenset(_error_files))

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\site-packages\django-3.0.8-py3.7.egg\django\utils\autoreload.py", line 141, in iter_modules_and_files

    resolved_path = path.resolve(strict=True).absolute()

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\pathlib.py", line 1161, in resolve

    s = self._flavour.resolve(self, strict=strict)

  File "C:\Users\Jhin\AppData\Local\Programs\Python\Python37\lib\pathlib.py", line 200, in resolve

    return self._ext_to_normal(_getfinalpathname(s))

OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: ''

百度几天均无果,后重新安装bootstrap

pip install django-bootstrap4

runserver还是一样的报错。

重新运行:

python manage.py migrate

随后runserver成功:

C:\Users\Jhin\AppData\Local\Programs\Python\Python37\Scripts\autotest>python manage.py runserver

Watching for file changes with StatReloader

Performing system checks...

System check identified no issues (0 silenced).

July 13, 2020 - 03:12:05

Django version 3.0.8, using settings 'autotest.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

[13/Jul/2020 03:12:15] "GET /product_manage/ HTTP/1.1" 200 2399

总结为先

python manage.py migrate

python manage.py runserver

就成功了(不知道为啥,手动狗头)

截图为证:

![自动化平台demo版](https://img-blog.csdnimg.cn/20200713032254965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

卡了我几天的坑,记录一下。

### 报错:

TemplateSyntaxError at /apistep_manage/

Invalid block tag on line 48: 'else', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713143236493.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

几经排查后还是找不到原因,后受文章 [传送门](https://blog.csdn.net/sddr_circle/article/details/80510726) 的启发,在if开始时的地方查找,找到问题所在:

![](https://img-blog.csdnimg.cn/20200713143604829.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

此处少了一个百分号%,添加上之后,报错解决。

### 报错:TemplateSyntaxError at /apistep_manage/

Could not parse the remainder: '% if apistep.apistatus == 1 %' from '% if apistep.apistatus == 1 %'

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713143901824.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

经排查,系此处多了一对花括号,去除一对花括号后,该报错解决。

### 报错:Unclosed tag on line 37: 'for'. Looking for one of: empty, endfor.

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713145704985.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

后排除问题如下:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713145836319.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

最后显示界面如下:

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020071315003824.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

总结:**一定要注意百分号空格/百分号是否成对/一对百分号是否写成了两对。。。引以为戒中。。。**

### 报错:TypeError: bug_manage() missing 1 required positional argument: 'request'

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200714080704604.png)

经排查,系此处括号有问题:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200714080742555.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

### runserver时报错:

raise RuntimeError("cryptography is required for sha256_password or caching_sha2_password")

RuntimeError: cryptography is required for sha256_password or caching_sha2_password

原因是没有连接数据库。

### 前端界面异常

搭建测试平台的时候,出现前端一场:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200714195700512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

经排查,乃一低级错误:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200714195754329.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

如图所示,container写到了引号外面(手动狗头),修改后显示正常。

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200714195903630.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTkwNjUz,size_16,color_FFFFFF,t_70)

你可能感兴趣的:(django踩坑记录)