渲染时捕获TypeError:不可哈希类型:'dict
我正在努力学习《实用Django项目》这本书,但遇到了一些问题。你可以在这里找到我目前的代码。(没有“Link”类。我刚刚添加了get_absolute_url这个Entry类,结果在尝试访问管理页面时突然出现了“Caught TypeError while rendering: unhashable type: 'dict'”的错误。这是我说的错误截图。我从来没有修改过错误中提到的那个文件 :(。我该怎么办呢?
编辑:这个错误是在添加以下内容后出现的:
def get_absolute_url(self):
return ('coltrane_entry_detail', (), { 'year': self.pub_date.strftime("%Y"),
'month': self.pub_date.strftime("%b").lower(),
'day': self.pub_date.strftime("%d"),
'slug': self.slug })
get_absolute_url = models.permalink(get_absolute_url)
这是来自urls.py的内容:
urlpatterns = patterns('django.views.generic.date_based',
(r'^$', 'archive_index', entry_info_dict, 'coltrane_entry_archive_index'),
(r'^(?P<year>\d{4})/$', 'archive_year', entry_info_dict, 'coltrane_entry_archive_year'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/$', 'archive_month', entry_info_dict, 'coltrane_entry_archive_month'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', 'archive_day', entry_info_dict, 'coltrane_entry_archive_day'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/?(?P<slug>[-\w]+)/$', 'object_detail', entry_info_dict, 'coltrane_entry_detail'),
)
4 个回答
0
你是否已经用pip和virtualenv设置好了你的Django环境?你的项目有以下依赖:
markdown==2.0.3
django-tagging==0.3.1
我把上面的内容放在一个叫做requirements.txt
的文件里,放在你的项目目录下。一旦你安装了pip和virtualenv,并为这个项目创建了一个独特的环境,你就可以这样安装上面的依赖:
pip install -r requirements.txt
在完成之前的设置后,你需要在你的settings.py
文件中的INSTALLED_APPS
里添加tagging
:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.flatpages',
'cms.search',
'coltrane',
'tagging',
)
删除你的cms.db
数据库文件,然后运行python manage.py sycndb
。你需要提供一个超级用户的用户名和密码。代码在我这边运行得很好,我可以访问管理后台。
1
你能告诉我你在URL配置中,哪个地方给coltrane_entry_detail
这个网址起了名字吗?至少有一个旧的帖子在djangoproject.com上提到,这个错误可能是因为网址模式配置不正确。如果你刚刚为你的模型添加了get_absolute_url
方法,我猜你可能也刚刚添加了它所指向的命名视图吧?
3
抱歉,你的代码现在无法加载。
根据我的猜测,你可能是在尝试把一个 dict
实例当作 dict
的键来使用。
举个例子,你不能这样做:
a = {'1' : 'one'}
b = {a : 'two'}