我正在创建一个只有基本python和taggit模块的基本博客,它位于http://127.0.0.1:8000/
(必须放入随机空格以便我可以提交)当我运行./manage.py syncdb
它正确执行时当我runserver
它没有返回任何错误。问题是当我真正访问该网站时。比这发生......
SyntaxError at /
invalid syntax (admin.py, line 4)
这将显示在webbrowser ..
中这是追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'taggit',
'MainBlog')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Users/zackbaker/MyBlog/MyBlog/urls.py" in <module>
5. admin.autodiscover()
File "/Library/Python/2.7/site-packages/django/contrib/admin/__init__.py" in autodiscover
29. import_module('%s.admin' % app)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
Exception Type: SyntaxError at /
Exception Value: invalid syntax (admin.py, line 4)
无论如何,任何可以提供的帮助都会很棒!非常感谢你们!
答案 0 :(得分:2)
admin.py
的第4行是
from admin.site.register(Post)
这是Python语法错误。它应该只是
admin.site.register(Post)
调用适当的方法向Django的管理站点注册你的Post类。
顺便提一下,Python中的from
关键字用于将某些内容导入当前模块。你应该确保你有一行
from django.contrib import admin
在admin.py第1行到第3行的某处(可能是第1行!),以确保在到达第4行时正确定义admin
。