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

时间:2018-06-21 08:54:27

标签: python django

这是我的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')),
]

和我的customer.urls

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使用反向网址

{% 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

这是我的customerMember视图:

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})

我的代码有什么问题?

2 个答案:

答案 0 :(得分:2)

问题似乎出在您未在问题中显示的其他网址格式之一。

检查您包括的所有urls.py。它们都应该是列表,

urlpatterns = [
    ...
]

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

urlpatterns = {
    ...
}

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

答案 1 :(得分:-1)

您正在为模板<a href="{% url 'customers:customerMember' company.id %}">中的反向使用显式名称空间,但没有在urls文件中定义名称空间;您需要按如下所示添加它:

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