我是Django的新手并尝试创建一个简单的博客,但语法错误仍然出现在Context行的views.py文件中。我使用Django 1.6,语法似乎与此版本兼容。 这是来自views.py的简单方法,我得到错误:
def archive(request):
posts = blogPost.objects.all()
t = loader.get_template("archive.html")
c = Context({'posts': })
return HttpResponse(t.render(c))
这是追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/blog
Django Version: 1.6
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
90. response = middleware_method(request)
File "C:\Python27\lib\site-packages\django\middleware\common.py" in process_request
71. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
573. resolve(path, urlconf)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
453. return get_resolver(urlconf).resolve(path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "C:\Python27\Lib\site-packages\django\bin\myblog\myblog\urls.py" in <module>
12. url(r'^blog/', include('blog.urls')),
File "C:\Python27\lib\site-packages\django\conf\urls\__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "C:\Python27\Lib\site-packages\django\bin\myblog\blog\urls.py" in <module>
2. from blog.views import archive
Exception Type: SyntaxError at /blog
Exception Value: invalid syntax (views.py, line 14)
答案 0 :(得分:1)
此行无效:
c = Context({'posts': })
那里需要有一个价值:
c = Context({'posts': posts})