1

我正在创建一个只有基本 python 和 taggit 模块的基本博客,它驻留在http://127.0.0.1:8000/ (必须在其中放置随机空格以便我可以提交),当我运行./manage.py syncdb它时它正确执行并且当我runserver没有返回错误时。问题是当我实际访问该网站时。比这发生...

SyntaxError at /
invalid syntax (admin.py, line 4)

这显示在网络浏览器中..

这是回溯:

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)

无论如何,任何可以提供的帮助都会很棒!非常感谢你们!!

4

1 回答 1

2

你的第 4 行admin.py

from admin.site.register(Post)

这是一个 Python 语法错误。应该很简单

admin.site.register(Post)

它调用适当的方法来向 Django 的管理站点注册您的 Post 类。

顺便说一句,fromPython 中的关键字用于将某些内容导入当前模块。你应该确保你有线路

from django.contrib import admin

在 admin.py 的第 1 行到第 3 行的某个地方(可能是第 1 行!),以确保admin在您到达第 4 行时正确定义。

于 2012-08-06T02:40:17.030 回答