ImportError

54 views
Skip to first unread message

Petar Pilipovic

unread,
Feb 17, 2015, 12:36:04 AM2/17/15
to django...@googlegroups.com
Hello all, I am trying to build and e_commerc django app, but I have encounter an confusing error.
Mine Error is:
Environment:


Request Method: GET

Django Version: 1.7.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'profiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Template error:
In template /home/petarp/Documents/Udemy/Learn_Django/static/templates/navbar.html, error at line 15
   cannot import name contactForm
   5 :           <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">


   6 :             <span class="sr-only">Toggle navigation</span>


   7 :             <span class="icon-bar"></span>


   8 :             <span class="icon-bar"></span>


   9 :             <span class="icon-bar"></span>


   10 :           </button>


   11 :           <a class="navbar-brand" href="#">Bootstrap theme</a>


   12 :         </div>


   13 :         <div id="navbar" class="navbar-collapse collapse">


   14 :           <ul class="nav navbar-nav">


   15 :             {% url 'home' as home %} 


   16 :            {% url 'about' as about %}


   17 :            {% url 'contact' as contact %}


   18 :             <li {% if request.path == home %} class="active" {%endif%}><a href="{% url 'home' %}">Home</a></li>


   19 :             <li {% if request.path == about %} class="active" {%endif%}><a href="{% url 'about' %}">About</a></li>


   20 :             <li {% if request.path == contact %} class="active" {%endif%}><a href="{% url 'contact' %}">Contact</a></li>


   21 :             <li class="dropdown">


   22 :               <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>


   23 :               <ul class="dropdown-menu" role="menu">


   24 :                 <li><a href="#">Action</a></li>


   25 :                 <li><a href="#">Another action</a></li>


Traceback:
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/petarp/Documents/Udemy/Learn_Django/source/profiles/views.py" in home
  7.     return render(request, tempalte, context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/shortcuts.py" in render
  50.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  178.         return t.render(context_instance)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  150.                 return template.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  444.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  551.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  415.             self._populate()
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
  303.                 lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback
  231.         self._callback = get_callable(self._callback_str)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/utils/lru_cache.py" in wrapper
  101.                     result = user_function(*args, **kwds)
File "/home/petarp/.virtualenvs/e_udemy/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
  97.             mod = import_module(mod_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/home/petarp/Documents/Udemy/Learn_Django/source/contact/views.py" in <module>
  3. from .forms import contactForm

Exception Type: ImportError at /
Exception Value: cannot import name contactForm

 I do not understand this error, or better to say I can not see the error,  how come he can not import 'contactForm'. Mine views.py and mine forms.py
Can you help me understand and overcome this.
Tank you.
 

aRkadeFR

unread,
Feb 17, 2015, 6:43:35 AM2/17/15
to django...@googlegroups.com
Hello :)

Can you dump us the layout of your files?
Where is your forms.py?

Thanks
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/819c2889-37f9-4fbd-a422-1c18a7177865%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Petar Pilipovic

unread,
Feb 17, 2015, 7:20:30 AM2/17/15
to django...@googlegroups.com
Hello, I can not do it wright now I am not at home.
As for mine forms.py, it is in mine contact app. I have two apps in mine root project, one off them is contact.
I will send you more info as soon as posible.
Tank you.
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/a9iraquxOHU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54E2FFDB.5010207%40arkade.info.

Petar Pilipovic

unread,
Feb 17, 2015, 7:30:58 AM2/17/15
to django...@googlegroups.com
Maybea this can help you, a bref info :
I made a contact app, I have defined app url in mine url root project, then i made a forms.py file, i have defined form in it, then i have made contact.html where I have defined contact.html form which mine app will render, I have then go to views.py an imported contactForm class.
Afther that I have defined connection between  contact.html page and navbar.html, collected static, run server and bam error occur.
I just can not explain to mine self how come he can not import contactForm, and how com he can not render havbar.html, error pop's out line 15.

aRkadeFR

unread,
Feb 17, 2015, 8:57:46 AM2/17/15
to django...@googlegroups.com
Two different problems here.

For your import of contactForm, please try the relative import like:
from contact.forms import contactForm
> contact is your app name

try the (absolute_import)[1] too if you're python2.

[1] https://docs.python.org/2/library/__future__.html

Let us know if this works

Petar Pilipovic

unread,
Feb 18, 2015, 12:02:54 AM2/18/15
to django...@googlegroups.com, con...@arkade.info
Hello aRkadeFR.
I have solved mine problem, I was working whit Python 2.X here, and like you recommended I have done some  absolute import.
from forms import contactForm ,  and that solved it.
There was one other error, mine base.html did not render mine block content, because I did not defined it, I did that and now mine form is working.
Tank you.
Best.
Petar...;-)

Petar Pilipovic

unread,
Feb 18, 2015, 1:17:12 AM2/18/15
to django...@googlegroups.com, con...@arkade.info
aRkadeFR I have one other question regarding urls.py. I got this error when I am trying to go from contact or about to home page.


Using the URLconf defined in tryDjango.urls, Django tried these URL patterns, in this order:

  1. ^$ [name='home']
  2. ^about/$ [name='about']
  3. ^contact/$ [name='contact']
  4. ^admin/
  5. ^static\/(?P<path>.*)$
  6. ^media\/(?P<path>.*)$

The current URL, contact/{% url 'home' %}, didn't match any of these.


Now mine root/urls.py, and this is mine navbar.html, every other thing is connected properly, and I can go from home page to contact, about, but I can not go reverse.
Can you explain to me where is the error?
Tank you.

aRkadeFR

unread,
Feb 18, 2015, 6:46:22 AM2/18/15
to django...@googlegroups.com

On 02/18/2015 07:17 AM, Petar Pilipovic wrote:
aRkadeFR I have one other question regarding urls.py. I got this error when I am trying to go from contact or about to home page.


Using the URLconf defined in tryDjango.urls, Django tried these URL patterns, in this order:

  1. ^$ [name='home']
  2. ^about/$ [name='about']
  3. ^contact/$ [name='contact']
  4. ^admin/
  5. ^static\/(?P<path>.*)$
  6. ^media\/(?P<path>.*)$

The current URL, contact/{% url 'home' %}, didn't match any of these.


the first part of your list are regexes. The "$" means the end of the line.
So there's nothing after "contact/" ; you can't do "contact/xxxx".

A link in a template is as simple as:
<a href="{% url "name_of_the_view" %}>Link</a>
For your home page:
<a href="{% url "home" %}>Link</a>

Petar Pilipovic

unread,
Feb 18, 2015, 7:29:51 AM2/18/15
to django...@googlegroups.com
Ok, I will work on it as soon as posible, tnx for your help.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54E44FCB.1070500%40arkade.info.
Reply all
Reply to author
Forward
0 new messages