当我尝试导航到我的news / views.py文件中使用此文章功能视图的页面时,它会出现此错误。我尝试调试视图,看起来模板和上下文都很好,所以我不确定为什么会发生这种情况。
新闻/ views.py
this
新闻/ urls.py
def article(request, a_slug):
a_slug = a_slug.strip('/')
articles = Article.objects.get(slug=a_slug)
recent = Article.objects.all()
if request.method == "POST":
form = NewsForm(request.POST)
if form.is_valid():
cleaned_data = NewsForm.cleaned_data
else:
form = NewsForm()
context = {'article': articles, 'recent': recent, 'form': form}
set_detail_context(request, context)
template = "overrides/news.html"
return render_to_response(template, context, context_instance=RequestContext(request))
新闻/ html.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^article/(?P<a_slug>.*)/?$', views.article, name='article')
]
引发异常的Django源代码
{% for article in articles %}
<div class="container">
<div class="row">
<div class="col-md-12 article">
<div class="col-md-5 col-xs-12">
<div class="article-image" style="background-image: url('{{ article.image }}');"></div>
</div>
<div class="col-md-7 col-xs-12" id="content">
<h1>{{ article.title }}</h1>
<p>{{ article.story|slice:"180" }}</p>
<a href="article/{{ article.slug }}">
<button class="article-btn">
READ MORE
</button>
</a>
</div>
</div>
</div>
</div>
{% endfor %}
Stacktrace:
current = context
try: # catch-all for silent variable failures
for bit in self.lookups:
try: # dictionary lookup
current = current[bit]
# ValueError/IndexError are for numpy.array lookup on
# numpy < 1.9 and 1.9+ respectively
except (TypeError, AttributeError, KeyError, ValueError, IndexError):
try: # attribute lookup
# Don't return class attributes if the class is the context:
if isinstance(current, BaseContext) and getattr(type(current), bit):
raise AttributeError
current = getattr(current, bit)
except (TypeError, AttributeError) as e:
# Reraise an AttributeError raised by a @property
if (isinstance(e, AttributeError) and
not isinstance(current, BaseContext) and bit in dir(current)):
raise
try: # list-index lookup
current = current[int(bit)]
except (IndexError, # list index out of range
ValueError, # invalid literal for int()
KeyError, # current is a dict without `int(bit)` key
TypeError): # unsubscriptable object
raise VariableDoesNotExist("Failed lookup for key "
"[%s] in %r",
(bit, current)) # missing attribute
inplace_edit.py
Request Method: GET
Request URL: http://localhost:8000/news/article/atlas-featured-in-retailing-today/
Django Version: 1.9.1
Python Version: 2.7.11
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'filebrowser',
'suit',
'django.contrib.admin',
'reversion',
'classytags',
'mptt',
'inplaceeditform',
'inplaceeditform_extra_fields',
'contact',
'home_content',
'navigation',
'page_content',
'webapp',
'webapp_admin',
'testimonials',
'products',
'services',
'news',
'clients',
'staff',
'careers')
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 /Users/timothybaney/Django_Projects/Atlas_Technology_Web/templates/overrides/news.html, error at line 0
(Could not get exception message) 1 : {% extends 'detail_page.html' %}
2 : {% load inplace_edit %}
3 : {% load static %}
4 :
5 : {% block extra-head %}
6 : {{ block.super }}
7 : <link type='text/css' rel='stylesheet' href='/static/css/news.css'>
8 : {% endblock %}
9 :
10 :
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/timothybaney/Django_Projects/Atlas_Technology_Web/news/views.py" in article
55. return render_to_response(template, context, context_instance=RequestContext(request))
File "/usr/local/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
45. using=using)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
114. template_name, context, context_instance, dirs, dictionary)
File "/usr/local/lib/python2.7/site-packages/django/template/engine.py" in render_to_string
244. return t.render(context_instance)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
206. return self._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
988. bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_annotated
955. return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
988. bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_annotated
955. return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
988. bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_annotated
955. return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
988. bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_annotated
955. return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
988. bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_annotated
955. return self.render(context)
File "/Users/timothybaney/Django_Projects/Atlas_Technology_Web/inplaceeditform/tag_utils.py" in render
104. context = self.prepare_context(args, kwargs, context)
File "/Users/timothybaney/Django_Projects/Atlas_Technology_Web/inplaceeditform/templatetags/inplace_edit.py" in prepare_context
99. obj = Variable(obj_context).resolve(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in resolve
846. value = self._resolve_lookup(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
901. (bit, current)) # missing attribute
Exception Type: VariableDoesNotExist at /news/article/atlas-featured-in-retailing-today/
Exception Value: Failed lookup for key [page] in u"[{'False': False, 'None': None, 'True': True}, {}, {}]"