Herramientas de usuario

Herramientas del sitio


fw: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
fw:django [2014/01/14 17:20]
alfred [Basic]
fw:django [2020/05/09 09:25] (actual)
Línea 36: Línea 36:
  
 ==== Basic knowledge for creating applications ==== ==== Basic knowledge for creating applications ====
-For django an application is like a module, something that have a concrete purpose and performs actions for that. An application can be used in several projects because a project is a group of applications.+For django an application is like a module, something that has a concrete purpose and performs actions for that. An application can be used in several projects because a project is finally ​a group of applications.
 <​code>​ <​code>​
 $ python manage.py startapp polls $ python manage.py startapp polls
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 103: Línea 135:
 ) )
 </​code>​ </​code>​
-=== url() function ===+==== url() function ​====
  
 The ''​url()''​ function takes four arguments, two required: regex and view, and two optional: kwargs, and name. The regex is a regular expression to match the url, it does not take GET neither POST arguments. In a request to ''<​nowiki>​http://​www.example.com/​myapp/?​page=3</​nowiki>'',​ the URLconf will also look for ''​myapp/''​. \\  The ''​url()''​ function takes four arguments, two required: regex and view, and two optional: kwargs, and name. The regex is a regular expression to match the url, it does not take GET neither POST arguments. In a request to ''<​nowiki>​http://​www.example.com/​myapp/?​page=3</​nowiki>'',​ the URLconf will also look for ''​myapp/''​. \\ 
-The view argument is the function which Django will call passing an ''​HttpRequest''​ object as the first argument and, if the regex uses arguments, also are passed.+The view argument is the function which Django will call passing an ''​HttpRequest''​ object as the first argument and, if the regex uses arguments, also are passed. This is an example of this kind of arguments:​ 
 +<code python>​ 
 +# views.py 
 +def detail(request,​ poll_id): 
 +    return HttpResponse("​You'​re looking at poll %s." % poll_id) 
 +# urls.py : 
 +urlpatterns = patterns('',​ 
 +    # ex: /polls/ 
 +    url(r'​^$',​ views.index,​ name='​index'​),​ 
 +    # ex: /polls/5/ 
 +    url(r'​^(?​P<​poll_id>​\d+)/​$',​ views.detail,​ name='​detail'​),​ 
 +
 +</​code>​ 
 +Calling ''/​polls/​34/''​ will make a call like this one: ''​detail(request=<​HttpRequest object>, poll_id='​34'​)''​. The poll_id='​34'​ part comes from ''​(?​P<​poll_id>​\d+)''​. ''?​P<​poll_id>''​ defines the name that will be used to identify the matched pattern; and ''​\d+''​ is a regular expression to match a sequence of digits.
 ===== 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>​
fw/django.1389720005.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)