编写django项目步骤及其出现的问题总结

一、项目开始

  1. 创建项目

django-admin startproject fresh(项目名)

  1. 创建app

python manage.py startapp user(应用名)

  1. 配置setiing

1)添加应用

在ALLOWED_HOSTS中添加app

2)设置可以访问的IP

在ALLOWED_HOSTS中设置

3)设置模版(TEMPLATES)

'DIRS': [os.path.join(BASE_DIR, 'templates')],

4)设置编码和时区

LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

5)设置数据库

1>首先在项目__init__.py中连接数据库

import pymysql

pymysql.install_as_MySQLdb()

2>设置数据库

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',  # 使用的数据库

        'NAME': 'fresh',  # 使用数据库的名字

        'USER': 'root',  # mysql数据库用户名

        'PASSWORD': '......',  # 数据库登陆密码

        'HOST': '127.0.0.1',  # 数据库所在主机

        'POST': 3306,  # 数据库端口号

    }

}

6)生成迁移文件

python manage.py makemigrations生成

python manage.py migrate迁移

7)创建超级用户

python manage.py createsuperuser

二、项目过程

1)编写models模块,规划数据库各种字段

·首先分析需求

·制定所需要的应用及其模块类型

2)编写views模块,写各种功能的控制和显示

3)写好url模块,配置完整的路由信息

三、项目中存在的问题

1)当写继承django系统的模型类的时候必须在stiing中配置:

AUTH_USER_MODEL = 'users.Users'  (应用.模型)

2)django.db.utils.OperationalError: unable to open database file

解决:在stiing中修改数据库:'ENGINE': 'django.db.backends.mysql'

3)django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module               named 'MySQLdb'.  Did you install mysqlclient or MySQL-python?

解决:在项目模块__init__中添加:

import pymysql

pymysql.install_as_MySQLdb()

4)django.urls.exceptions.NoReverseMatch: Reverse for 'order' not found. 'order' is not a valid view function or pattern name.

解决:{% url 'user:order' 1 %}  冒号左右不能出现空格

5)127.0.0.1 将您重定向的次数过多。

解决:LoginRequiredMixin这个继承类不能使用在登陆注册上使用,不然会报重定向次数过多。

6)django.core.exceptions.ImproperlyConfigured: WSGI application 'fresh.wsgi.application' could not be loaded; Error importing module: 'No module named 'redis_sessions''

解决:安装redis-session后之后正常运行,然后untubu和windos配置问题

Untubu:SESSION_ENGINE = "redis_sessions.session"

Windos:SESSION_ENGINE = "django.contrib.sessions.backends.cache"

7)Could not find backend 'django_redis.cache.RedisCache': No module named 'django_redis'

解决:原django-redis版本3.2,升级到4.1之后正常使用

8)NOAUTH Authentication required.

解决:在setiings.py配置连接redis数据库时加上password=密码,根据自己当时给redis设置密码与否,若没设置则可以不用设置

9)You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/user/register/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

解决:      <form method="post" action="/user/register/"> from表单后面的路径最后要加斜杠

10)error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

解决:

10)ImportError: cannot import name 'AliPay' from 'alipay' (D:\tools\pythonhj\lib\site-packages\alipay\__init__.py)

         解决:# 从 1.3.0升级上来的用户, 请先卸载pycrypto:
            pip uninstall pycrypto
            # 安装python-alipay-sdk
            pip install python-alipay-sdk --upgrade

11)SyntaxError: Generator expression must be parenthesized

解决:widgets.py文件中%s=%s' % (k, v) for k, v in params.items(),后面的逗号去掉,这是django版本问题

12)raise ValueError("Dependency on app with no migrations: %s" % key[0])

ValueError: Dependency on app with no migrations: user

解决:重新迁移文件

13)当图片路径自动加上多余部分时,应从根目录下寻找图片位置,例如:

问题:"GET /user/static/images/goods/goods002_uyzKQ7k.jpg HTTP/1.1" 404 4228

数据库存储路径:static/images/goods/goods002_uyzKQ7k.jpg

解决:<img src="/{{ goods.image.url }}">在静态页面图片路径前加上斜杠,表示从根目录寻找。

14)No fields were found in any search_indexes. Please correct this before attempting to search.

解决:建立索引文件search_indexes

15)You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/user/register/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

解决:

四、其他

1)数据库授权

授权(fresh.*指的是fresh数据库下所有表)

Grant all privileges on fresh.* to ‘root’@’127.0.0.1’ identified by ‘root’ with grant option

授权生效:

Flush privileges

猜你喜欢

转载自www.cnblogs.com/quietly-elegant/p/10257117.html