Django tutorial, Getting: TypeError at /admin/ argument to reversed() must be a sequence
Asked Answered
O

1

6

I'm working my way through the django tutorial for version 1.8 and I am getting an error that I am stuck on and can't seem to figure out. I thought I was following the tutorial pretty much to the T.

I have the following tree set up:

. ├── dj_project │   ├── __init__.py │   ├── __init__.pyc │   ├── settings.py │   ├── settings.pyc │   ├── urls.py │   ├── urls.pyc │   ├── wsgi.py │   └── wsgi.pyc ├── manage.py └── polls ├── admin.py ├── admin.pyc ├── __init__.py ├── __init__.pyc ├── migrations │   ├── 0001_initial.py │   ├── 0001_initial.pyc │   ├── __init__.py │   └── __init__.pyc ├── models.py ├── models.pyc ├── tests.py ├── urls.py ├── urls.pyc ├── views.py └── views.pyc

and have, just as in the tutorial for polls/urls.py:

from django.conf.urls import url

from . import views

urlpatterns = {
    url(r'^$', views.index, name='index'),
}

and for my dj_project/urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [

    url(r'^polls/', include('polls.urls')), 
    url(r'^admin/', include(admin.site.urls)),

]

and in polls/views.py i have:

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("is there something here?")

so when I go to <mysite>/polls I see "is there something here", but if I go to <mysite>/admin, I get the error: TypeError at /admin/ argument to reversed() must be a sequence. If I remove polls from urlpatterns in dj_project/urls.py, the admin comes in fine.

What might be the problem? I can't seem to figure it out.

Orderly answered 14/8, 2015 at 16:49 Comment(1)
Are you trying to use reverse anywhere?Ore
C
27

In the polls/urls.py file, you are declaring the urlpatterns as dict, it must be a list.

change the

urlpatterns = {
    url(r'^$', views.index, name='index'),
}

to:

urlpatterns = [
    url(r'^$', views.index, name='index'),
]
Canaster answered 14/8, 2015 at 17:4 Comment(5)
Can it be a set? Or could you declare a set named urlpatterns, then change it to a list?Scrag
I'm not sure if you really want to convert the urlpatterns to set or just misunderstood the it must be set as a list part of my answer. Can you be more specific?Canaster
Like, if I were to want to have a url for every object in a database, would I be able to convert it to a set, to delete possible duplicates, then back into a list?Scrag
You can convert the urlpatterns to set to remove duplicates and change back to a list. But i see no significance doing it, because urlpatterns are generally written manually. Why would you create urls for each and very row/object in the db. why not use a simple regex?Canaster
Fun Fact: If you define a dict (which would be wrong) and process it with format_suffix_patterns() it will be okay - the function does not seem to mind and converts it properly. I went and copied a bit of code with dict and function for my new app, then removed format_suffix_patterns and the whole thing went boom.Burtis

© 2022 - 2024 — McMap. All rights reserved.