2

I'm started learning python-django and I would like create my first django app. I'm following the Django 1.8 tutorial. In my project "mysite", there is a source folder "polls". In the folder there is "views.py" module where a "index" function is defined. And there is a "urls.py" file:

from django.conf.urls import url

from . import views

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

Now i'm getting an error:

Traceback (most recent call last):
 File "/home/ukasz/Pulpit/Python/mysite/polls/urls.py", line 3, in <module>
from . import views
SystemError: Parent module '' not loaded, cannot perform relative import

Edit: Updated with new information provided by woljako in an answer

my mysite/ursl.py:

 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),
 ]

and I'm getting an error:

Traceback (most recent call last):
  File "/home/ukasz/Pulpit/Python/mysite/mysite/urls.py", line 20, in <module>
  url(r'^polls/', include('polls.urls')),
  File "/usr/local/lib/python3.5/dist-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
 File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'polls'

I added to INSTALLED_APPS app 'polls', but still it didn't help

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
]
woljako
  • 39
  • 1
  • 5

4 Answers4

2

There are a couple of things that can cause this. I have two possible solutions that are not mentioned yet.

  1. Try to replace url(r'^polls/', include('polls.urls')), with url(r'^polls/', include('mysite.polls.urls')),
  2. Have you remembered to include an __init__.py file inside the polls folder? This is required for the import to work. See the docs for reference. Simply create an empty file with the name __init__.py. Note the double underscores.

Please let me know if this solves you problems.

Martin Hallén
  • 1,492
  • 1
  • 12
  • 27
  • ehhh, another problem appears, current error: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. – woljako Jun 15 '16 at 16:57
  • I think this solves the problem: http://stackoverflow.com/questions/15556499/django-db-settings-improperly-configured-error – Martin Hallén Jun 15 '16 at 16:59
  • I have just started learning django and I don't know how to use first option which is the easiest, I write python manage.py shell in console and what is the next step? – woljako Jun 15 '16 at 17:24
0

Try to register polls in your applications in settings.py. Something like this: enter image description here

Gichana
  • 3
  • 4
0

In Polls/urls.py add the this line at top

from django.contrib import admin

And replace the from . import views with from polls import views. I hope this will work.

mosc9575
  • 5,618
  • 2
  • 9
  • 32
sai rohith
  • 36
  • 5
0

In Polls/urls.py Add the this line at top "from django.contrib import admin" And "from polls import views" I hope this will work.

sai rohith
  • 36
  • 5