Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:django [2014/05/19 16:41] alfred [Basic knowledge for creating applications] |
fw:django [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 51: | Línea 51: | ||
| from polls.models import Poll | from polls.models import Poll | ||
| admin.site.register(Poll) | admin.site.register(Poll) | ||
| + | </code> | ||
| + | The structure to arrange several applications inside a project is: | ||
| + | <code> | ||
| + | <my project> | ||
| + | -- manage.py | ||
| + | -- <project name> | ||
| + | ----- urls.py | ||
| + | ----- models.py | ||
| + | ----- views.py | ||
| + | ----- blog\ <- An app | ||
| + | -------- views.py | ||
| + | -------- urls.py | ||
| + | -------- models.py | ||
| + | ----- tasks\ <- Another app | ||
| + | -------- views.py | ||
| + | -------- urls.py | ||
| + | -------- models.py | ||
| </code> | </code> | ||
| ==== Shell ==== | ==== Shell ==== | ||
| Running the command ''$ python manage.py shell'' will open a Python shell to interact with data inside the project. | Running the command ''$ python manage.py shell'' will open a Python shell to interact with data inside the project. | ||
| - | ==== Configuration ==== | + | ==== Configuration and settings ==== |
| + | A settings file is just a Python module with module-level variables. So it can import values from other settings file. | ||
| + | |||
| + | === Using settings in Python code === | ||
| + | <code> | ||
| + | from django.conf import settings | ||
| + | |||
| + | if settings.DEBUG: | ||
| + | # Do something | ||
| + | </code> | ||
| + | Remember that ''django.conf.settings'' is not a module so ''from django.conf.settings import DEBUG'' will not work. | ||
| + | |||
| + | === Superuser creation === | ||
| + | <code> | ||
| + | $ python manage.py createsuperuser --username=joe --email=joe@example.com | ||
| + | </code> | ||
| ===== Database ===== | ===== Database ===== | ||
| ==== Basic ==== | ==== Basic ==== | ||
| Línea 122: | Línea 154: | ||
| ===== Notes ===== | ===== Notes ===== | ||
| * To see the sql from an application you'll need to run: ''$ python manage.py sql appname'' | * To see the sql from an application you'll need to run: ''$ python manage.py sql appname'' | ||
| + | |||
| + | ==== Setting another settings file ==== | ||
| + | You should indicate your settings files, probably you'll need an environtment variable, to do easily you could execute manage using it: | ||
| + | <code> | ||
| + | $ DJANGO_SETTINGS_MODULE=settings.dev python manage.py runserver -l info | ||
| + | </code> | ||
| + | Or adding it before the imports: | ||
| + | <code python> | ||
| + | import os | ||
| + | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.dev') | ||
| + | |||
| + | from common.testCase import MongoTestCase | ||
| + | import engines.css.models as model | ||
| + | ... | ||
| + | </code> | ||