我正在尝试构建一个e_commerc django应用程序,但我遇到了一个错误,这有点令人困惑。 我的错误是:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
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
我不明白这个错误,或者更好地说我看不到这个错误。这是我的views.py和forms.py。他怎么不能导入contactForm? 有人可以解释我怎样才能克服这一点。 保护你。
答案 0 :(得分:1)
我认为你在forms.py中犯了一个错误。
请确保您已正确拼写contactForm
功能。在contactForm
中,F
必须大写。
class contactForm(forms.Form):
答案 1 :(得分:0)
您正在尝试在模板中使用contactForm
,但您没有通过它。所以在你看来,
def contact(request):
form = contactForm(request.POST or None)
if form.is_valid():
print request.POST
#remember save your form.
context = {'contactForm':form}
template = 'contact.html'
return render(request, template, context)
在模板中,您可以使用
{% extends "base.html" %}
{% block content %}
<h1>Contact</h1>
<form method="POST" action="" > {% csrf_token %}
{{ contactForm.as_p }}
<input type="submit" value="submit form" class="btn btn-default" />
</form>
{% endblock %}