Django反向给我带来了一些麻烦

时间:2016-09-06 21:33:59

标签: python django

我的问题是,当调用reverse时会抛出以下异常

使用参数'(3,)'和关键字参数'{}'找不到'/ documentation /'的反转。尝试了0种模式:[]

这是我的urls.py

url(r'^documentation/$', views.view1),
url(r'^documentation/([0-9])/$', views.documents, name='documentation'),

这里是我的views.py

def view1(request):

  if request.method == 'POST':
      profe = request.POST.get('value')

      a = value.encode('ascii', 'ignore')
      b = int(a)
      return HttpResponseRedirect(reverse('documentation', args=(b,)))

else:

    return render(request, "documentation.html", info)

def documents(request,valor):
      ...something...
      return render(request, "anotherdoc.html", ..something..)

这是我的模板(documentation.html)

...
<form method="POST" action="">
    {% csrf_token %}
    {% for p in ps %}
    <tr>
        <td><button id="boton1" button type="submit" name = "valor" value ="{{p.idp}}" class="btn btn-success btn-sm">{{p.nombre}} {{p.apellido}}</button>
        </td>
        <td>algo</a> </td>
    </tr>
    {%endfor%}
</form>

我想从反向()获取127.0.0.1:8000/documentation/1但是我很难尝试

由于

(使用追溯编辑)

Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Pa\De\Nueva carpeta\ag\Ag\scheduler\views.py" in documentation
  82.         url = reverse('documentation', args=(3,))

File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse
  600.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  508.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /documentation/
Exception Value: Reverse for 'documentation' with arguments '(3,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:-1)

反向使用网址名称,您将名称声明为'/documentation/',但我们可以看到您正在使用File "C:\Users\Pa\Desktop\Nueva carpeta\ag\Ag\scheduler\views.py" in documentation 82. url = reverse('/documentation/', args=(3,))

url = reverse('documentation', args=(3,))

将该行更改为'[namespace]:documentation'

如果您的网址是名称间隔的(如果您有其他urls.py,其中此网址包含()),您就会知道这一点,您应该使用0