2016-07-24 23 views
0

我在django中創建了我的第一個模型,我一直在關注與我正在使用的django版本相比較之前的django書籍,我創建了一個應用書籍並編寫了以下模型檢查模型時出現屬性錯誤

from django.db import models 

#Create your models here. 

    class Publisher(models.Model): 
     name = models.CharField(max_length=30) 
     address = models.CharField(max_length=50) 
     city = models.CharField(max_length=60) 
     state_province = models.CharField(max_length=30) 
     country = models.CharField(max_length=50) 
     website = models.URLField() 
    class Author(models.Model): 
     first_name = models.CharField(max_length=30) 
     last_name = models.CharField(max_length=40) 
     email = models.EmailField() 
    class Book(models.Model): 
     title = models.CharField(max_length=100) 
     authors = models.ManyToManyField(Author) 
     publisher = models.ForeignKey(Publisher) 
     publication_date = models.DateField() 

當我使用的檢查它,PY manage.py檢查它給

AttributeError: module 'first' has no attribute 'books' 

我的網站結構是1)first\books(models) 2)first\first 3)first\manage.py

我更改設置文件

INSTALLED_APPS = [ 
    #'django.contrib.admin', 
    #'django.contrib.auth', 
    #'django.contrib.contenttypes', 
    #'django.contrib.sessions', 
    #'django.contrib.messages', 
    #'django.contrib.staticfiles', 
    'first.books' -- I commented the defaults as described in the book 
] 

MIDDLEWARE_CLASSES = [ 
    #'django.middleware.security.SecurityMiddleware', 
    #'django.contrib.sessions.middleware.SessionMiddleware', 
    #'django.middleware.csrf.CsrfViewMiddleware', 
    #'django.contrib.auth.middleware.AuthenticationMiddleware', 
    #'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    #'django.contrib.messages.middleware.MessageMiddleware', 
    #'django.middleware.clickjacking.XFrameOptionsMiddleware' 
] 

任何人都可以擺脫一些燈我被卡住了。

My directory stucture

C:\Program Files\Python35\mywebapp\first>tree /f 
Folder PATH listing 
Volume serial number is CE58-0759 
C:. 
│ db.sqlite3 
│ manage.py 
│ 
├───books 
│ │ admin.py 
│ │ apps.py 
│ │ models.py 
│ │ tests.py 
│ │ views.py 
│ │ __init__.py 
│ │ 
│ └───migrations 
│   __init__.py 
│ 
└───first 
    │ settings.py 
    │ urls.py 
    │ views.py 
    │ wsgi.py 
    │ __init__.py 
    │ 
    ├───template 
    │  basic.html 
    │ 
    └───__pycache__ 
      settings.cpython-35.pyc 
      urls.cpython-35.pyc 
      views.cpython-35.pyc 
      wsgi.cpython-35.pyc 
      __init__.cpython-35.pyc 

my settings.py

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '*********' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    ''''django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles',''' 
    'first.books', 
] 

MIDDLEWARE_CLASSES = [ 
    ''' 'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware',''' 
] 

ROOT_URLCONF = 'first.urls' 
import os.path 
TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(os.path.dirname(__file__) ,'template').replace('\\','/')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'first.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.9/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 

STATIC_URL = '/static/' 
+0

你用什麼名字創建應用程序,當你做了'python manage.py startapp ...' –

+0

摩西,我已經將它命名爲書籍 –

+0

不是'first.books',只是''first','in INSTALLED_APPS –

回答

1

您的應用程序被命名爲books,所以books(而不是first.books)應該被包括在你的INSTALLED_APPS設定app_name

+0

摩西,它沒有踢,也是我評論出默認值的方式是正確的行爲不是嗎? –

+0

不,你不應該評論他們。 Django需要這些來讓你的項目正常工作 –

+0

是的,謝謝得到了兩個錯誤first.app,然後評論mislead :) –

相關問題