【问题标题】:python models.py syncdb does not workpython models.py syncdb 不起作用
【发布时间】:2013-03-19 00:08:16
【问题描述】:

安装 Python 2.7.2 和 Django 1.5 并创建我的第一个应用程序的第二天,现在我无法同步 models.py 并更新我的数据库。

工具:Windows Vista、Putty SSH 位置:Bluehost (www.bluehost.com) 服务器

这是一个我已经阅读但没有解决我的问题的主题: Why did I get error for python manage.py syncdb (IndentationError: unexpected indent)

建议是关于 models.py 中的 制表符和空格缩进),但请相信我使用了正确的缩进,如下所示(models.py )。

from django.db import models

# Create your models here.

class posts(models.Model):

    author = models.CharField(max_lenght = 30)
    title = models.CharField(max_lenght = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

我已经做了什么:

我已经安装了 Python 2.7.2,检查:

username@domain.com [~]# python -V
Python 2.7.2 

我已经安装了 Django 1.5,检查一下

username@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import get_version
>>> get_version()
'1.5'
>>>

我已经安装了 MySQL-python-1.2.4(在这一点上我不确定它是否正常工作):

username@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

我有problem with django-admin.py, solved it changing the .bashrc PATH entry,并成功运行colde:

django-admin.py startproject mysite

然后我像这样修改models.py(创建一个简单的博客):

from django.db import models

# Create your models here.

class posts(models.Model):

    author = models.CharField(max_lenght = 30)
    title = models.CharField(max_lenght = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

然后我创建了一个 mysql 数据库 和一个 user,将用户添加到数据库并给 所有权限(全部在 bluehost 前端面板中完成)。

然后我像这样修改了 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'domain_dbname',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'domain_username',
        'PASSWORD': 'password',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

问题

现在是更新数据库的时候了,我试试这个:

python models.py syncdb

这是我得到的:

Traceback (most recent call last):
  File "models.py", line 1, in <module>
    from django.db import models
  File "/home1/domain/python/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/home1/domain/python/lib/python2.7/site-packages/django/conf/__init__.py", line 52, in __getattr__
    self._setup(name)
  File "/home1/domain/python/lib/python2.7/site-packages/django/conf/__init__.py", line 45, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

所以我的问题是:

为什么这条线不起作用?

python models.py syncdb

【问题讨论】:

    标签: python mysql database django python-2.7


    【解决方案1】:

    使用这个:

    python manage.py syncdb
    

    【讨论】:

    • 哦!我明白了,谢谢。在花了这么多时间之后,我想我需要休息一下。 user@domain.com [~/www/domain/mysite]# python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no):
    • 是的,我没有足够的声望来投票。我已经尝试接受答案,但我应该等待 3 分钟。再次感谢。
    • @Eniac 是否创建了帖子表?
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2020-04-13
    • 2011-08-04
    相关资源
    最近更新 更多