Introduction to Django
Django"""The Web framework for perfectionists withdeadlines.""""""Django is a high-level Python Webframework that encourag...
What is inside?● Built-in ORM(Object-relational Mapper)● Simple but powerful URL system● Built-in template system● I10n(In...
MVT not MVC● Model  ○ defines the data structure  ○ takes care for querying the database● View  ○ defines what data should...
The live of request
Project structureTo start new project run:    django-admin.py startproject my_projectThe following structure will be creat...
Application structureTo create new application in you project just run the following commandin the shell:     python manag...
Models# models.pyfrom django.db import modelsfrom django.utils.translation import ugettext_lazy as _class News(models.Mode...
URLs dispatcher# urls.pyfrom django.conf.urls import patterns, url, includeurlpatterns = patterns(,   url(r^$, app.home, n...
Views# views.pyfrom .models import Newsfrom django.shortcuts import get_object_or_404def single_news(request, slug):  news...
Templates# template.html{% extends my_project/base.html %}<h1>{{ news.title }}</h1><div class="news-content">     {{ news....
About Meeng. Ilian Iliev● Web Developer for 9+ years● Python/Django fan for 3+ years● ilian@i-n-i.org● http://ilian.i-n-i....
Upcoming SlideShare
Loading in...5
×

Introduction to django

10,770

Published on

1 Comment
1 Like
Statistics
Notes
  • Very helpful for beginners...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
10,770
On Slideshare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
43
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to django

  1. 1. Introduction to Django
  2. 2. Django"""The Web framework for perfectionists withdeadlines.""""""Django is a high-level Python Webframework that encourages rapid developmentand clean, pragmatic design.""" https://www.djangoproject.com/
  3. 3. What is inside?● Built-in ORM(Object-relational Mapper)● Simple but powerful URL system● Built-in template system● I10n(Internationalization)● Cache system● Built-in authentication system● Built-in webserver(for development)● Automatic admin interface
  4. 4. MVT not MVC● Model ○ defines the data structure ○ takes care for querying the database● View ○ defines what data should be presented ○ returns HTTP response● Template ○ renders the data in suitable format - HTML/XML/etc.
  5. 5. The live of request
  6. 6. Project structureTo start new project run: django-admin.py startproject my_projectThe following structure will be created my_project/ manage.py my_project/ __init__.py settings.py # project settings reside here urls.py wsgi.pyThis is the default created and good for learning the basics but this way thesettings may be hard to maintain for project in production. Alternative approachis available at http://ilian.i-n-i.org/django-project-file-structure/.
  7. 7. Application structureTo create new application in you project just run the following commandin the shell: python manage.py startapp appThe following structure will be created app/ __init__.py admin.py # not created automatically before 1.5 models.py tests.py views.pyThere is no urls.py file but most developer find it useful to keep the applicationspecific URLs inside it and include them in the main URLs configuration file.
  8. 8. Models# models.pyfrom django.db import modelsfrom django.utils.translation import ugettext_lazy as _class News(models.Model): title = models.CharField(_(Title), max_length=255) slug = models.SlugField(_(Slug)) content = models.TextField()To create the tables for the define models just run: python manage.py syncdb
  9. 9. URLs dispatcher# urls.pyfrom django.conf.urls import patterns, url, includeurlpatterns = patterns(, url(r^$, app.home, name=home), # give the specific URL meaningfulname url(roffers/, include(offers.urls)), # include URLs from another app (r^news/$, news.views.offers), # point to specific view (r^news/(d{4})/$, news.views.year_archive), # pass arg(s) (r^news/(d{4})/(d{2})/$, news.views.month_archive), # pass arg(s) (r^news/(?P<slug>[-w]+)/$, news.views.single_news), # pass named args)● URLs dispatcher uses regular expression● The first match found is called● All arguments are passed as unicode strings
  10. 10. Views# views.pyfrom .models import Newsfrom django.shortcuts import get_object_or_404def single_news(request, slug): news = get_object_or_404(News.objects.public(), slug=slug) return render_to_response(news/single_news.html, {news: news})
  11. 11. Templates# template.html{% extends my_project/base.html %}<h1>{{ news.title }}</h1><div class="news-content"> {{ news.content|safe }}</div>
  12. 12. About Meeng. Ilian Iliev● Web Developer for 9+ years● Python/Django fan for 3+ years● ilian@i-n-i.org● http://ilian.i-n-i.org● https://github.com/IlianIliev/
  1. A particular slide catching your eye?

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

×