【问题标题】:django not reading my models.pydjango 没有阅读我的 models.py
【发布时间】:2012-08-28 08:43:47
【问题描述】:

使用 Ubuntu 12.04 无头服务器
Python v2.7.3
Django v1.3.1 最终版

Python 已预装,我从 apt-get python-django 安装了 django

我会注意到我已经设置并配置了 2 台服务器以供开发使用。既是为了练习,也是因为我经常出差,并且有一个方便的虚拟机总是很好的。但是,这台机器给我带来了麻烦,我无法理解原因。

我以前的设置非常简单。安装 ubuntu,安装 python-django,将文件复制到服务器,检查权限,运行 python manage.py syncdb,然后运行 ​​python.manage.py runserver 0.0.0.0:8080,一切正常。由于某种原因,这在此服务器上不起作用。我完全迷失了,因为我没有改变任何东西,而且我很确定我没有做任何不同的事情。

感谢任何帮助,我提前致谢

   $python manage.py shell
   ...
   >>>import pwht.models
   >>>k = Device(serial="something", mac="something", ip="10.211.119.50", active = True)
   Traceback (most recent call last):
   File "<console>", line 1, in <module>
   NameError: name 'Device' is not defined

我的路径在manage.py下的sys.path中

charles@Jobserver-Gibby:~/pwht/pwht$ python manage.py shell
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import sys
>>> print sys.path
['/home/charles/pwht/pwht', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/u
sr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>> 

为了完整性

charles@Jobserver-Gibby:~/pwht/pwht$ python manage.py shell
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from pwht.models import Device
>>>

import django 也可以正常工作,虽然我没有在这里列出。

这是我忘记提及的硬币的另一面:

charles@Jobserver-Gibby:~/pwht/pwht$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
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): no
Installing custom SQL ...
Installing indexes ...
No fixtures found.
charles@Jobserver-Gibby:~/pwht/pwht$ 

我的模型中的表都没有被创建。在 Mysql 中也是如此。

这是我在项目中的文件

manage.py

   #!/usr/bin/env python
   from django.core.management import execute_manager
   import imp
   try:
       imp.find_module('settings') # Assumed to be in the same directory.
   except ImportError:
       import sys
       sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
       sys.exit(1)

   import settings

   if __name__ == "__main__":
       execute_manager(settings)

settings.py

   # Django settings for pwht project.

   DEBUG = True
   TEMPLATE_DEBUG = DEBUG

   ADMINS = (
       # ('Your Name', 'your_email@example.com'),
   )

   MANAGERS = ADMINS

   DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.sqlite3',
           'NAME': '/home/charles/pwht/pwht/mydb.sql',                      
           'USER': '',                      
           'PASSWORD': '',                  
           'HOST': '',                      
           'PORT': '',                      
       }
   }

   TIME_ZONE = 'America/New_York'

   LANGUAGE_CODE = 'en-us'

   SITE_ID = 1

   USE_I18N = True

   USE_L10N = True

   MEDIA_ROOT = '/home/charles/pwht/pwht/media/'

   MEDIA_URL = '/media/'

   STATIC_ROOT = '/home/charles/pwht/pwht/static/'

   STATIC_URL = '/static/'

   ADMIN_MEDIA_PREFIX = '/static/admin/'

   STATICFILES_DIRS = (
       '/home/charles/pwht/pwht/statics',
   )

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


   SECRET_KEY = [purposely removed]

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

   MIDDLEWARE_CLASSES = (
       'django.middleware.common.CommonMiddleware',
       'django.contrib.sessions.middleware.SessionMiddleware',
       'django.middleware.csrf.CsrfViewMiddleware',
       'django.contrib.auth.middleware.AuthenticationMiddleware',
       'django.contrib.messages.middleware.MessageMiddleware',
   )

   ROOT_URLCONF = 'pwht.urls'

   TEMPLATE_DIRS = (
       '/home/charles/pwht/pwht/templates/',
   )

   INSTALLED_APPS = (
       'django.contrib.auth',
       'django.contrib.contenttypes',
       'django.contrib.sessions',
       'django.contrib.sites',
       'django.contrib.messages',
       'django.contrib.staticfiles',
   )

   LOGGING = {
       'version': 1,
       'disable_existing_loggers': False,
       'handlers': {
           'mail_admins': {
               'level': 'ERROR',
               'class': 'django.utils.log.AdminEmailHandler'
           }
       },
       'loggers': {
           'django.request': {
               'handlers': ['mail_admins'],
               'level': 'ERROR',
               'propagate': True,
           },
       }
   }

models.py

'''
Created on Aug 18, 2012

@author: Charles B. Gibby
'''
from django.db import models

class Device (models.Model):
    serial = models.CharField(max_length=30)
    mac = models.CharField(max_length=17)
    ip = models.IPAddressField()
    active = models.BooleanField()

    def __unicode__(self):
        return self.serial

class Console (models.Model):
    device = models.ForeignKey(Device)
    serial = models.CharField(max_length=30)
    address = models.CharField(max_length=2)
    active = models.BooleanField()

    def __unicode__(self):
        return self.serial

    class Meta:
        ordering = ['device', 'address']

class Current_zones (models.Model):
    console = models.ForeignKey(Console)
    zone = models.IntegerField()
    temp = models.IntegerField(null=True)
    when_temp = models.DateTimeField(blank=True, auto_now=False)
    output = models.IntegerField(null=True)
    when_output = models.DateTimeField(blank=True, auto_now=False)
    man_sp = models.IntegerField(null=True)
    when_man_sp = models.DateTimeField(blank=True, auto_now=False)
    trim = models.IntegerField(null=True)
    when_trim = models.DateTimeField(blank=True, auto_now=False)
    clamp = models.IntegerField(null=True)
    when_clamp = models.DateTimeField(blank=True, auto_now=False)
    auto_sp = models.IntegerField(null=True)
    when_auto_sp = models.DateTimeField(blank=True, auto_now=False)
    sp_type = models.IntegerField(null=True)
    when_sp_type = models.DateTimeField(blank=True, auto_now=False)

    def __unicode__(self):
        return u'%s-%s' % (self.console, self.zone)

    class Meta:
        ordering = ['console', 'zone']

class Message (models.Model):
    console = models.ForeignKey(Console)
    req_type = models.BooleanField() # 0 for Read and 1 for Write
    parameter = models.CharField(max_length=1)
    target = models.CharField(max_length=2)
    data = models.CharField(max_length=4)
    when_message = models.DateTimeField(blank=True, auto_now=False)
    tx = models.BooleanField()
    when_tx = models.DateTimeField(blank=True, auto_now=False)
    status = models.BooleanField()
    when_status = models.DateTimeField(blank=True, auto_now=False)

    def __unicode__(self):
        return u'%s-%s' % (self.console, self.parameter)

    class Meta:
        ordering = ['console', 'when_message']

class Recorded_zones (models.Model):
    console = models.ForeignKey(Console)
    zone = models.IntegerField()
    temp = models.IntegerField(null=True)
    when_temp = models.DateTimeField(blank=True, auto_now=False)
    output = models.IntegerField(null=True)
    when_output = models.DateTimeField(blank=True, auto_now=False)
    man_sp = models.IntegerField(null=True)
    when_man_sp = models.DateTimeField(blank=True, auto_now=False)
    trim = models.IntegerField(null=True)
    when_trim = models.DateTimeField(blank=True, auto_now=False)
    clamp = models.IntegerField(null=True)
    when_clamp = models.DateTimeField(blank=True, auto_now=False)
    auto_sp = models.IntegerField(null=True)
    when_auto_sp = models.DateTimeField(blank=True, auto_now=False)
    sp_type = models.IntegerField(null=True)
    when_sp_type = models.DateTimeField(blank=True, auto_now=False)

    def __unicode__(self):
        return u'%s-%s' % (self.console, self.zone)

    class Meta:
        ordering = ['console', 'zone', 'when_temp']

class Profiler (models.Model):
    console = models.ForeignKey(Console)
    climb = models.IntegerField()
    soak = models.IntegerField()
    hold = models.IntegerField()
    down = models.IntegerField()
    end = models.IntegerField()
    duration = models.IntegerField()
    when_duration = models.DateTimeField(blank=True, auto_now=False)

    def __unicode__(self):
        return self.console

    class Meta:
        ordering = ['console']    

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    第一个 sn-p 中的第三行;而不是

    import pwht.models
    

    你试过了吗:

    from pwht.models import Device
    

    更新pwht 应用程序包含在您的settings.py INSTALLED_APPS 中。因此在您的INSTALLED_APPS 中添加pwht

    【讨论】:

    • 是的,为了同样的结果,我编辑了问题以反映这一点。
    • @Klemorali:设备中的大写 D
    • @Klemorali 服务器上的syncdb 运行正常吗? IE。它是否创建了数据库表?这意味着正在读取models.py
    • @Klemorali 被称为pwhtapp 是否包含在您的settings.py INSTALLED_APPS 中?
    • 应用程序是否包含在 INSTALLED_APPS 中与是否可以导入无关。事实上,你最近的一次大写 D 似乎奏效了,正如你上面的复制和粘贴所示:你还有什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2016-11-11
    • 2017-07-20
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多