django URL Reverse:reversed()的参数必须是一个序列

问题描述 投票:1回答:2

这是我的urls.py conf

from django.conf.urls import include, url

from django.contrib import admin

from home import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index),
    url(r'^home/', include('home.urls')),
    url(r'^setting/', include('setting.urls')),
    url(r'^customer/', include('customers.urls')),
    url(r'^material/', include('materials.urls')),
    url(r'^order/', include('orders.urls')),
    url(r'^partner/', include('partner.urls')),
]

和我的customers.urls conf

from django.conf.urls import url


from customers import views

app_name = 'customers'

urlpatterns = [
    url(r'^list', views.customerList, name='customerList'),
    url(r'^create', views.createCustomer, name='createCustomer'),
    url(r'^remmove', views.lockCustomer, name='removeCustomer'),
    url(r'^update', views.updateCustomer, name='updateCustomer'),
    url(r'^detail/(?P<id>\S+)/$', views.customerDetail, name='customerDetail'),
    url(r'^member/(?P<customer_id>\S+)/$', views.customerMember, name='customerMember'),
]

和我的template.html使用url反向

{% for company in customers %}
                                <tr>
                                    <td>
                                        <a href="{% url 'customers:customerMember' company.id %}">
                                            <span style="color:#08c; font-weight: bold;">{{ company.name }}</span>
                                        </a>
                                    </td>
                                    <td>
                                        {{ company.region_id }}
                                    </td>
                                    <td>
                                        {{ company.register_time }}
                                    </td>
                                    <td>
                                        {% if current_time > company.due_time %}
                                            <span class="text-center text-danger">Expired</span>
                                        {% else %}
                                            <span class="text-center text-danger">{{ company.due_time }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ company.account_limit }}
                                    </td>
                                    <td>
                                        {{ company.isdelete }}
                                    </td>
                                </tr>
                            {% endfor %}

当我访问http://localhost:8000/customer/list页面时,它给了我一个TypeError

TypeError at /customer/list/

argument to reversed() must be a sequence
Request Method: GET
Request URL:    http://127.0.0.1:8000/customer/list/
Django Version: 1.10.3
Exception Type: TypeError
Exception Value:    
argument to reversed() must be a sequence
Exception Location: C:\Python\lib\site-packages\django\urls\resolvers.py in _populate, line 196
Python Executable:  D:\Code\Python\CXJ\venv\Scripts\python.exe
Python Version: 3.5.4
Python Path:    
['D:\\Code\\Python\\SHCXJ\\apps',
'D:\\Code\\Python\\SHCXJ',
'D:\\Code\\Python\\CXJ\\venv\\Scripts\\python35.zip',
'C:\\Python\\DLLs',
'C:\\Python\\lib',
'C:\\Python',
'D:\\Code\\Python\\CXJ\\venv',
'D:\\Code\\Python\\CXJ\\venv\\lib\\site-packages',
'C:\\Users\\Tony\\AppData\\Roaming\\Python\\Python35\\site-packages',
'C:\\Python\\lib\\site-packages']

In template D:\Code\Python\SHCXJ\templates\customers\list.html, error at line 78

这是我的客户会员观点:

def customerList(request):
    current_time = datetime.datetime.now()
    name = request.GET.get('name', None)
    if name is None:
        company_list = Company.objects.filter(isdelete=0).order_by('due_time').all()
    else:
        company_list = Company.objects.filter(name__contains=name).order_by('due_time').all()
    return render(request, 'customers/list.html', {'customers': company_list, 'current_time': current_time})

我的代码出了什么问题?

python django
2个回答
2
投票

看起来问题出在您的其他网址模式中,而您未在问题中显示。

检查您包含的所有urls.py。他们都应该是名单,

urlpatterns = [
    ...
]

但看起来好像你在某个地方使用了一套:

urlpatterns = {
    ...
}

为了帮助找到导致问题的urls.py,您可以尝试逐个注释掉include()网址格式。如果注释掉包含停止argument to reversed() must be a sequence错误,那么你发现了导致问题的urls.py。请注意,注释掉包含这样的内容并不总是很容易 - 根据您的模板,它可能会导致其他{% url %}标记失败。


-1
投票

您在模板<a href="{% url 'customers:customerMember' company.id %}">中使用了显式命名空间,但是您没有在urls文件中定义命名空间。你需要添加如下:

url(r'^customer/', include('customers.urls'), namespace='customers'),

© www.soinside.com 2019 - 2024. All rights reserved.