在WAMP服务器上使用Django/Mezzanine时MySQL未创建表

0 投票
1 回答
664 浏览
提问于 2025-04-18 01:02

我创建了一些模型,具体在

models.py 文件里:

from django.db import models
from mezzanine.pages.models import Page

class Author(Page):
    dob = models.DateField("Date of birth")

然后我通过输入命令 easy_install South 安装了 south,接着又运行了 python manage.py createdbpython manage.py syncdbpython manage.py migrate 这些命令。虽然这些命令显示表格已经创建,但实际上数据库里并没有任何表。

这是我的 settings.py 文件:

from __future__ import absolute_import, unicode_literals


MANAGERS = ADMINS

ALLOWED_HOSTS = ['example.com' ]

TIME_ZONE = "America/Vancouver"
USE_TZ = True


LANGUAGE_CODE = "en"

_ = lambda s: s
LANGUAGES = (
    ('en', _('English')),
)


DEBUG = False


SESSION_EXPIRE_AT_BROWSER_CLOSE = True

SITE_ID = 1


USE_I18N = False

INTERNAL_IPS = ("127.0.0.1",)


TEMPLATE_LOADERS = (
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
)

AUTHENTICATION_BACKENDS = ("mezzanine.core.auth_backends.MezzanineBackend",)


STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)


DATABASES = {
    "default": {
        # Add "postgresql_psycopg2", "mysql", "sqlite3" or "oracle".
        "ENGINE": "django.db.backends.mysql",
        # DB name or path to database file if using sqlite3.
        "NAME": "abc",
        # Not used with sqlite3.
        "USER": "root",
        # Not used with sqlite3.
        "PASSWORD": "123",
        # Set to empty string for localhost. Not used with sqlite3.
        "HOST": "",
        # Set to empty string for default. Not used with sqlite3.
        "PORT": "",
    }
}



import os

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

PROJECT_DIRNAME = PROJECT_ROOT.split(os.sep)[-1]



STATIC_URL = "/static/"

STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))


MEDIA_URL = STATIC_URL + "media/"



# Package/module name to import the root urlpatterns from for the project.
ROOT_URLCONF = "%s.urls" % PROJECT_DIRNAME

TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "templates"),)

ACCOUNTS_VERIFICATION_REQUIRED = True
INSTALLED_APPS = (
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.redirects",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.sitemaps",
    "django.contrib.staticfiles",
    "mezzanine.boot",
    "mezzanine.conf",
    "mezzanine.core",
    "mezzanine.generic",
    "mezzanine.blog",
    "mezzanine.forms",
    "mezzanine.pages",
    "mezzanine.galleries",
    "mezzanine.twitter",
    "mezzanine.accounts",
    "project_name",
    "south"
    #"mezzanine.mobile",
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.static",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.core.context_processors.tz",
    "mezzanine.conf.context_processors.settings",
)


MIDDLEWARE_CLASSES = (
    "mezzanine.core.middleware.UpdateCacheMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "mezzanine.core.request.CurrentRequestMiddleware",
    "mezzanine.core.middleware.RedirectFallbackMiddleware",
    "mezzanine.core.middleware.TemplateForDeviceMiddleware",
    "mezzanine.core.middleware.TemplateForHostMiddleware",
    "mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
    "mezzanine.core.middleware.SitePermissionMiddleware",
    "mezzanine.pages.middleware.PageMiddleware",
    "mezzanine.core.middleware.FetchFromCacheMiddleware",
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
OPTIONAL_APPS = (
    "debug_toolbar",
    "django_extensions",
    "compressor",
    PACKAGE_NAME_FILEBROWSER,
    PACKAGE_NAME_GRAPPELLI,
)

DEBUG_TOOLBAR_CONFIG = {"INTERCEPT_REDIRECTS": False}


try:
    from local_settings import *
except ImportError:
    pass


try:
    from mezzanine.utils.conf import set_dynamic_settings
except ImportError:
    pass
else:
    set_dynamic_settings(globals())

更新:

我的项目现在叫做 mpj。 我使用 Wamp 来保存数据库和表。 我的数据库名字是 abcdef。 虽然数据库里没有表,但在运行迁移命令后,仍然出现了一些警告和错误。 Django 在我的 Wamp 服务器上运行得很好,但为什么 Mezzanine 会给我提示错误,显示“你的数据库不支持运行修改模式的语句”呢?

这里我附上了 Mezzanine-Migration 生成的错误图片。

注意:我在已安装的应用中提到了 “south”。

enter image description here

enter image description here

1 个回答

0

解决方案:

首先,找到你的项目文件夹,然后打开 local_settings.py 这个文件。在里面定义你的数据库,格式如下:

DATABASES = {
    "default": {
        # Ends with "postgresql_psycopg2", "mysql", "sqlite3" or "oracle".
        "ENGINE": "django.db.backends.mysql",
        # DB name or path to database file if using sqlite3.
        "NAME": "abc",
        # Not used with sqlite3.
        "USER": "root",
        # Not used with sqlite3.
        "PASSWORD": "123",
        # Set to empty string for localhost. Not used with sqlite3.
        "HOST": "",
        # Set to empty string for default. Not used with sqlite3.
        "PORT": "",
    }
}

要在 local_settings.pysettings.py 这两个文件中都定义数据库。

撰写回答