首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django ValueError

Django ValueError
EN

Stack Overflow用户
提问于 2012-12-05 15:31:49
回答 1查看 253关注 0票数 0

当我在一本名为“实践Django项目”的书上做练习时,我遇到了一些problem.When,我点击了我的文章中关于细节的网址,它返回了一个错误。

urls.py

代码语言:javascript
运行
复制
url(r'^weblog/$','django.views.generic.date_based.archive_index',entry_info_dict),
url(r'^weblog/(P<year>\d{4}/$)','django.views.generic.date_based.archive_year', entry_info_dict),
url(r'^weblog/(P<year>\d{4}/(P<month>\w{3})/$)','django.views.generic.date_based.archive_month', entry_info_dict),
url(r'^weblog/(P<year>\d{4}/(P<month>\w{3})/(P<day>\d{2})/$)','django.views.generic.date_based.archive_day', entry_info_dict),
url(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(P<day>\d{2})/(P<slug>[-\w]+)/$)',"blogCategory.views.entry_detail"),

views.py

代码语言:javascript
运行
复制
def entry_detail(request, year, month, day, slug):
    date_stamp=time.strptime(year+month+day, "%Y%b%d")
    pub_date=datetime.date(*date_stamp[:3])
    entry=get_object_or_404(Entry,pub_date__year=pub_date.year,
        pub_date__month=pub_date.month,
        pub_date__day=pub_date.day,
        slug=slug)
    return render_to_response('Entry/entry_detail.html',{'entry': entry })

错误:

代码语言:javascript
运行
复制
Traceback :
ValueError at /weblog/2012/dec/04/test22/

time data u'2012/dec/04/test22/dec04' does not match format '%Y%b%d'

Request Method:     GET
Request URL:    /weblog/2012/dec/04/test22/
Django Version:     1.4.1
Exception Type:     ValueError
Exception Value:    

time data u'2012/dec/04/test22/dec04' does not match format '%Y%b%d'

Exception Location:     /usr/lib/python2.7/_strptime.py in _strptime, line 325
Python Executable:  /usr/bin/python
Python Version:     2.7.3
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 15:46:42

错误的数据被传递给了strptime,请看这里:

代码语言:javascript
运行
复制
>>> time.strptime('2012dec04','%Y%b%d')
time.struct_time(tm_year=2012, tm_mon=12, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=339, tm_isdst=-1)
>>> time.strptime('2012/dec/04/test22/dec04','%Y%b%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '2012/dec/04/test22/dec04' does not match format '%Y%b%d'
>>> 

原因是因为你的第一个群没有关闭:

代码语言:javascript
运行
复制
# yours
url(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(P<day>\d{2})/(P<slug>[-\w]+)/$)',"blogCategory.views.entry_detail"),

# change to
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(P<day>\d{2})/(P<slug>[-\w]+)/$',"blogCategory.views.entry_detail"),

您让第一个组在$周围关闭,而不是在第一个/之前关闭

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13718343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档