doc:appunti:prog:django
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:appunti:prog:django [2011/06/26 10:02] – [L'applicazione admin] niccolo | doc:appunti:prog:django [2011/06/26 11:34] – [Mettere i dati nella vista] niccolo | ||
---|---|---|---|
Line 9: | Line 9: | ||
===== Progetto e applicazioni ===== | ===== Progetto e applicazioni ===== | ||
+ | |||
+ | * **Progetto** | ||
+ | * **Applicazione**: | ||
Per creare un **nuovo progetto**: | Per creare un **nuovo progetto**: | ||
Line 147: | Line 150: | ||
===== Creare una nuova pagina ===== | ===== Creare una nuova pagina ===== | ||
+ | ==== Vista ==== | ||
+ | |||
+ | Nel file **'' | ||
+ | |||
+ | <code python> | ||
+ | from django.http import HttpResponse | ||
+ | |||
+ | def my_view(request): | ||
+ | output = u''' | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | ''' | ||
+ | u' | ||
+ | ) | ||
+ | return HttpResponse(output) | ||
+ | </ | ||
+ | |||
+ | ==== URL ==== | ||
+ | |||
+ | Nel file **'' | ||
+ | |||
+ | <code python> | ||
+ | from my_app.views import * | ||
+ | |||
+ | urlpatterns = patterns('', | ||
+ | (r' | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | La pagina sarà visibile all' | ||
+ | ==== Mettere i dati nella vista ==== | ||
+ | |||
+ | Invece di includere il codice HTML direttamente nella vista (funzione Python) si utilizza il sistema dei **template**. | ||
+ | |||
+ | Nella directory radice del progetto si crea la sottodirectory **'' | ||
+ | |||
+ | <code python> | ||
+ | import os.path | ||
+ | |||
+ | TEMPLATE_DIRS = ( | ||
+ | os.path.join(os.path.dirname(__file__), | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | Quindi si crea il file **'' | ||
+ | |||
+ | <code html> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Infine si modifica il codice della vista: | ||
+ | |||
+ | <code python> | ||
+ | from django.http import HttpResponse | ||
+ | from django.template import Context | ||
+ | from django.template.loader import get_template | ||
+ | |||
+ | def my_view(request): | ||
+ | template = get_template(' | ||
+ | variables = Context({ | ||
+ | ' | ||
+ | ' | ||
+ | }) | ||
+ | output = template.render(variables) | ||
+ | return HttpResponse(output) | ||
+ | </ | ||
===== Webserver di test ===== | ===== Webserver di test ===== | ||
doc/appunti/prog/django.txt · Last modified: 2011/06/28 09:15 by niccolo