Reputation: 5578
I recently started Django and I'm having a problem rendering a "hello world" page, the issue goes like this :
Could not import 'index'. The path must be fully qualified.
Django is not detecting my views.index function
here is the code:
src/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^pages/', include('pages.urls')),
]
pages/urls.py
from django.conf.urls import url
urlpatterns = [
url(r'', 'index', name="index"), #error
]
pages/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render('index.html', context)
I want to render the index.html, how can I do this?
--------------- UPDATE ---------------
CMD error
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044D70D0>
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\src\urls.py", line 26, in <module>
url(r'^pages/', include('pages.urls')),
File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\pages\urls.py", line 5, in <module>
url(r'^$', views.index, name="index"),
AttributeError: 'module' object has no attribute 'index'
added from pages import views
in urls.py
Upvotes: 2
Views: 3185
Reputation: 309009
In Django 1.8+, you should import the view and include the callable in your URL patterns.
from .views import index
urlpatterns = [
...
url(r'^$', index, name="index"),
Note that the second argument is now index
, the view we have imported, instead of the string 'index'
. I have also changed the regex so that it only matches the index page. Using ''
would match every URL.
Another option would be to import the views
module instead of the individual view. This will make it easier to include other views e.g. views.about
or views.contact
.
from . import views
urlpatterns = [
...
url(r'^$', views.index, name="index"),
Upvotes: 9