Herramientas de usuario

Herramientas del sitio


wiki2:python:medium_django

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python:medium_django [2018/10/11 19:35]
alfred [Django gotchas]
wiki2:python:medium_django [2020/05/09 09:25] (actual)
Línea 1: Línea 1:
-====== Django ​gotchas ​======+====== Django ​Gotchas ​======
  
 ===== Django hows ===== ===== Django hows =====
-===== How Django manages Url'​s? ​=====+==== 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? ====
 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.
 <code python> <code python>
Línea 106: Línea 132:
 </​code>​ </​code>​
  
-==== URL namespaces ​====+=== URL namespaces ===
 A URL namespace comes in two parts, both of which are strings: A URL namespace comes in two parts, both of which are strings:
   * Application namespace: This describes the name of the application that is being deployed. ​   * Application namespace: This describes the name of the application that is being deployed. ​
Línea 114: Línea 140:
 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 =====
wiki2/python/medium_django.1539286536.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)