doc:appunti:prog:django
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| doc:appunti:prog:django [2011/02/20 16:42] – [L'applicazione admin] niccolo | doc:appunti:prog:django [2011/06/28 09:15] (current) – [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 143: | Line 146: | ||
| admin.site.register(Expense, | admin.site.register(Expense, | ||
| + | </ | ||
| + | |||
| + | ===== 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) | ||
| + | </ | ||
| + | |||
| + | ==== Visualizzazione e aggregazione dei dati ==== | ||
| + | |||
| + | Nel codice della **view** è possibile accedere ai dati (i **model**) sfruttando anche le relazioni tra essi (le //foreign key// delle tabelle del database). È possibile anche usare funzioni di aggregazione. | ||
| + | |||
| + | Ecco un esempio di una vista dell' | ||
| + | |||
| + | <code python> | ||
| + | from django.http import HttpResponse | ||
| + | from django.template import Context | ||
| + | from django.template.loader import get_template | ||
| + | |||
| + | from django.db.models import Sum, Count | ||
| + | |||
| + | from django.contrib.auth.models import User | ||
| + | #from spese.models import Expense | ||
| + | |||
| + | def balance(request): | ||
| + | |||
| + | users = User.objects.all() | ||
| + | |||
| + | max_expense = 0 | ||
| + | for user in users: | ||
| + | user.aggr = user.expense_set.aggregate(Count(' | ||
| + | if user.aggr[' | ||
| + | max_expense = user.aggr[' | ||
| + | for user in users: | ||
| + | user.balance = user.aggr[' | ||
| + | |||
| + | template = get_template(' | ||
| + | variables = Context({ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }) | ||
| + | output = template.render(variables) | ||
| + | return HttpResponse(output) | ||
| + | </ | ||
| + | |||
| + | Ecco infine il template **'' | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | {% if users %} | ||
| + | <table border=" | ||
| + | <tr> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </tr> | ||
| + | {% for user in users %} | ||
| + | <tr> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | {% endfor %} | ||
| + | </ | ||
| + | {% else %} | ||
| + | <p>No users found.</ | ||
| + | {% endif %} | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||
| ===== Webserver di test ===== | ===== Webserver di test ===== | ||
doc/appunti/prog/django.1298216570.txt.gz · Last modified: by niccolo
