Django     Szalai Ferenc(szferi@wsbricks.com)
Bevezetés
“Django egy magas szintű Python    nyelven írt web alkalmazás   keretrendszer, ami a gyors éspragmatikus alkalmazás fejles...
Telepítés
előfeltétel: python, django forrás(svn, tar.gz)# cd django# python setup.py installhttp://www.djangoproject.com/documentat...
Alapfogalmak●   Projekt●   Alkalmazas●   Model●   View●   Controller (URL)●   Template
$ django-admin.py startproject supporter
$ ls -l supporter/__init__.pymanage.pysettings.pyurls.py
$ python manage.py runserverValidating models...0 errors foundDjango version 1.1 beta 1, usingsettings supporter.settingsD...
settings.py●   DATABASE_ENGINE●   DATABASE_NAME●   DATABASE_USER●   DATABASE_PASSWORD●   DATABASE_HOST●   normál python mo...
Alkalmazások● logikai egység● újrafelhasználható● összetartozó model, view, url  gyűjteménye●   $ python manage.py startap...
$ ls topic/__init__.pymodels.pytests.pyviews.py
INSTALLED_APPS = (    django.contrib.auth,    django.contrib.contenttypes,    django.contrib.sessions,    django.contrib.s...
User                                 Companydjango.contrib.auth                                  Topic                    ...
Model
BEGIN;CREATE TABLE "topic_topic" (    "id" integer NOT NULL PRIMARY KEY,    "author_id" integer NOT NULL,    "type" varcha...
Az SQL csúnya A Python cool
from django.db import modelsfrom django.contrib.auth.models import Userclass Topic(models.Model):    TOPIC_TYPES = (      ...
$ python   manage.py validate$ python   manage.py syncdbCreating   table auth_permissionCreating   table auth_groupCreatin...
Model API
$ python manage.py shellIn [1]: from supporter.topic.models import TopicIn [2]: from django.contrib.auth.models import Use...
class Topic(models.Model):    ...   def __unicode__(self):       return self.title
$ python manage.py shellIn [1]: from supporter.topic.models import TopicIn [2]: print Topic.objects.all()[<Topic: test>]
class Topic(models.Model):    ...   class Meta:       ordering = [created]
Relációk
from django.db import modelsfrom django.contrib.auth.models import Userclass Topic(models.Model):    TOPIC_TYPES = (      ...
$ python manage.py shellIn [2]: from django.contrib.auth.models import UserIn [3]: u = User.objects.get(id=1)In [4]: print...
Views
from django.http import HttpResponsedef index(request):    return HttpResponse(Hello!)
intermezzó: URL
/customer/account/1/contacts/2/
from django.conf.urls.defaults import *import viewsurlpatterns = patterns(,    (r^$, views.index),    (r^(?P<topic>[0-9]*)...
from django.conf.urls.defaults import *urlpatterns = patterns(,    (r^topic/, include(supporter.topic.urls)),)
Összetettebb View-k
from django.http importHttpResponsefrom supporter.topic.modelsimport Topicdef index(request):    r = <ul>    topics = Topi...
intermezzó 2: Tempalte
<html><body><p>There is {{ topics|length }} topics</p><ul>{% for t in topics %}   <li>{{ t.title }}</li>{% endfor %}</ul><...
Django Admin
from django.conf.urls.defaults import *from django.contrib import adminadmin.autodiscover()urlpatterns = patterns(,    (r^...
Formok
from models import Topicfrom django.forms import ModelFormclass TopicForm(ModelForm):    class Meta:        model = Topic ...
def add(request):    from forms import TopicForm    from django.http import HttpResponseRedirect    from django.contrib.au...
<html><body><form method="post" action=".">{{ form }}<input type="submit" value="SAVE"name="save"/></form></body></html>
Hova tovább, hovatovább●   automatikus teszt●   saját template tag és filter●   session kezelés●   authentikáció●   cache●...
Django tutorial 2009
Django tutorial 2009
Django tutorial 2009
Django tutorial 2009
Django tutorial 2009
Django tutorial 2009
Upcoming SlideShare
Loading in...5
×

Django tutorial 2009

1,134

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,134
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Django tutorial 2009"

  1. 1. Django Szalai Ferenc(szferi@wsbricks.com)
  2. 2. Bevezetés
  3. 3. “Django egy magas szintű Python nyelven írt web alkalmazás keretrendszer, ami a gyors éspragmatikus alkalmazás fejlesztést támogatja.”
  4. 4. Telepítés
  5. 5. előfeltétel: python, django forrás(svn, tar.gz)# cd django# python setup.py installhttp://www.djangoproject.com/documentation/install/
  6. 6. Alapfogalmak● Projekt● Alkalmazas● Model● View● Controller (URL)● Template
  7. 7. $ django-admin.py startproject supporter
  8. 8. $ ls -l supporter/__init__.pymanage.pysettings.pyurls.py
  9. 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. 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. 11. Alkalmazások● logikai egység● újrafelhasználható● összetartozó model, view, url gyűjteménye● $ python manage.py startapp topic
  12. 12. $ ls topic/__init__.pymodels.pytests.pyviews.py
  13. 13. INSTALLED_APPS = ( django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, django.contrib.sites, supporter.topic)
  14. 14. User Companydjango.contrib.auth Topic Topic djangon.contrib.comment Topic Comment supporter.topic Comment
  15. 15. Model
  16. 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. 17. Az SQL csúnya A Python cool
  18. 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. 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. 20. Model API
  21. 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. 22. class Topic(models.Model): ... def __unicode__(self): return self.title
  23. 23. $ python manage.py shellIn [1]: from supporter.topic.models import TopicIn [2]: print Topic.objects.all()[<Topic: test>]
  24. 24. class Topic(models.Model): ... class Meta: ordering = [created]
  25. 25. Relációk
  26. 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. 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. 28. Views
  29. 29. from django.http import HttpResponsedef index(request): return HttpResponse(Hello!)
  30. 30. intermezzó: URL
  31. 31. /customer/account/1/contacts/2/
  32. 32. from django.conf.urls.defaults import *import viewsurlpatterns = patterns(, (r^$, views.index), (r^(?P<topic>[0-9]*)/$, views.detail))
  33. 33. from django.conf.urls.defaults import *urlpatterns = patterns(, (r^topic/, include(supporter.topic.urls)),)
  34. 34. Összetettebb View-k
  35. 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. 36. intermezzó 2: Tempalte
  37. 37. <html><body><p>There is {{ topics|length }} topics</p><ul>{% for t in topics %} <li>{{ t.title }}</li>{% endfor %}</ul></body></html>
  38. 38. Django Admin
  39. 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. 40. Formok
  41. 41. from models import Topicfrom django.forms import ModelFormclass TopicForm(ModelForm): class Meta: model = Topic exclude = (author, )
  42. 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. 43. <html><body><form method="post" action=".">{{ form }}<input type="submit" value="SAVE"name="save"/></form></body></html>
  44. 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.

×