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.

1

1 Answer 1

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'),
]
5
  • Can it be a set? Or could you declare a set named urlpatterns, then change it to a list? Commented Oct 19, 2015 at 5:26
  • 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?
    – v1k45
    Commented Oct 19, 2015 at 9:09
  • 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? Commented Oct 19, 2015 at 11:15
  • 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?
    – v1k45
    Commented Oct 19, 2015 at 11:57
  • 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.
    – Chris
    Commented Sep 6, 2017 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.