woljako woljako - 5 months ago 101
Python Question

Writing your first Django app, error with from . import views

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

Answer

You can directly use

import views
Comments