1.TypeError at /admin/
問題呈現:當在瀏覽器輸入127.0.0.1:8000/admin/
時,出現如下錯誤提示:
TypeError at /admin/
argument to reversed() must be a sequence
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.9.4
Exception Type: TypeError
Exception Value:
argument to reversed() must be a sequence
Exception Location: /usr/local/var/pyenv/versions/2.7.11/lib/python2.7/site-packages/django/core/urlresolvers.py in _populate, line 293
Python Executable: /usr/local/var/pyenv/versions/2.7.11/bin/python2.7
Python Version: 2.7.11
Python Path: ...
...
問題解決:經網上一番搜尋后,發現如下兩個url配置的{}
應該改成[]
,問題即可解決,如下所示:
#項目主目錄文件夾/urls.py
urlpatterns = { #---->should be change to [
url(r'^admin/', admin.site.urls),
url(r'^rango/', include('rango.urls')),
} #---->should be change to ]
#rango/urls.py
urlpatterns = { #---->should be change to [
url(r'^$', views.index, name='index'),
url(r'^about/', views.about, name='about'),
} #---->should be change to ]
問題總結:在django開發中,url相關路徑的設置最好用[]
。
2.Slug in catergories is causing me problems!
問題呈現:按照TWD的教程,學習到7.32節為Category表增加Slug字段
時,運行python manage.py makemigrations rango
后,出現如下問題:
? python manage.py makemigrations
You are trying to add a non-nullable field 'slug' to category without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
問題解決:參考了https://github.com/leifos/tango_with_django/issues/19
的討論,按照Charlotteis的方法即可解決問題,他的方法如下:
1. Revert the work you did to add slugs into URLS within the Category model.-->撤銷將slug寫入Category的操作。
2. Makemigration for rango with your reverted changes, then migrate.-->運行makemigratons和migrate兩個命令。
3. runserver and go to the admin interface, delete all the categories which will also delete all pages.-->運行服務器程序,進入管理界面,刪掉所有的Category和Page。
4. Redo the work to add slugs to the Category model--將slug重新寫入Category。
5. Makemigration, then migrate.-->運行makemigrations和migrate兩個命令。
6. Run the populate script.-->運行populate_rango的腳本程序。
問題思考:我猜想出錯的原因可能是:對于數據庫中已經存在的數據類,如果某字段的關鍵字unique
為True
,則不能繼續對該數據類添加其他字段關鍵字unique
為True
的數據。
3.Pillow is not installed
按照教程,學習到9.3節增加用戶屬性
時,仿照教程敲好代碼后,運行python manage.py makemigrations
,出現如下錯誤:
SystemCheckError: System check identified some issues:
ERRORS:
rango.UserProfile.picture: (fields.E210) Cannot use ImageField because Pillow is not installed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".
其實我的mac電腦里之前已經裝好了Pillow
,但我還是按照提示pip install Pillow
后,重新運行python manage.py makemigrations
,仍然報出以上錯誤。之后我甚至pip uninstall Pillow
和pip install Pillow
后,仍然解決不了問題。之后網上搜尋答案,找到了
stackoverflow(http://stackoverflow.com/questions/25662073/python-django-cannot-use-imagefield-because-pillow-is-not-installed?rq=1)
上面的回答,按照給出的答案嘗試后,仍然解決不了問題。不過從第二個回答中找到靈感,在3.5版本和2.7.11版本的IDLE
中輸入from PIL import Images
,發現3.5版本不報錯,2.7.11版本(就是項目的django環境版本)報錯,或許用3.5版本作為項目的環境版本就不會出現以上問題,這有待以后試驗。
最后的解決辦法,注釋掉項目中會使用到Pillow
的相關語句,如下所示:
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True)
# picture = models.ImageField(upload_to='profile_images', blank=True) -->將此句注釋掉,就它是引發Pillow錯誤的罪魁禍首。
def __unicode__(self):
return self.user.username
希望此問題以后能根本解決。
4.模板標簽的url傳遞
問題呈現:按照TWD的教程,學習到第10.4
節,在index.html
的將<li><a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>
中的href
引用變成<li><a href="{% url 'category' category.slug %}">{{ category.name }}</a></li>
,出現如下錯誤:
NoReverseMatch at /rango/
Reverse for 'category' with arguments '(u'',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['rango/category/(?P<category_name_slug>[\\w\\-]+)/$']
問題解決:暫未找到解決辦法,此步驟不要仿照教程做改變。