NoReverseMatch at / accounts / detail

时间:2017-10-01 12:12:19

标签: python html django

我收到错误,NoReverseMatch at / accounts / detail 反向上传'参数'()'和关键字参数' {}'未找到。尝试了1种模式:[u' accounts / upload /(?P \ d +)/ $']。 我在detail.html中写过

<body>
   <a href="{% url 'accounts:upload' %}"><img src="{% static 'accounts/Send.jpg' %}" alt="SEND"></a>
</body>

我的理想系统是当我点击图片时,它发送到上传方法(也许我应该说photo.html)。 我在views.py中写过

def detail(request):
    return render(request, 'registration/accounts/detail.html')

def upload(request, p_id):
    form = UserImageForm(request.POST or None)
    d = {
        'p_id': p_id,
        'form':form,
    }
    return render(request, 'registration/accounts/photo.html', d)
在urls.py中

urlpatterns = [
    url(r'^detail$', views.detail,name='detail'),
    url(r'^photo/$', views.photo, name='photo'),
    url(r'^upload/(?P<p_id>\d+)/$', views.upload, name='upload'),
]

当我访问http://localhost:8000/accounts/detail时,会发生这些错误。我该如何解决这个问题?我该怎么写呢?

1 个答案:

答案 0 :(得分:2)

urls.py中,您的上传网址需要参数p_id

url(r'^upload/(?P<p_id>\d+)/$', views.upload, name='upload')
#                 ^^^

因此,当反向访问此网址时,您必须指定p_id的值:

your_url = reverse('accounts:upload', p_id=123)

或者,在您的模板中:

<a href="{% url 'accounts:upload' 123 %}">Click me</a>