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\\-]+)/$']
问题解决:暂未找到解决办法,此步骤不要仿照教程做改变。
网友评论