0

制作博客、数据库等。事情是,我有一个名为“帖子”的数据库。

'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'posts',                      # Or path to database file if using sqlite3.
    'USER': '*******',                      # Not used with sqlite3.
    'PASSWORD': '*******',                  # Not used with sqlite3.
    'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '****',                      # Set to empty string for default. Not used with sqlite3.
}

模型.py

from django.db import models


class posts(models.Model):
    author = models.CharField(max_length = 30)
    title = models.CharField(max_length = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

视图.py

from django.shortcuts import render_to_response
from models import posts 



#from settings import default
def index(request):
    entries = posts.objects.all()[:10]
    return render_to_response('homepage/index.html', {'posts' : entries}

输出

OperationalError at /
(1049, "Unknown database 'posts.homepage_posts'")
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.4
Exception Type: OperationalError
Exception Value:    
(1049, "Unknown database 'posts.homepage_posts'")
Exception Location: /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/MySQLdb/connections.py in __init__, line 187
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/thor/Sites/FirstBlog',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']
Server time:    Wed, 2 May 2012 19:56:53 -0500

它会自动为数据库名称添加后缀.homepage_posts,使其无法访问。请帮忙?

4

1 回答 1

0

django 自动生成表的默认命名约定是在表名前面加上<app_label>_
你显然在名为“主页”的应用程序中有这个模式/视图。这是有效的,不是你的问题。django 这样做是为了在不同应用程序之间命名您的各种表,并在混合 3rd 方应用程序时防止名称冲突。

此错误告诉您整个数据库不存在。它几乎 100% 意味着你没有告诉 django 从你的模型结构中填充你的数据库:

python manage.py syncdb

现在运行它,每次添加新应用程序或创建新模型时也运行它。它将生成您的数据库和表。

如果您真的关心,您还可以通过为模型设置元“db_table”字段来控制数据库表的实际名称:https ://docs.djangoproject.com/en/dev/ref/models/options/#db -桌子

class posts(models.Model):
    class Meta:
        db_table = 'foo_bar'

...但同样,这完全是一个单独的问题。

于 2012-05-03T01:32:53.853 回答