Django is a Python web framework that encourages rapid development and clean, pragmatic design. It uses the MTV (Model Template View) pattern rather than MVC. Django features an ORM, form handling, template system and admin interface. To set up a Django project, you create a project directory, install Django, add apps, set up models and URLs, and develop templates and views. Django promotes loose coupling, DRY principles and writing minimal code.
2. What's Django?
"Django is a high-level Python Web framework that encourages
rapid development and clean, pragmatic design."
from http://www.djangoproject.com/
"...and not a CMS System."
7. Skills
Convention Over Configuration
Object-Relational Mapping (ORM)
Very useful admin CRUD
Form handling
Elegant URL Design
Template system
Cache system
i18n
8. Principles (...or philosophies)
DRY (Don't Repeat Yourself )
Loose coupling
Write less code as possible
Quick development
Explicit is better than implicit
17. Let's to do something...
Open settings.py:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'myblog.db'
add 'django.contrib.admin'
Open urls.py:
Uncomment the line: "from django.contrib... "
Uncomment the line: "admin.autodiscover()... "
Uncomment the line: "(r'^admin... "
21. Setup files
init.py - indicates a python package
models.py - app domain model
views.py - project controller
22. App x Project
App - web app that do something. E.g. XXX
Project - lot of apps and themselves settings. A project can
be a lot of apps, and a app can stay in severals projects
23. Create the model
Open models.py and edit:
from django.db import models
class Artigo(models.Model):
titulo = models.CharField(max_length=100)
conteudo = models.TextField()
publicacao = models.DateTimeField()
24. Setting the model classes to admin
Open admin.py and edit:
from django.contrib import admin
from models import Artigo
admin.site.register(Artigo)
25. Add the app to the admin
Open settings.py and add:
"my_project.blog"
Re-sync the database
Re-run the development server
26. Setting the urls
Open the urls.py and edit:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from my_blog.models import Artigo
urlpatterns = patterns('',
(r'^$', 'django.views.generic.date_based.archive_index',
{'queryset': Artigo.objects.all(), 'date_field': 'publicacao'}),
(r'^admin/(.*)', admin.site.root),
)
27. Setting the templates
Create templates/blog/artigo_archive.html and edit:
<html><body>
<h1>Meu blog</h1>
{% for artigo in latest %}
<h2>{{ artigo.titulo }}</h2>
{{ artigo.conteudo }}
{% endfor %}
</body></html>