Django:没有替补

时间:2018-04-13 11:06:48

标签: python django

您好,我不知道为什么会出现此错误。我无法看到问题,我尝试解决html中的网址。

NoReverseMatch at /admin/product_list/20/edit/

 Reverse for 'edit_product' with keyword arguments '{u'product_id': ''}' not found. 1 pattern(s) tried: ['admin/product_list/(?P<product_id>\\d+)/edit/$']

Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/product_list/20/edit/
Django Version:     1.11.11
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'edit_product' with keyword arguments '{u'product_id': ''}' not found. 1 pattern(s) tried: ['admin/product_list/(?P<product_id>\\d+)/edit/$']



Exception Location:     /usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
  Python Executable:    /usr/bin/python
  Python Version:   2.7.12

我的网址:

url(r'^admin/product_list/(?P<product_id>\d+)/edit/$', views.admin_zone_edit_product, name='edit_product'),

我的HTML:

{% extends 'admin/baseadmin.html' %} 
{% load staticfiles %}
{% block content %}
{% csrf_token %}
<div ><form id="login" action="{% url 'edit_product' product_id=product.id %}" method ="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save"></input>

  </form></div>
   {% endblock %}  

这是视图,我猜是正确的。用于编辑它的视图搜索数据。使用表格,但我不相信是问题。

def admin_zone_edit_product(request,product_id):
    product = Product.objects.get(id=product_id)
    if request.method =='POST':
        form = ProductEditForm(product,request.POST)
        if form.is_valid():
            un = form.cleaned_data['name']
            des = form.cleaned_data['description']
            pr = form.cleaned_data['price']
            ca = form.cleaned_data['category']
            if un:
                product.name = un
            if des:
                product.description = des
            if pr:
                product.price = pr
            if ca:
                product.category = ca


            product.save()

            return redirect('product_list')
    else:
        form = ProductEditForm(product)
    return render(request,'admin/admin_zone_edit_product.html',{'form': form })

2 个答案:

答案 0 :(得分:0)

你不需要网址实际发布在同一页面

只需使用此

{% extends 'admin/baseadmin.html' %} 
{% load staticfiles %}
{% block content %}
{% csrf_token %}
<div ><form id="login" action="" method ="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save"></input>

  </form></div>
   {% endblock %}  

答案 1 :(得分:0)

据我所知,您没有将product传递给模板。试试这个:

return render(request,'admin/admin_zone_edit_product.html',{'form': form, 'product': product })