/ admin / blogengine / post / add /中的DatabaseError

时间:2013-06-13 18:39:27

标签: database django-admin

新手在这里。我正在尝试使用Django创建一个博客。在我添加一个slu to帖子之前,一切正常/发布。但是,由于我添加了slug,当我尝试在/ admin / blogengine / post / add /中添加一条名为DatabaseError的帖子时,我收到错误消息 没有这样的专栏:blogengine_post.slug。错误回溯是:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin

Django Version: 1.5.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blogengine',
 'django.contrib.admin')
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')


Template error:
In template /Users/chrissiepollock/projects/DjangoBlog/templates/posts.html, error at line 6
   no such column: blogengine_post.slug
   1 : <html>


   2 :     <head>


   3 :         <title>My Django Blog</title>


   4 :     </head>


   5 :     <body>


   6 :          {% for post in posts %} 


   7 :         <h1><a href="/{{ post.slug }}">{{ post.title }}</a></h1>


   8 :         <h3>{{ post.pub_date }}</h3>


   9 :         {{ post.text }}


   10 :         {% endfor %}


   11 :         <br />


   12 :         {% if page.has_previous %}


   13 :         <a href="/{{ page.previous_page_number }}/">Previous Page</a>


   14 :         {% endif %}


   15 :         {% if page.has_next %}


   16 :         <a href="/{{ page.next_page_number }}/">Next Page</a>


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/chrissiepollock/projects/DjangoBlog/blogengine/views.py" in getPost
  27.     return render_to_response('posts.html', { 'posts':post})
File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  29.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
  172.         return t.render(Context(dictionary))
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
  148.         len_values = len(values)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __len__
  90.                 self._result_cache = list(self.iterator())
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in iterator
  301.         for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  775.         for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.         cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py" in execute
  41.             return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  366.             six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  362.             return Database.Cursor.execute(self, query, params)

Exception Type: DatabaseError at /admin
Exception Value: no such column: blogengine_post.slug

我运行了manage.py sqlall myapp,根据它,slug信息就在那里(除非我读错了):

CREATE TABLE "blogengine_post" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"pub_date" datetime NOT NULL,
"text" text NOT NULL,
"slug" varchar(40) NOT NULL UNIQUE

) ;

我一直试图解决这个问题几天,从头开始逐行读取并重新定位文件和文件夹,但没有任何作用。有任何想法吗?非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

根据Matthew Daly,这是解决方案:

问题是运行syncdb可以创建新表,但不能修改现有表。因此,不添加slug柱。

最简单的解决方案是删除backend.db并再次运行python manage.py syncdb以使用新架构重建它。

更复杂的解决方案是使用South来创建和管理数据库迁移。使用South,您可以轻松地将数据库迁移到新结构,或者将其回滚。它超出了本教程的范围,但我建议您查看http://www.jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/,以便了解如何开始使用South。

答案 1 :(得分:0)

Syncdb不会更改现有架构,只会查找不存在的新表架构并创建它们。 我建议使用South来跟踪所有数据库更改,这非常有用。还有一点需要注意,如果您的任何应用程序都在南方检查之下,那么您的新表也将使用南迁移创建(syncdb将不适用于这些应用程序)

对于Ex:

./manage.py convert_to_south appname

然后进行更改

  ./manage.py schemamigration --auto app_name
./manage.py migrate app_name

如需更多帮助,请点击此处查看:

South Help