Django (Web Konferencia 2009)

3,354
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,354
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
42
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django (Web Konferencia 2009)

  1. 1. Farkas Szilveszter Magyarországi Web Konferencia Budapest, 2009. október 3.
  2. 2. Farkas Szilveszter
  3. 3. Farkas Szilveszter
  4. 4. Farkas Szilveszter
  5. 5. Farkas Szilveszter
  6. 6. from presentation import (Django, Forms, Middleware, Tests, Python)
  7. 7. >>> import django >>> django.ORIGINAL_AUTHOR 'Adrian Holovaty' >>> django.OPEN_SOURCED datetime.date(2005, 7, 13) >>> django.VERSION '1.1-final' >>> len(django.AUTHORS) 485
  8. 8. >>> import django >>> django.ORIGINAL_AUTHOR 'Adrian Holovaty' >>> django.OPEN_SOURCED datetime.date(2005, 7, 13) >>> django.VERSION '1.1-final' >>> len(django.AUTHORS) 485
  9. 9. >>> import django >>> django.ORIGINAL_AUTHOR 'Adrian Holovaty' >>> django.OPEN_SOURCED datetime.date(2005, 7, 13) >>> django.VERSION '1.1-final' >>> len(django.AUTHORS) 485
  10. 10. >>> import django >>> django.ORIGINAL_AUTHOR 'Adrian Holovaty' >>> django.OPEN_SOURCED datetime.date(2005, 7, 13) >>> django.VERSION '1.1-final' >>> len(django.AUTHORS) 485
  11. 11. >>> import django >>> django.ORIGINAL_AUTHOR 'Adrian Holovaty' >>> django.OPEN_SOURCED datetime.date(2005, 7, 13) >>> django.VERSION '1.1-final' >>> len(django.AUTHORS) 485
  12. 12. >>> django.MODEL 'model' >>> django.VIEW 'template' >>> django.CONTROLLER 'view'
  13. 13. $ django-admin startproject webkonf $ cd webkonf $ ./manage.py startapp conference
  14. 14. from django.db import models class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class Conference(models.Model): name = models.CharField(max_length=32) venue = models.ForeignKey(ConferenceVenue) from django.contrib.auth.models import User class Attendee(models.Model): user = models.OneToOneField(User) conferences = models.ManyToManyField( Conference, related_name='attendees')
  15. 15. from django.db import models class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class Conference(models.Model): name = models.CharField(max_length=32) venue = models.ForeignKey(Venue) from django.contrib.auth.models import User class Attendee(models.Model): user = models.OneToOneField(User) conferences = models.ManyToManyField( Conference, related_name='attendees')
  16. 16. from django.db import models class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class Conference(models.Model): name = models.CharField(max_length=32) venue = models.ForeignKey(Venue) from django.contrib.auth.models import User class Attendee(models.Model): user = models.OneToOneField(User) conferences = models.ManyToManyField( Conference, related_name='attendees')
  17. 17. [...] <h1>{{ conference.name }}</h1> <h2>Látogatók</h2> <ul> {% for attendee in conference.attendees.all %} <li>{{ attendee.user.get_full_name }}</li> {% endfor %} </ul> [...]
  18. 18. from django.shortcuts import (get_object_or_404, render_to_response) from conference.models import Conference def conference_page(request, conf_id): conference = get_object_or_404( Conference, pk=conf_id) context = { 'conference': conference } return render_to_response( 'conference.html', context)
  19. 19. from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^/conf/(?P<conf_id>d+)/$', 'conference.views.conference_page'), )
  20. 20. from django import forms
  21. 21. űrlapok
  22. 22. űrlapok szerver oldali adatellenőrzés
  23. 23. Űrlap osztály class AttendeeForm(forms.Form): Mező name = forms.CharField('Név', max_length=32) Widget password = forms.CharField( 'Jelszó', max_length=32, widget=forms.PasswordInput())
  24. 24. Űrlap osztály class AttendeeForm(forms.Form): Mező name = forms.CharField('Név', max_length=32) Widget password = forms.CharField( 'Jelszó', max_length=32, widget=forms.PasswordInput())
  25. 25. Űrlap osztály class AttendeeForm(forms.Form): Mező name = forms.CharField('Név', max_length=32) Widget password = forms.CharField( 'Jelszó', max_length=32, widget=forms.PasswordInput())
  26. 26. Beépített ellenőrzés email = forms.EmailField(max_length=75) zip = forms.IntegerField(min_value=1000, max_value=9999) Mezőszintű egyedi ellenőrzés def clean_FIELDNAME(self): Űrlapszintű egyedi ellenőrzés def clean(self):
  27. 27. Beépített ellenőrzés email = forms.EmailField(max_length=75) zip = forms.IntegerField(min_value=1000, max_value=9999) Mezőszintű egyedi ellenőrzés def clean_FIELDNAME(self): Űrlapszintű egyedi ellenőrzés def clean(self):
  28. 28. Beépített ellenőrzés email = forms.EmailField(max_length=75) zip = forms.IntegerField(min_value=1000, max_value=9999) Mezőszintű egyedi ellenőrzés def clean_FIELDNAME(self): Űrlapszintű egyedi ellenőrzés def clean(self):
  29. 29. Mezőszintű egyedi ellenőrzés def clean_email(self): if 'email' in self.cleaned_data: email = self.cleaned_data['email'] if not email.endswith('@web.conf.hu'): raise forms.ValidationError('Nem vagy szervező.') else: return email
  30. 30. from django import middlewares
  31. 31. process_request() process_response() process_view() process_exception()
  32. 32. from django.http import HttpResponseRedirect class LoginMiddleware(object): def process_request(self, request): if not request.user.is_authenticated(): if request.get_full_path() != '/login/': return HttpResponseRedirect( '/login/?next=%s' % request.get_full_path()) else: return None
  33. 33. from django.http import HttpResponseRedirect class LoginMiddleware(object): def process_request(self, request): if not request.user.is_authenticated(): if request.get_full_path() != '/login/': return HttpResponseRedirect( '/login/?next=%s' % request.get_full_path()) else: return None
  34. 34. process_view(self, request, view_func, view_args, view_kwargs) process_response(self, request, response) process_exception(self, request, exception)
  35. 35. from django import test
  36. 36. doctest class Conference(models.Model): """ >>> v = Venue.objects.create( name='CEU', address='Budapest') >>> c = Conference.objects.create( Name='WebKonf', venue=v) >>> c.name u'WebKonf' >>> c.venue.name u'CEU' """ name = models.CharField(max_length=32) venue = models.ForeignKey(Venue)
  37. 37. unittest import unittest class ConferenceTest(unittest.TestCase): def setUp(self): venue = Venue.objects.create( Name='CEU', address='Budapest') self.conf = Conference.objects.create( Name='WebKonf', venue=venue) def test_conference(self): self.assertEquals(self.conf.name, u'WebKonf') self.assertEquals(self.conf.venue.name, u'CEU') def tearDown(self): self.conf.delete()
  38. 38. from django.test import TestCase -> kliens (GET, POST) -> „hozzávalók” beemelése (JSON/XML dump) -> egyedi url konfiguráció -> e-mail fiók -> további assert-ek
  39. 39. from django.test import TestCase -> kliens (GET, POST) def test_conference(self): response = self.client.get('/conf/1/') self.assertTrue('<h1>WebKonf</h1>' in response.content)
  40. 40. from django.test import TestCase -> „hozzávalók” beemelése (JSON/XML dump) class ConferenceTest(TestCase): fixtures = ['webkonf.json'] $ ./manage.py dumpdata conference > conference/fixtures/webkonf.json
  41. 41. from django.test import TestCase -> egyedi url konfiguráció class ConferenceTest(TestCase): urls = 'conference.test_urls'
  42. 42. from django.test import TestCase -> e-mail fiók from django.core import mail def test_email(self): mail.send_mail('Subject', 'Message.', 'from@web.conf.hu', ['jakab@gipsz.hu'], fail_silently=False) self.assertEquals(len(mail.outbox), 1) self.assertEquals(mail.outbox[0].subject, 'Subject')
  43. 43. from django.test import TestCase -> további assert-ek assertContains(response, text, count=None, status_code=200) assertNotContains(response, text, status_code=200) assertFormError(response, form, field, errors) assertTemplateUsed(response, template_name) assertTemplateNotUsed(response, template_name) assertRedirects(response, expected_url, status_code=302, target_status_code=200)
  44. 44. $ python
  45. 45. from django.db import models from django.contrib.localflavor.us.models import USStateField class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class USVenue(Venue): state = USStateField(default='CA') class ConferenceVenue(Venue): rooms = forms.PositiveIntegerField()
  46. 46. from django.db import models from django.contrib.localflavor.us.models import USStateField class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class USVenue(Venue): state = USStateField(default='CA') class ConferenceVenue(Venue): rooms = forms.PositiveIntegerField()
  47. 47. from django.db import models from django.contrib.localflavor.us.models import USStateField class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class USVenue(Venue): state = USStateField(default='CA') class ConferenceVenue(Venue): rooms = forms.PositiveIntegerField()
  48. 48. from django.db import models from django.contrib.localflavor.us.models import USStateField class Venue(models.Model): name = models.CharField(max_length=64) address = models.CharField(max_length=128) class Meta: abstract = True class USVenue(Venue): state = USStateField(default='CA') class ConferenceVenue(Venue): rooms = forms.PositiveIntegerField()
  49. 49. from django.contrib.auth.decorators import login_required @login_required def conference_private(request): [...] from django.utils.decorators import decorator_from_middleware from conference.middleware.login import LoginMiddleware login_required = decorator_from_middleware( LoginMiddleware) @login_required def conference_private(request): [...]
  50. 50. from django.contrib.auth.decorators import login_required @login_required def conference_private(request): [...] from django.utils.decorators import decorator_from_middleware from conference.middleware.login import LoginMiddleware login_required = decorator_from_middleware( LoginMiddleware) @login_required def conference_private(request): [...]
  51. 51. Modulok (csomagok) __init__.py conference/ __init__.py models.py templates/ conference.html views.py manage.py settings.py urls.py
  52. 52. Modulok (csomagok) __init__.py conference/ __init__.py models.py templates/ conference.html views.py manage.py registration/ [...] settings.py urls.py
  53. 53. Modulok (csomagok) __init__.py conference/ __init__.py models.py templates/ conference.html views/ __init__.py attendees.py conferences.py manage.py registration/ [...] settings.py urls.py
  54. 54. Globális/újrahasznosítható modulok INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'conference', )
  55. 55. Globális/újrahasznosítható modulok INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'conference', 'django_registration', )
  56. 56. Globális/újrahasznosítható modulok INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'conference', 'django_registration', 'django_openid_auth', )
  57. 57. Globális/újrahasznosítható modulok INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'conference', 'django_registration', 'django_openid_auth', 'django_contact_form', )
  58. 58. from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^/conf/(?P<conf_id>d+)/$', 'conference.views.conference_page'), (r'^openid/', include('django_openid_auth.urls')), )
  59. 59. Globális/újrahasznosítható modulok django-compressor django-contact-form django-db-log django-debug-toolbar django-extensions django-flatblocks django-gravatar django-oembed django-openid-auth django-proxy django-registration django-tagging django-timezones django-tinymce django-voting django-wikiapp
  60. 60. Remixek Pinax (http://pinaxproject.com/) 37 django-* Mingus (http://github.com/montylounge/django-mingus/) 28 django-*
  61. 61. Hivatkozások http://www.djangoproject.com/ http://docs.djangoproject.com/ http://gábor.20y.hu/django/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×