• Save
Django tutorial 2009
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Django tutorial 2009

on

  • 1,504 views

 

Statistics

Views

Total Views
1,504
Views on SlideShare
1,504
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Django tutorial 2009 Presentation Transcript

  • 1. Django Szalai Ferenc(szferi@wsbricks.com)
  • 2. Bevezetés
  • 3. “Django egy magas szintű Python nyelven írt web alkalmazás keretrendszer, ami a gyors éspragmatikus alkalmazás fejlesztést támogatja.”
  • 4. Telepítés
  • 5. előfeltétel: python, django forrás(svn, tar.gz)# cd django# python setup.py installhttp://www.djangoproject.com/documentation/install/
  • 6. Alapfogalmak● Projekt● Alkalmazas● Model● View● Controller (URL)● Template
  • 7. $ django-admin.py startproject supporter
  • 8. $ ls -l supporter/__init__.pymanage.pysettings.pyurls.py
  • 9. $ python manage.py runserverValidating models...0 errors foundDjango version 1.1 beta 1, usingsettings supporter.settingsDevelopment server is running athttp://127.0.0.1:8000/Quit the server with CONTROL-C.
  • 10. settings.py● DATABASE_ENGINE● DATABASE_NAME● DATABASE_USER● DATABASE_PASSWORD● DATABASE_HOST● normál python modul● a saját konfig paramétereink legyenek nagybetűsek!● http://www.djangoproject.com/documentation/settings/
  • 11. Alkalmazások● logikai egység● újrafelhasználható● összetartozó model, view, url gyűjteménye● $ python manage.py startapp topic
  • 12. $ ls topic/__init__.pymodels.pytests.pyviews.py
  • 13. INSTALLED_APPS = ( django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, django.contrib.sites, supporter.topic)
  • 14. User Companydjango.contrib.auth Topic Topic djangon.contrib.comment Topic Comment supporter.topic Comment
  • 15. Model
  • 16. BEGIN;CREATE TABLE "topic_topic" ( "id" integer NOT NULL PRIMARY KEY, "author_id" integer NOT NULL, "type" varchar(1) NOT NULL, "title" varchar(128) NOT NULL, "content" text NOT NULL, "feeling" integer NOT NULL);COMMIT;
  • 17. Az SQL csúnya A Python cool
  • 18. from django.db import modelsfrom django.contrib.auth.models import Userclass Topic(models.Model): TOPIC_TYPES = ( (Q, Question), (I, Idea), (P, Problem), ) author = models.ForeignKey(User) type = models.CharField(max_length=1, choices=TOPIC_TYPES) title = models.CharField(max_length=128) content = models.TextField() feeling = models.IntegerField() created = models.DateTimeField(auto_now_add=True)
  • 19. $ python manage.py validate$ python manage.py syncdbCreating table auth_permissionCreating table auth_groupCreating table auth_userCreating table auth_messageCreating table django_content_typeCreating table django_sessionCreating table django_siteCreating table topic_topicYou just installed Djangos auth system, whichmeans you dont have any superusers defined.Would you like to create one now? (yes/no): yesUsername (Leave blank to use szferi):E-mail address: szferi@wsbricks.comPassword:Password (again):Superuser created successfully.Installing index for auth.Permission modelInstalling index for auth.Message modelInstalling index for topic.Topic model
  • 20. Model API
  • 21. $ python manage.py shellIn [1]: from supporter.topic.models import TopicIn [2]: from django.contrib.auth.models import UserIn [3]: u = User.objects.get(id=1)In [4]: print uszferiIn [5]: t = Topics.objects.create(author=u, type=Q,title=test, content=nagybaj!, feeling=-1)In [6]: print tTopic objectIn [7]: print t.titletestIn [8]: print Topic.objects.all()[<Topic: Topic object>]In [9]: ts = Topic.objects.filter(content__contains=baj)In [10]: print ts[0].contentnagybaj!
  • 22. class Topic(models.Model): ... def __unicode__(self): return self.title
  • 23. $ python manage.py shellIn [1]: from supporter.topic.models import TopicIn [2]: print Topic.objects.all()[<Topic: test>]
  • 24. class Topic(models.Model): ... class Meta: ordering = [created]
  • 25. Relációk
  • 26. from django.db import modelsfrom django.contrib.auth.models import Userclass Topic(models.Model): TOPIC_TYPES = ( (Q, Question), (I, Idea), (P, Problem), ) author = models.ForeignKey(User) type = models.CharField(max_length=1, choices=TOPIC_TYPES) title = models.CharField(max_length=128) content = models.TextField() feeling = models.IntegerField() created = models.DateTimeField(auto_now_add=True)
  • 27. $ python manage.py shellIn [2]: from django.contrib.auth.models import UserIn [3]: u = User.objects.get(id=1)In [4]: print uszferiIn [5]: print u.topic_set.all()[<Topic: test>]In [6]: print u.topic_set.filter(type=Q)[<Topic: test>]
  • 28. Views
  • 29. from django.http import HttpResponsedef index(request): return HttpResponse(Hello!)
  • 30. intermezzó: URL
  • 31. /customer/account/1/contacts/2/
  • 32. from django.conf.urls.defaults import *import viewsurlpatterns = patterns(, (r^$, views.index), (r^(?P<topic>[0-9]*)/$, views.detail))
  • 33. from django.conf.urls.defaults import *urlpatterns = patterns(, (r^topic/, include(supporter.topic.urls)),)
  • 34. Összetettebb View-k
  • 35. from django.http importHttpResponsefrom supporter.topic.modelsimport Topicdef index(request): r = <ul> topics = Topic.objects.all() for t in topics: r += "<li>%s</li>" % t.title r += </ul> return HttpResponse(r)
  • 36. intermezzó 2: Tempalte
  • 37. <html><body><p>There is {{ topics|length }} topics</p><ul>{% for t in topics %} <li>{{ t.title }}</li>{% endfor %}</ul></body></html>
  • 38. Django Admin
  • 39. from django.conf.urls.defaults import *from django.contrib import adminadmin.autodiscover()urlpatterns = patterns(, (r^admin/, include(admin.site.urls)), (r^topic/, include(supporter.topic.urls)),)
  • 40. Formok
  • 41. from models import Topicfrom django.forms import ModelFormclass TopicForm(ModelForm): class Meta: model = Topic exclude = (author, )
  • 42. def add(request): from forms import TopicForm from django.http import HttpResponseRedirect from django.contrib.auth.models import User if request.method == POST: form = TopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.author = User.objects.get(id=1) topic.save() return HttpResponseRedirect(/) else: form = TopicForm() return render_to_response(add.html,{form: form })
  • 43. <html><body><form method="post" action=".">{{ form }}<input type="submit" value="SAVE"name="save"/></form></body></html>
  • 44. Hova tovább, hovatovább● automatikus teszt● saját template tag és filter● session kezelés● authentikáció● cache● l10n, i18n● RSS/Atom● Generikus view-k● stb, stb.