python - Django Meta class -
i'm new django. i'm learning writing first django app.
when build following code (from tutorial) in sublime text 2:
from django.db import models django.utils import timezone import datetime class poll(models.model): question = models.charfield(max_length=200) pub_date = models.datetimefield('date published') def __unicode__(self): return self.question class choice(models.model): poll = models.foreignkey(poll) choice_text = models.charfield(max_length=200) votes = models.integerfield(default=0) def __unicode__(self): return self.choice_text
i error:
traceback (most recent call last): file "c:\djangoprojects\blog\polls\models.py", line 5, in <module> class poll(models.model): file "c:\python27\lib\site-packages\django\db\models\base.py", line 93, in __new__ kwargs = {"app_label": model_module.__name__.split('.')[-2]} indexerror: list index out of range [finished in 0.3s exit code 1]
reading similar questions on stackoverflow have read have add meta class
each of classes (and helps me).
but in django documentation read following:
if model exists outside of standard locations (models.py or models package in app), model must define app part of.
and not case.
that's location of models.py
in app:
blog/ manage.py blog/ __init__.py settings.py urls.py wsgi.py polls/ __init__.py admin.py models.py tests.py views.py
that's have in settings.py
(if necessary):
import os debug = true template_debug = debug admins = ( # ('your name', 'your_email@example.com'), ) managers = admins base_dir = "c:/djangoprojects/blog" databases = { 'default': { 'engine': 'django.db.backends.sqlite3', # add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'name': os.path.join(base_dir, 'db.sqlite3'), # or path database file if using sqlite3. # following settings not used sqlite3: 'user': '', 'password': '', 'host': '', # empty localhost through domain sockets or '127.0.0.1' localhost through tcp. 'port': '', # set empty string default. } } # hosts/domain names valid site; required if debug false # see https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts allowed_hosts = [] # local time zone installation. choices can found here: # http://en.wikipedia.org/wiki/list_of_tz_zones_by_name # although not choices may available on operating systems. # in windows environment must set system time zone. time_zone = 'europe/moscow' # language code installation. choices can found here: # http://www.i18nguy.com/unicode/language-identifiers.html language_code = 'en-us' site_id = 1 # if set false, django make optimizations not # load internationalization machinery. use_i18n = true # if set false, django not format dates, numbers , # calendars according current locale. use_l10n = true # if set false, django not use timezone-aware datetimes. use_tz = true # absolute filesystem path directory hold user-uploaded files. # example: "/var/www/example.com/media/" media_root = os.path.join(os.path.dirname(__file__), 'static/') # url handles media served media_root. make sure use # trailing slash. # examples: "http://example.com/media/", "http://media.example.com/" media_url = '' # absolute path directory static files should collected to. # don't put in directory yourself; store static files # in apps' "static/" subdirectories , in staticfiles_dirs. # example: "/var/www/example.com/static/" static_root = 'c:/djangoprojects/blog/static/' # url prefix static files. # example: "http://example.com/static/", "http://static.example.com/" static_url = '/static/' # additional locations of static files staticfiles_dirs = ( # put strings here, "/home/html/static" or "c:/www/django/static". # use forward slashes, on windows. # don't forget use absolute paths, not relative paths. ) # list of finder classes know how find static files in # various locations. staticfiles_finders = ( 'django.contrib.staticfiles.finders.filesystemfinder', 'django.contrib.staticfiles.finders.appdirectoriesfinder', 'django.contrib.staticfiles.finders.defaultstoragefinder', ) # make unique, , don't share anybody. secret_key = '@iae@w#o=(!9l0qc4jd3wrd&h@po4na7v*a#dq4csvfecy95nv' # list of callables know how import templates various sources. template_loaders = ( 'django.template.loaders.filesystem.loader', 'django.template.loaders.app_directories.loader', # 'django.template.loaders.eggs.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', # uncomment next line simple clickjacking protection: # 'django.middleware.clickjacking.xframeoptionsmiddleware', ) root_urlconf = 'blog.urls' # python dotted path wsgi application used django's runserver. wsgi_application = 'blog.wsgi.application' template_dirs = ( # put strings here, "/home/html/django_templates" or "c:/www/django/templates". # use forward slashes, on windows. # don't forget use absolute paths, not relative paths. "c:/djangoprojects/blog/templates" ) installed_apps = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # uncomment next line enable admin: 'django.contrib.admin', 'polls', # uncomment next line enable admin documentation: # 'django.contrib.admindocs', ) # sample logging configuration. tangible logging # performed configuration send email # site admins on every http 500 error when debug=false. # see http://docs.djangoproject.com/en/dev/topics/logging # more details on how customize logging configuration. logging = { 'version': 1, 'disable_existing_loggers': false, 'filters': { 'require_debug_false': { '()': 'django.utils.log.requiredebugfalse' } }, 'handlers': { 'mail_admins': { 'level': 'error', 'filters': ['require_debug_false'], 'class': 'django.utils.log.adminemailhandler' } }, 'loggers': { 'django.request': { 'handlers': ['mail_admins'], 'level': 'error', 'propagate': true, }, } }
the output of python manage.py sql polls
:
begin; create table "polls_poll" ( "id" integer not null primary key, "question" varchar(200) not null, "pub_date" datetime not null ) ; create table "polls_choice" ( "id" integer not null primary key, "poll_id" integer not null references "polls_poll" ("id"), "choice_text" varchar(200) not null, "votes" integer not null ) ; commit;
what's can wrong this?
Comments
Post a Comment