2

I'm new to Django, have been doing several tutorials to get really comfortable with the structuring, and am now running through the official tutorial.

I've created an polls App, which has a polls/views.py file as follows:

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


# Create your views here.
def index(request):
    return HttpResponse("Hello, World. You're at the polls index.")

I've also created an App URLconf file polls/urls.py with the following code:

from django.conf.urls import url

from . import views

url_patterns = [
    url(r'^$', views.index, name='index'),
]

This is pretty much exactly as done in the Django tutorial.

My issue is when I'm specifying url routes in the main projectname/url.py file on a project level as such:

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

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

When doing this, I get the following error:

django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from 'ProjectFolder\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

This is how the official Django tutorial dictates it to be done. However, if I explicity import the polls/views.py file from the app, I can accomplish the task as follows:

from django.conf.urls import url
from django.contrib import admin
from polls import views


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^polls/', views.index),

]

My immediate concern is the import of every app/urls file I ever create being necessitated by this approach, as well as the obvious divergence from the official Django instruction.

I hesitated to even ask this question because I feel that such a fundamental issue has bound to have an easy fix. Any help would be greatly appreciated.

To clarify, I can get around the error by explicitly importing the view files from apps. Whenever using the Django documentation-described approach of using the include() function I receive the error. I can appreciate the value of this function, and would like to know why is giving me the error described above.

alphazwest
  • 3,483
  • 1
  • 27
  • 39

1 Answers1

3

Just writte urlpatterns = [ .. and not url_patterns in your poll.views.py.

Bestasttung
  • 2,388
  • 4
  • 22
  • 34
  • the `url_patterns` list is explained by the Django documentation as belonging in the `poll.urls.py` file, just as it belongs in the `mainprojectname/urls.py` file. Even if you are referring to the `urls.py` files, I'm still not sure what you are describing. – alphazwest Sep 20 '17 at 22:13
  • can you link me that tutorial? From the official document i see : ```urlpatterns = [ url(r'^$', views.index, name='index'), ]``` and not ```url_patterns``` : https://docs.djangoproject.com/en/1.11/intro/tutorial01/#write-your-first-view – Bestasttung Sep 20 '17 at 22:15
  • I'm just saying you misspelled the variable name ```urlpatterns``` – Bestasttung Sep 20 '17 at 22:19
  • No problem, that happens sometime :) – Bestasttung Sep 20 '17 at 22:22