4
votes

I'm new to django and in the process of learning. While doing some tutorial exercises I ran into some errors which I need help with.

Error: ImportError at /blog. No module named urls

The urls.py file under the app named 'blog' is

from django.conf.urls.defaults import patterns, include, url
from mysite.blog.views import archive

urlpatterns = patterns('',
    url(r'^$',archive),
                       )

The views.py file under the app named 'blog' is

# Create your views here.
from django.template import loader, Context
from django.http import HttpResponse
from mysite.blog.models import Blogpost

def archive(request):
    posts = BlogPost.objects.all()
    t = loader.get_template("archive.html")
    c = context({'posts': posts })
    return HttpResponse(t.render(c))

The urls.py file under the project 'mysite' is

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
url(r'^blog/', include('mysite.blog.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)

Traceback:

File "D:\Super Developer\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "D:\Super Developer\Python\lib\site-packages\django\middleware\common.py" in process_request
  67.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
  531.         resolve(path, urlconf)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  420.     return get_resolver(urlconf).resolve(path)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "D:\Super Developer\Python\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /blog
Exception Value: No module named urls

Structure of Project/App:

  1. mysite
    • manage.py
    • init.py
    • settings.py
    • urls.py
    • wsgi.py
    • blog
      • init.py
      • models.py
      • views.py
      • urls.py
      • tests.py
      • Templates - archive.html

Python path

['D:\Super Developer\Proj\mysite', 'C:\Windows\system32\python27.zip', 'D:\Super Developer\Python\DLLs', 'D:\Super Developer\Python\lib', 'D:\Super Developer\Python\lib\plat-win', 'D:\Super Developer\Python\lib\lib-tk', 'D:\Super Developer\Python', 'D:\Super Developer\Python\lib\site-packages']

8
Will you be providing the traceback as well?Ignacio Vazquez-Abrams
Traceback has been addedambi dextro
@stalk what do I do to fix it?ambi dextro
Please show us the contents of mysite.blog.urlsIzz ad-Din Ruhulessin
@Izzad-DinRuhulessin the contents are included aboveambi dextro

8 Answers

5
votes
url(r'^blog/', include('mysite.blog.urls')),

This may need to be changed to

url(r'^blog/', include('blog.urls')),

Note: no 'mysite' prefix.

1
votes

In blog's urls.py, you import

from django.conf.urls.defaults import patterns, include, url

But in project's urls.py, you use

from django.conf.urls import patterns, include, url

Is this indented? The latter seems to fail at least in my env.

1
votes

Check if you did the declaraion for media or static urls in your app url file using

url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
  ),

instead of in the application url file.

I had the same problem, changed this and I didn't have the problem anymore.

0
votes

Maybe you're missing a __init__.py file in your blog folder.

0
votes

I think patterns cannot have comma after last element:

urlpatterns = patterns('',
    url(r'^$',archive), <-- delete this comma
)

also try to run manage.py shell and import blog.urls, if there's not raised any exception in there

0
votes

Try giving this:

url(r'^blog/', include('mysite.urls')),
0
votes

A friend of mine was going through some django tutorial and got the same error. The mistake he made was in the file structures. In the new version of django, it creates two directory levels for example mysite/mysite and he tried putting the blog folder in mysite/blog instead of mysite/mysite/blog. After making that change, it all seemed to work well. Try to look at your structure and hope it helps.

0
votes

Looks like you are using Django 1.6 or newer. Due to some changes in the framework, this code wouldn't work. Use 1.5 or this code in blog's urls.py:

    from django.conf.urls import patterns, include, url
    from blog import views

    urlpatterns = patterns('',
        url(r'^$', views.archive),
    )

And this in views.py:

    from django.template import loader, Context
    from django.http import HttpResponse
    from blog import models 

    def archive(request):
        posts = models.BlogPost.objects.all()
        t = loader.get_template('archive.html')
        c = Context({ 'posts': posts })
        return HttpResponse(t.render(c))