Django ARTICLE
History of Django
Django was created in 2003 by Python programmers Adrian Holovaty and Simon Willison when they were working at the Lawrence Journal-World newspaper. Python is one of the most popular programming languages today, and Django is a tool used by Python developers to facilitate the web development process. Read on to learn how Django helps developers create bespoke and fully customisable websites for clients.
Django Introduction
What is Django?
- Django is a free, open source framework for Python web development and is a very flexible web development tool that can be used to create just about any type of website or app that is needed. A framework is a collection of modules that provides pre-built elements that make coding more efficient and stable.
- As an example – a developer shouldn't build login screens and login processing themselves. There's too many places for it to go wrong.
Templates
- A template contains variables, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.
- Below is a minimal template that illustrates a few basics. Each element will be explained later in this document.
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
{% endfor %}
{% endblock %}
Variables
- A variable outputs a value from the context, which is a dict-like object mapping keys to values. Variables are surrounded by {{ and }} like this:
My first name is {{ first_name }}. My last name is {{ last_name }}.
- With a context of {'first_name': 'John', 'last_name': 'Doe'}, this template renders to:
My first name is John. My last name is Doe.
Tags
- Tags are surrounded by {% and %} like this:
{% csrf_token %}
- Most tags accept arguments:
{% cycle 'odd' 'even' %}
- Some tags require beginning and ending tags:
{% if user.is_authenticated %}Hello, {{ user.username }}.{% endif %}