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 | ||
|
wiki2:python:medium_django [2018/10/13 08:38] alfred [Django gotchas] |
wiki2:python:medium_django [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 2: | Línea 2: | ||
| ===== Django hows ===== | ===== Django hows ===== | ||
| + | ==== Cómo tener varios setting files ==== | ||
| + | Símplemente has de definir una variable de entorno denominada ''DJANGO_SETTINGS_MODULE'' e indicar la ruta del módulo de settings deseado: | ||
| + | <code> | ||
| + | DJANGO_SETTINGS_MODULE = coopolis_backoffice.settings.alfred | ||
| + | </code> | ||
| + | |||
| + | ==== Loggear las peticiones a DB ==== | ||
| + | En las settings: | ||
| + | <code> | ||
| + | LOGGING = { | ||
| + | 'version': 1, | ||
| + | 'disable_existing_loggers': False, | ||
| + | 'handlers': { | ||
| + | 'console': { | ||
| + | 'class': 'logging.StreamHandler', | ||
| + | }, | ||
| + | }, | ||
| + | 'loggers': { | ||
| + | 'django.db.backends': { | ||
| + | 'handlers': ['console'], | ||
| + | 'level': 'DEBUG', | ||
| + | }, | ||
| + | }, | ||
| + | } | ||
| + | |||
| + | </code> | ||
| ==== How Django manages Url's? ==== | ==== How Django manages Url's? ==== | ||
| It only needs a module for url configs, which will be a simple mapping between URL patterns to python functions corresponding to views. | It only needs a module for url configs, which will be a simple mapping between URL patterns to python functions corresponding to views. | ||
| Línea 113: | Línea 139: | ||
| Namespaced URLs are specified using the ':' operator. For example, the main index page of the admin application is referenced using 'admin:index'. This indicates a namespace of 'admin', and a named URL of 'index'. Namespaces can also be nested. The named URL 'sports:polls:index' would look for a pattern named 'index' in the namespace 'polls' that is itself defined within the top-level namespace 'sports'. | Namespaced URLs are specified using the ':' operator. For example, the main index page of the admin application is referenced using 'admin:index'. This indicates a namespace of 'admin', and a named URL of 'index'. Namespaces can also be nested. The named URL 'sports:polls:index' would look for a pattern named 'index' in the namespace 'polls' that is itself defined within the top-level namespace 'sports'. | ||
| + | |||
| + | ==== Dynamic fields ==== | ||
| + | For example in urls: | ||
| + | <code> | ||
| + | urlpatterns = [ | ||
| + | path('', TemplateView.as_view( | ||
| + | template_name="fok/home.html", | ||
| + | extra_context={ | ||
| + | # The lambda makes this expression to be executed each call of home (because of the admin updates) | ||
| + | 'campaigns': lambda: Campaign.objects.filter(visible=True).order_by('-created_at') | ||
| + | } | ||
| + | ), name='home'), | ||
| + | ... | ||
| + | </code> | ||
| ===== Questions ===== | ===== Questions ===== | ||