NoReverseMatch:未传递关键字参数

时间:2014-03-21 17:00:16

标签: django django-templates django-views

我收到以下错误,我不能为我的生活找出问题所在,我已将所有内容从“网站”重命名为“地点”以确保名称与之不冲突除了place_id之外的任何东西仍然没有被传递,虽然我可以在URL和错误页面的交互式视图中看到它为u'7'。

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8080/afp/sites/update/4/

Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.gis',
 'compressor',
 'django_extensions',
 'floppyforms',
 'crispy_forms',
 'djcelery',
 'kombu.transport.django',
 'mptt',
 'south',
 'tastypie',
 'django_nose',
 'selectable',
 'rapidsms',
 'rapidsms.router.db',
 'rapidsms.contrib.handlers',
 'rapidsms.contrib.messagelog',
 'rapidsms_telerivet',
 'phis.nomads',
 'phis.core',
 'phis.afp',
 'phis.messagebox']
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template /home/dan/workspace/phis-polio/eha-phis/phis/afp/templates/sites-update.html, error at line 10
   Reverse for 'sites-update' with arguments '()' and keyword arguments '{u'place_id': ''}' not found. 1 pattern(s) tried: ['afp/sites/update/(?P<place_id>[-\\d]+)/$']
   1 : {% extends "newbase.html" %}


   2 : {% load url from future %}


   3 : {% load floppyforms %}


   4 : {% load staticfiles %}


   5 : {% block title %} - Update Site {% endblock %}


   6 : {% block content %}


   7 : <div class="row">


   8 : <div class="col-sm-6 col-md-6">


   9 :     <h3 class="heading">Update Surveillance Site</h3>


   10 :     <form method="post" action= {% url 'sites-update' place_id=site.pk %} >


   11 :         {% csrf_token %}


   12 :         <div class="formSep">


   13 :             <div class="row">


   14 :                 <div class="col-sm6 col-md-6">


   15 :                 <label for="id_name">Site Name:<span class="f_req">*</span></label>


   16 :                     {{ form.name }}


   17 :                 <span class="help-block">What is the site name?</span>


   18 :                 </div>


   19 :                 <div class="col-sm-6 col-md-6">


   20 :                     <label for="id_lga">LGA:<span class="f_req">*</span></label>


Traceback:
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/dan/workspace/phis-polio/eha-phis/phis/afp/views.py" in sites_update_view
  91.         'place_id': place_id
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  169.         return t.render(context_instance)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  447.                         six.reraise(*exc_info)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  433.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  509.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/dan/workspace/phis-polio/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  429.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /afp/sites/update/4/
Exception Value: Reverse for 'sites-update' with arguments '()' and keyword arguments '{u'place_id': ''}' not found. 1 pattern(s) tried: ['afp/sites/update/(?P<place_id>[-\\d]+)/$']

这是我的模板代码:

{% extends "newbase.html" %}
{% load url from future %}
{% load floppyforms %}
{% load staticfiles %}
{% block title %} - Update Site {% endblock %}
{% block content %}
<div class="row">
<div class="col-sm-6 col-md-6">
    <h3 class="heading">Update Surveillance Site</h3>
    <form method="post" action={% url 'sites-update' place_id=site.id %}>
        {% csrf_token %}
        <div class="formSep">
            <div class="row">
                <div class="col-sm6 col-md-6">
                <label for="id_name">Site Name:<span class="f_req">*</span></label>
                    {{ form.name }}
                <span class="help-block">What is the site name?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_lga">LGA:<span class="f_req">*</span></label>
                    {{ form.lga }}
                <span class="help-block">What is the LGA?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_type">Site Type:<span class="f_req">*</span></label>
                    {{ form.site_type }}
                <span class="help-block">What type of site is this?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_priority">Site Priority:<span class="f_req">*</span></label>
                    {{ form.site_priority }}
                <span class="help-block">What is the priority of this site?</span>
                </div>
                <div class="col-sm-6 col-md-6">
                    <label for="id_site_category">Site Category:<span class="f_req">*</span></label>
                    {{ form.site_category }}
                <span class="help-block">What category should the site be in?</span>
                </div>
            </div>
        </div>
        <div class="row">
            <input class="btn btn-default" type="submit" value="Save" />
            <a class="btn btn-default" href={% url "sites-index" %}>Cancel</a>
        </div>
    </form>
</div>
</div>
{{ form.errors }}
{% endblock %}
{% block sidebar %}
    {% include "afp-sidebar.html" %}
{% endblock %}

我的观点:

@login_required
def sites_update_view(request, place_id=None):
    if place_id:
        site = get_object_or_404(SiteMeta, pk=place_id)
    else:
        return redirect('sites-index')

    if request.GET:
        form = SitesAddForm(instance=site)

    if request.POST:
        form = SitesAddForm(request.POST, instance=site)
        if form.is_valid():
            form.save()
            return redirect('sites-index')
    else:
        form = SitesAddForm()

    return render(request, 'sites-update.html', {
        'form': form,
        'place_id': place_id
    })

我的网址:

#!/usr/bin/env python
# encoding=utf-8

# afp.urls

from django.conf.urls import patterns, url
from .views import (afp_index_view, personnel_index_view, sites_index_view, afp_messaging_view, afp_weekly_create_view,
                    case_report_create_view, weekly_reports_list, case_report_list, personnel_add_view, sites_add_view,
                    afp_nonreporting_site_list, sites_update_view)

urlpatterns = patterns('',
    # ex: /afp/
    url(r'^$', afp_index_view, name='afp-index'),
    url(r'^personnel/$', personnel_index_view, name='personnel-index'),
    url(r'^personnel/add/$', personnel_add_view, name='personnel-add'),
    url(r'^sites/$', sites_index_view, name='sites-index'),
    url(r'^sites/add/$', sites_add_view, name='sites-add'),
    url(r'^sites/update/(?P<place_id>[-\d]+)/$', sites_update_view, name='sites-update'),
    url(r'^afp-messaging/$', afp_messaging_view, name='afp-messaging'),
    url(r'^weekly-reports-list', weekly_reports_list, name='weekly-reports-list'),
    url(r'^weekly/add/$', afp_weekly_create_view, name="afp-weekly-add"),
    url(r'^weekly/add/(?P<msgid>[-\d]+)/$', afp_weekly_create_view, name="afp-weekly-add-from-msg"),
    url(r'^case-report/add/(?P<msgid>[-\d]+)/$', case_report_create_view, name='case-report-add-from-msg'),
    url(r'^case-report-list', case_report_list, name='case-report-list'),
    url(r'^sites-not-reporting/$', afp_nonreporting_site_list, name='afp-sites-not-reporting'),
    )

1 个答案:

答案 0 :(得分:1)

模板中触发错误的行包括:

{% url 'sites-update' place_id=site.id %}

但是您没有将任何名为"site"的内容传递给您的模板:仅formplace_id。你可以直接使用place_id,但你可能想要将网站传递给模板。