Django開發過程中遇到的問題和解決方案


1.django向數據庫中添加中文時報錯

解決方案:創建數據庫的時候設置編碼格式

2.django的信號使用無法觸發信號里的內容

解決方案:在django 1.7后,使用信號時候需要在應用配置類中的ready() 方法中連接。
所以我們需要配置先ready()
需要在以下兩個地方寫入配置
需要在項目的app.py,init.py兩個文件中寫入配置

3.django-admin.py:未找到命令

解決方案:將django的安裝路徑添加到環境變量中,
一般的安裝路徑在python目錄下的\Lib\site-packages\Django-1.8-py2.7.egg\django\bin,
可能有不同,總之在python安裝目錄下找到django\bin即可。
添加完環境變量后記得重啟命令提示符,否則還是提示錯誤。

4.當我把 DEBUG = True設為False的時候運行 python manage.py runserver 的時候

報錯 : CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
解決方案: 在 setting.py 中添加 ALLOWED_HOSTS = [‘127.0.0.1‘, ‘localhost‘]

5.外部機器無法訪問本站點

然后在settings里修改ALLOWED_HOSTS = [],
改為ALLOWED_HOSTS = [‘*’,]

6.django解決跨域請求的問題

解決方案:添加中間件,安裝django-cors-headers
配置settings.py文件,添加
MIDDLEWARE_CLASSES = (

‘corsheaders.middleware.CorsMiddleware’,
‘django.middleware.common.CommonMiddleware’, # 注意順序

)
#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
‘*’
)
7.安裝Mysql,詳細教程:https://blog.csdn.net/bobo553443/article/details/81383194
,遇到出現error 1042,無法正常啟動解決方案:https://www.jianshu.com/p/de3adc46c8ec
8.django.db.utils.OperationalError: (1049, "Unknown database 'djangodb'")
DATABASES = {

'default': {

'ENGINE':'django.db.backends.mysql',

'NAME': 'mysql',

'USER':'root',

'PASSWORD':'root',

'HOST':'127.0.0.1',

'PORT':'3306',

}

}

出這個問題是因為在setting.py的配置信息NAME配置錯誤,改為mysql即可。
然后再執行

9.ImportError: cannot import name 'render_to_response' 解決方法
Django 3.0 已經將 render_to_response 移除了。因為有部分代碼在用 render_to_response ,而我們打包鏡像的時候沒有指定 Django 版本,所以啟動時報錯了。
(1)方法一
安裝指定版本的 Django 版本(3.0以下),如:

pip3 install django==2.1.3
(2)方法二
使用 render 代替 render_to_response。

相同點:都是展示模板頁面的。

不同點:render 方法可接收三個參數,一是request參數,二是待渲染的html模板文件,三是保存具體數據的字典參數。它的作用就是將數據填充進模板文件,最后把結果返回給瀏覽器。render 自動使用RequestContext,而 render_to_response 需要 coding 進去。

return render(request,"information.html",{"name":"test","password":"123456"})
return render_to_response("information.html",{"name":"test","password":"123456"},context_instance = RequestContext(request))

10.Django中html里的語法標簽{% dosomething %},“%”與"{” 或者"}"之間不能有空格!

11.發生'staticfiles' is not a registered tag library錯誤,主要是因為:staticfiles is now deprecated and you have to load it as {% load static %} instead of old way {% load static from staticfiles %}

12.ImportError: No module named 'django.core.urlresolvers',Django 2.0 把原來的 django.core.urlresolvers 包改為了 django.urls包,所以需要把原來的from django.core.urlresolvers import reverse 改為 from django.urls import reverse

13.DoesNotExist at /admin/ Site matching query does not exist.
看了
http://stackoverflow.com/questions/6086852/how-to-fix-the-django-sites-table
You don't really need the sites framework if you only run one site from the project, so the easiest fix would be to remove the following item from your INSTALLED_APPS and the error should go away:
'django.contrib.sites'
You can also re-create the missing Site object from shell. Run python manage.py shell and then:
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='mdev.5buckchuck.com', name='5buckchuck.com')


免責聲明!

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



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