Django Overview
Django Overview
2. Basic Knowledge
3. Technical Details
4. Tutorial
5. Conclusion
Basic Knowledge
Basic Knowledge
Workflow
Web Browser
Template URL
View
Form
Model
Admin
Database
Basic Knowledge
Web Browser Web Browser
Template URL
• What users actually see
• Responds to what users do
View
when they
Form
Model 1. Click
2. Type
Admin
3. Press Enter
Database
Basic Knowledge
Web Browser URL
Template
• Often referred as the ‘web
URL
address’
View
• Provides mapping to View
Form
Model
What is mapping?
Admin The act of assigning
functions to URLs
Database
Basic Knowledge
Web Browser View
• Where all the functions are
Template URL
written
View
• Render content to Template
Form Get information from Model
•
Admin
Database
Basic Knowledge
Web Browser Form
Template URL HTML Form ≠ Django Form
Admin
Database
Basic Knowledge
Web Browser Model
• Describes the structure of an
Template URL
object you want to store in
View
Database
Form
• Goes straight to Database
Model and create & edit & request
Admin information as you wish
Database
Basic Knowledge
Web Browser Admin
Template URL
• Helps to register your object
in your Model so you can
View
manage data in Database
Form
• The registration has to be
Model
done in the first place
Admin
Database
Basic Knowledge
Web Browser Database
Template URL
View
Model
Form
Admin
?
Database
Basic Knowledge
Web Browser Database
Template URL
View
Admin
Database
Basic Knowledge
Web Browser Database
• Collectionof data
Template URL
Admin
Database
Basic Knowledge
Workflow
Web Browser
Template URL
View
Form
Model
Admin
Database
Outline
1. Introduction
2. Basic Knowledge
3. Technical Details
4. Tutorial
5. Conclusion
Technical Details
1. Directory Architecture
2. Module
3. Commands
4. Template Tags
5. High-level URL Configuration
Technical Details
Directory Architecture
project_name/ ———————— Container of your entire project, which often referred as ‘workspace’
manage.py ————— The command-line utility to interact with your Django project
E.g1. python manage.py help E.g2. python manage.py runserver -h
your_project/ ———— The name of your Django project
inti .py ——- The file required for Python to treat this directory as a package
settings.py ——- Configuration for this Django project
url.py ————- Management of URLs to provide mapping to view.py
mypackage/
init .py
mymodule.py
Note: Migration is Django’s way of propagating the changes you made into your database schema
Technical Details
Commands
$ python manage.py -h
To ask for what kind of command can be used
$ python manage.py runserver -h
To ask for what kind of command can be used
after runserver
Technical Details
1. Directory Architecture
2. Module
3. Commands
4. Template Tags
5. High-level URL Configuration
Technical Details
Template Tags
{{ form.as_p }}
{{ csrf_token }}
class Entry(models.Model):
title =
models.CharField(max_length=200)
slug = models.SlugField(unique=True)
body = models.TextField()
data =models.DateTimeField(default=datetime.now)
categories = models.ManyToManyField(Category)
python manage.py syncdb
View
def entry_list(request):
entries = Ebtry.objects.all()[:5]
return render_to_response('list.html', {'entries': entries})
<html>
<head>
<title>My Blog</title>
</head>
<body>
{% for entry in entries %}
<h1>{{ entry.title|upper }}</h1>
{{ entry.body }}<br/>
Published {{ entry.data|date:"d F Y" }},
<a href=”{{ entry.get_absolute_url }}”>link</a>.
{% endfor %}
</body>
</html>
Tag and Filter
Build in Filters and Tags
Custom tag and filter libraries
Put logic in tags
{% load comments %}
<h1>{{ entry.title|upper }}</h1>
{{ entry.body }}<br/>
Published {{ entry.data|date:"d F Y" }},
<a href=”{{ entry.get_absolute_url }}”>link</a>.
<h3> </h3>
{% get_comment_list for entry as comment_list %}
{% for comment in comment_list %}
{{ comment.content }}
{% endfor %}
Template Inheritance
base.html index.html
<html>
<head>
<title>
{% extend “base.html” %}
{% block title %}
{% block title %}
{% endblock %}
Main page
</title>
{% endblock %}
</head>
{% block body
<body>
%} Content
{% block body %}
{% endblock %}
{% endblock %}
</body>
</html>
URL Dispatcher
urlpatterns = patterns('',
#http://jianghu.leyubox.com/articles/
((r'^articles/$', ‘article.views.index'), )
#http://jianghu.leyubox.com/articles/2003/
(r'^articles/(?P<year>\d{4})/$', ‘article.views.year_archive'),
# http://jianghu.leyubox.com/articles/2003/ 12/
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$',
'article.views.month_archive'),
# http://jianghu.leyubox.com/articles/2003/ 12/3
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article..
views.article_detail'), )
High-levelURL Configuration
url(r'^$', views.function)
?P< v > : to take v and sent to view as a variable
^ : beginning of the url
$ : end of the url
() : to capture part of the pattern
+ : to indicate the previous item should repeat at
least once
{} : to indicate a specific number of repetition
High-levelURL Configuration
url(r’^anything/(?P<variable>[0-9]+)/$’, views.function)
url(r'^(?P<variable>/[0-9]{n})/$', views.function)
Note:
o No need to add a leading slash(/articles-year)
o The 'r' in front of each regular expression string is optional but recommended. It tells
Python that a
string is "raw" -- that nothing in the string should be escaped.
In python, the ‘\’ backslash character in control chars must be escaped for regular
expression use. Basically we have to add one more slash i.e \\t, \\b. To work around
backslash plague, you can raw string, by prefixing the string with the letter r.
56
Can include other URLconf modules
urlpatterns = patterns(' ',
url(r'^support/', include('demoproject.support.urls')),
)
Using Prefix
urlpatterns = patterns(' ',
(r'^articles/(\d{4})/$', 'mysite.news.views.articles_year'),
(r'^articles/(\d{4})/(\d{2})/$', 'mysite.news.views.articles_month'),
)
Here mysite.news.views is common, so can be rewritten as follows
urlpatterns = patterns('mysite.news.views',
(r'^articles/(\d{4})/$', 'articles_year'),
(r'^articles/(\d{4})/(\d{2})/$', 'articles_month'),
)
Passing extra arguments and Dictionary mapping
patterns(' ',
(r'^articles/(?P<year>\d{4})/$', 'articles_year'), {'foo': 'bar'}),
)