Django 101

Warning: The browser you are currently using isn't supported on this website. The enjoy this course the way it was intended please upgrade your browser or use Google Chrome or Mozilla Firefox.

Chapter 1

Django Introduction

Audio Player


Start

Press the play button to start this chapter.

Need more time to check out the slides? Just press the pause key.

About your teacher

The real Dave, not the Voice-Over Dave ;)

Follow me on Twitter! (@fijter). Note: The tie on this photo is just for the occasion ;)

About this chapter

Python

The Python Shell

In the next chapter of this course we will cover the Python basics

Django's basic features

Security in Django

SQL Injection

Batteries included

The Django Admin

The Django Admin

Authentication

Authentication

Caching

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.\
                    db.DatabaseCache',
        'LOCATION': 'my_cache_table',
    }
}
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.\
                    memcached.MemcachedCache',
        'LOCATION': [
            '172.19.26.240:11211',
            '172.19.26.242:11211',
            '172.19.26.244:11213',
        ]
    }
}
    

Comments


{% for comment in comment_list %}
    
    
        permalink for comment #{{ forloop.counter }}
    

    

{{ comment.comment }}

submitted {{ comment.submit_date }} by {{ comment.user_name }}

{% endfor %}

E-mail

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 
    'from@example.com', ['to@example.com'])
from django.core.mail import EmailMultiAlternatives

subject = 'Welcome to Django101.com'
from_email = 'from@example.com'
to = 'to@example.com'
text_content = 'hello world.'
html_content = '

hello world.

' msg = EmailMultiAlternatives(subject, text_content, \ from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()

Humanize

  • apnumber
    • 1 becomes one
    • 2 becomes two
  • intcomma
    • 1337 becomes 1,337
    • 1000000 becomes 1,000,000
  • intword
    • 1000000 becomes 1.0 million
    • 1000000000 becomes 1.0 billion
  • naturalday
    • Sept. 25, 2017 becomes today
    • Sept. 24, 2017 becomes yesterday
  • ordinal
    • 1 becomes 1st
    • 2 becomes 2nd

Internationalization

#: actions.py:49
#, python-format
msgid "Successfully deleted %(count)d %(items)s."
msgstr "%(count)d 個の %(items)sを削除しました。"

#: actions.py:61 options.py:1219
#, python-format
msgid "Cannot delete %(name)s"
msgstr "%(name)s が削除できません"

#: actions.py:63 options.py:1221
msgid "Are you sure?"
msgstr "よろしいですか?"

#: actions.py:85
#, python-format
msgid "Delete selected %(verbose_name_plural)s"
msgstr "選択された %(verbose_name_plural)s の削除"
    

Pagination

from django.core.paginator import Paginator

objects = ['john', 'paul', 'george', 'ringo']
p = Paginator(objects, per_page=2)
page1 = p.page(1)
    

Values

>>> p.count
4
>>> p.num_pages
2
>>> p.page_range
[1, 2]

>>> page1.object_list
['john', 'paul']

Messages

from django.contrib import messages

messages.debug(request, '%s signed in.' % user.user_name)
messages.info(request, 'Your next chapter is available.')
messages.success(request, 'Thank you for signing up!')
messages.warning(request, 'Maintenance in 5 minutes...')
messages.error(request, 'Please login first!')

To display them in your Templates:

{% if messages %}
    {% for message in messages %}
  • {{ message }}
  • {% endfor %}
{% endif %}

Sitemaps & Feeds

from django.contrib.syndication.views import Feed
from news.models import NewsItem

class LatestEntriesFeed(Feed):
    title = "Django101 News"
    link = "/news/"
    description = "The latest news!"

    def items(self):
        return NewsItem.objects.order_by('-pub_date')[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.description
    

Don't repeat yourself

Not DRY
print "Don't repeat yourself"
print "Don't repeat yourself"
print "Don't repeat yourself"
...
    
DRY (well, sorta ;))
print "Don't repeat yourself\n" * 1000
    

What is Django

Django's documentation

Conclusion

The complete course contains:

  • 21 additional chapters covering all the basics
  • Self tests; Become a Django101 certified developer
  • Time saving code snippets & configuration templates
  • The possibility to ask me questions regarding the course

So what are you waiting for?

order now