是网址还是观点?

时间:2013-01-21 04:33:17

标签: django

看起来应该有用......但.. 这是views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from create_lesson.models import Lesson
from django.contrib.auth.models import User
from add_material.models import Materials

def Use_Lesson(request, lesson):
    user=request.user
    lessons = Lesson.objects.get(slug=lesson)
    lessons.attributes=Materials.objects.filter(lesson=lessons.pk)
    context = {'user':user,'lesson':lessons,}
    return render_to_response('use_lesson/use_lesson.html', context, context_instance=RequestContext(request))

urls.py看起来像这样

from django.conf.urls.defaults import *

urlpatterns= patterns('',
    (r'^browse/', 'use_lesson.views.Browse'),
    (r'^lesson/(?P<lesson>[a-zA-Z0-9-]+)', 'use_lesson.views.Use_Lesson'),   

这是基本的错误消息 请求方法:GET 请求网址:... / lesson / superfarts Django版本:1.4.1 异常类型:DoesNotExist 例外值:课程匹配查询不存在。

并且问题就在这里:lessons = Lesson.objects.get(slug = lesson)

当我在“python manage.py shell”中执行相同的查询时 我得到了我正在寻找的结果。

2 个答案:

答案 0 :(得分:1)

尝试将其更改为:

(r'^lesson/(?P<lesson>[a-zA-Z0-9-]+)/$', 'use_lesson.views.Use_Lesson')

答案 1 :(得分:0)

一个slugfield最好用正则表示\ W等于[a-zA-Z0-9_]

(r'^lesson/(?P<lesson>\W+)/$', 'use_lesson.views.Use_Lesson')