0

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

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 并根据它,蛞蝓信息在那里(除非我读错了):

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

) ;

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

4

2 回答 2

0

Syncdb don't change the existing schema it just looks for new table schema which are not present and create them. I will suggest using of South to keep track of all the database changes, it is very useful. One more thing to note, if your any app is under south inspection then your new tables also will be created with south migrations(syncdb won't work for those apps)

For Ex:

./manage.py convert_to_south appname

Do changes then

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

For more help you can check here:

South Help

于 2013-06-18T11:10:14.700 回答
0

Here is the solution according to Matthew Daly:

The problem is that running syncdb can create new tables, but cannot amend existing ones. As a result, the slug column isn't added.

The simplest solution is to just delete backend.db and run python manage.py syncdb again to rebuild it using the new schema.

A more sophisticated solution is to use South to create and manage database migrations. With South, you can easily migrate databases to a new structure, or roll them back. It's out of scope for this tutorial, but I would recommend having at look at http://www.jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/ to give you a good idea of how to start using South.

于 2013-06-14T12:56:49.173 回答