編寫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

 

注意:本篇文章是個人總結,僅供參考。若出現其他問題,與寫者無關,切勿用於商業獲取福利!

   如果總結有勿,或者交流與學習,請與寫着聯系!qq:1349176744


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM