Jason Lee
Jason Lee

Reputation: 77

django URL Reverse : argument to reversed() must be a sequence

This is my 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')),
]

and my 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'),
]

and my template.html to use url reverse

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

when I visit http://localhost:8000/customer/list page, It gives me a 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

here is my customerMember view:

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

What is wrong with my code?

Upvotes: 1

Views: 527

Answers (2)

Alasdair
Alasdair

Reputation: 309089

It looks like the problem is in one of your other url patterns that you haven't shown in your question.

Check all of urls.py that you include. They should all be lists,

urlpatterns = [
    ...
]

but it looks as if you are using a set somewhere:

urlpatterns = {
    ...
}

To help find the urls.py that is causing the issue, you could try commenting out the include() URL patterns one by one. If commenting out the include stops the argument to reversed() must be a sequence error, then you've found a urls.py that is causing a problem. Note that it isn't always easy to comment out includes like this - depending on your template, it might cause other {% url %} tags to fail.

Upvotes: 2

umgelurgel
umgelurgel

Reputation: 312

You're using an explicit namespace for the reverse in your template <a href="{% url 'customers:customerMember' company.id %}"> but you're not defining the namespace in your urls file; you need to add it as follows:

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

Upvotes: -1

Related Questions