路漫漫其修远兮,吾将上下而求索。

导航

安装Django1.11后启动失败的解决方法和第一个Django工程

1. Django和python版本信息

Django版本:Django 1.11(可通过pip list命令查看)
python版本:Python 3.7.6(可通过python --version命令查看)
客户机操作系统版本:Windows 10 家庭中文版

2. 问题描述

2.1 在Windows上安装Django

    C:\WINDOWS\system32>pip install -i https://pypi.douban.com/simple django==1.11

2.2 新建Django项目

    D:\>django-admin startproject mysite

2.3 启动Django,报错如下

    启动命令:D:\mysite>python manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001CDFA9D6DC8>
Traceback (most recent call last):
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\utils\autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\apps\registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\apps\config.py", line 94, in create
    module = import_module(entry)
  File "D:\Application\LINUX\python\python-3.7.6\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\contrib\admin\__init__.py", line 4, in <module>
    from django.contrib.admin.filters import (
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\contrib\admin\filters.py", line 10, in <module>
    from django.contrib.admin.options import IncorrectLookupParameters
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\contrib\admin\options.py", line 12, in <module>
    from django.contrib.admin import helpers, widgets
  File "D:\Application\LINUX\python\python-3.7.6\lib\site-packages\django\contrib\admin\widgets.py", line 151
    '%s=%s' % (k, v) for k, v in params.items(),
    ^
SyntaxError: Generator expression must be parenthesized

2.4 问题产生原因和解决方案

问题原因
django 1.11版本和python3.7版本不兼容, 2.0版本以后的Django修复了这个问题
解决方案1
修改\You_local_Path\python-3.7.6\Lib\site-packages\django\contrib\admin\widgets.py,去掉图片中的逗号即可(这里采用该种解决方案)


解决方案2
升级Django版本:pip install -U Django

2.5 采用方案1后,重启Django成功

D:\mysite>python manage.py runserver 127.0.0.1:8080
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 02, 2020 - 21:05:56
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CTRL-BREAK.
[02/May/2020 21:06:11] "GET / HTTP/1.1" 200 1716
Not Found: /favicon.ico
[02/May/2020 21:06:12] "GET /favicon.ico HTTP/1.1" 404 1962

通过浏览器访问,页面显示正常,问题消除

虽然页面能正常显示了,但是输出信息中存在报错,因此到项目目录下执行命令:python manage.py migrate

D:\mysite>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

完成后,重启Django项目,报错消失,如下:

Performing system checks...

System check identified no issues (0 silenced).
May 04, 2020 - 13:16:14
Django version 1.11, using settings 'MyDjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

3 第一个展示指定页面的Django项目

修改urls.py文件中的内容(直接使用下面的内容替换掉该页面中的代码即可),如下,这种是Django定义好的模板,按照要求写就行

from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse, render

def login(request):
    """
    处理用户请求,并返回内容
    :param request: 用户请求的所有信息(对象)
    :return:
    """
    # return HttpResponse('welcome')      # 直接将括号中的字符串返回给浏览器
    return render(request, 'login.html')  # 将login.html文件返回给浏览器

urlpatterns = [
    # url(r'^admin/', admin.site.urls),
    url(r'^login', login),
]


页面将要展示的html文件内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
    <h1>请输入用户名和密码</h1>
    <form action="">
        <input type="text" name="username">
        <input type="password" name="password">
        <div>
            <input type="submit" value="login">
        </div>
    </form>

</body>
</html>

启动Django项目,并在浏览器中访问,结果如下:

此时pycharm中Django输出信息如下所示:

posted on 2020-05-02 21:23  rushiy  阅读(901)  评论(0编辑  收藏  举报