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

Django tutorial 2009

on

  • 1,460 views

 

Statistics

Views

Total Views
1,460
Views on SlideShare
1,460
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 Django tutorial 2009 Presentation Transcript

  • 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 fejlesztést támogatja.”
  • Telepítés
  • előfeltétel: python, django forrás(svn, tar.gz)# cd django# python setup.py installhttp://www.djangoproject.com/documentation/install/
  • 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.settingsDevelopment server is running athttp://127.0.0.1:8000/Quit the server with CONTROL-C.
  • 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/
  • Alkalmazások● logikai egység● újrafelhasználható● összetartozó model, view, url gyűjteménye● $ python manage.py startapp topic
  • $ ls topic/__init__.pymodels.pytests.pyviews.py
  • INSTALLED_APPS = ( django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, django.contrib.sites, supporter.topic)
  • User Companydjango.contrib.auth Topic Topic djangon.contrib.comment Topic Comment supporter.topic Comment
  • Model
  • 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;
  • Az SQL csúnya A Python cool
  • 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)
  • $ 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
  • Model API
  • $ 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!
  • 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 = ( (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)
  • $ 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>]
  • 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]*)/$, views.detail))
  • 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 = Topic.objects.all() for t in topics: r += "<li>%s</li>" % t.title r += </ul> return HttpResponse(r)
  • intermezzó 2: Tempalte
  • <html><body><p>There is {{ topics|length }} topics</p><ul>{% for t in topics %} <li>{{ t.title }}</li>{% endfor %}</ul></body></html>
  • Django Admin
  • 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)),)
  • Formok
  • from models import Topicfrom django.forms import ModelFormclass TopicForm(ModelForm): class Meta: model = Topic exclude = (author, )
  • 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 })
  • <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● l10n, i18n● RSS/Atom● Generikus view-k● stb, stb.