Django,NoReverse Match

时间:2014-05-04 02:30:16

标签: python django

所以我完成了我的应用程序并且有几个模型。此特定错误仅针对某些值出现。示例:每个用户都注册了课程和年份。因此,在一个页面中,我试图显示所选年份所有属于该课程的模块。当用户在year 3学习时,它可以正常工作。如果它year 2它会抛出该错误。

Reverse for 'module_details_upcoming' with arguments '(10L,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['studies/module/(?P<module_id>\\d)/upcoming/$']

在错误页面中,它抱怨{% url "module_details_upcoming" module.id %},我猜也是{% url "module_details_previous" module.id %}

下面你可以看到我的代码:

views.py

def modules(request):
    if request.user.is_authenticated():
        user_id = request.user.id

        try:
            studying_course = Student.objects.get(user__id=user_id).course
            studying_year = Student.objects.get(user__id=user_id).year
        except Student.DoesNotExist:
            return HttpResponseRedirect(reverse('step2'))

        try:
            modules_list = Module.objects.filter(course=studying_course,year=studying_year).exclude(name="school")
        except Module.DoesNotExist:
            modules_list = None
        # import pdb; pdb.set_trace()
        return render_to_response("studies/modules.html", {'course': studying_course, 'year': studying_year, 'modules': modules_list,},  RequestContext(request))

    else:
        return HttpResponseRedirect(reverse('home'))

def module_details_upcoming(request, module_id):
    if request.user.is_authenticated():
        user_id = request.user.id
        dated = True

        try:
            studying_course = Student.objects.get(user__id=user_id).course
            studying_year = Student.objects.get(user__id=user_id).year
        except Student.DoesNotExist:
            return HttpResponseRedirect(reverse('step2'))

        try:
            module = Module.objects.get(id=module_id)
        except Module.DoesNotExist:
            module = None

        now = datetime.datetime.now()
        events = Event.objects.filter(module__in=module_id,date__gte=now).order_by('date')

        paginator = Paginator(events, 5)
        try:
            page = int(request.GET.get("page", '1'))
        except ValueError:
            page = 1

        try:
            posts = paginator.page(page)
        except (InvalidPage, EmptyPage):
            posts = paginator.page(paginator.num_pages)

        return render_to_response("studies/module.html", {'module': module, 'events': events, 'posts': posts, 'dated': 'dated'},  RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('home'))

urls.py

urlpatterns = patterns('',
    url(r'^modules/$', 'studies.views.modules', name="modules"),
    url(r'^module/(?P<module_id>\d)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
    url(r'^module/(?P<module_id>\d)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),
)

模板文件:

            {% if modules %}
                <p>Below you can find all your modules for this year.</p>
                <table class="table table-hover">
                    <th>Code</th>
                    <th>Name</th>
                    <th>Tasks/Events</th>
                {% for module in modules %}
                    <tbody>
                        <td>{{ module.code }}</td>
                        <td>{{ module.name }}</td>
                        <td><a href="{% url "module_details_upcoming" module.id %}">Upcoming</a> / <a href="{% url "module_details_previous" module.id %}">Previous</a></td>
                    </tbody>
                {% endfor %}
                </table>
            {% else %}
                <p>Oops! It seems that there are no modules for {{ course.name }} in our records. Please contact your school office.</p>
            {% endif %}

1 个答案:

答案 0 :(得分:2)

您需要在module_id标记中传递关键字参数url

{% url "module_details_upcoming" module_id=module.id %}

另请参阅此类似问题:NoReverseMatch with {% url ... %} and keyword args

UPD:您实际上是在网址中捕获一位数字。由于year 3 module_id10(两位数),因此无法完成反向。

替换:

url(r'^module/(?P<module_id>\d)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
url(r'^module/(?P<module_id>\d)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),

with(观看\d+而不是\d):

url(r'^module/(?P<module_id>\d+)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
url(r'^module/(?P<module_id>\d+)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),