{"title":"Django at a glance","version":"6.1","locale":"en","docname":"intro/overview","url":"/en/6.1/intro/overview/","canonical":"https://djangodocs.dev/en/6.1/intro/overview/","summary":"Because Django was developed in a fast-paced newsroom environment, it was designed to make common web development tasks fast and easy. Here’s an informal overview…","html":"<h1>Django at a glance<a class=\"heading-anchor\" href=\"#django-at-a-glance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Because Django was developed in a fast-paced newsroom environment, it was\ndesigned to make common web development tasks fast and easy. Here’s an informal\noverview of how to write a database-driven web app with Django.</p>\n<p>The goal of this document is to give you enough technical specifics to\nunderstand how Django works, but this isn’t intended to be a tutorial or\nreference – but we’ve got both! When you’re ready to start a project, you can\n<a class=\"reference internal\" href=\"/en/6.1/intro/tutorial01/\"><span class=\"doc\">start with the tutorial</span></a> or <a class=\"reference internal\" href=\"/en/6.1/topics/\"><span class=\"doc\">dive right into\nmore detailed documentation</span></a>.</p>\n<section id=\"design-your-model\">\n<h2>Design your model<a class=\"heading-anchor\" href=\"#design-your-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Although you can use Django without a database, it comes with an\n<a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Object-relational_mapping\">object-relational mapper</a> in which you describe your database layout in\nPython code.</p>\n<p>The <a class=\"reference internal\" href=\"/en/6.1/topics/db/models/\"><span class=\"doc\">data-model syntax</span></a> offers many rich ways of\nrepresenting your models – so far, it’s been solving many years’ worth of\ndatabase-schema problems. Here’s a quick example:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/models.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Reporter</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    full_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">70</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>full_name\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Article</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    pub_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    headline <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n    content <span class=\"o\">=</span> models<span class=\"o\">.</span>TextField<span class=\"p\">()</span>\n    reporter <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Reporter<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>headline\n</code></pre></figure>\n</section>\n<section id=\"install-it\">\n<h2>Install it<a class=\"heading-anchor\" href=\"#install-it\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Next, run the Django command-line utilities to create the database tables\nautomatically:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python manage.py makemigrations\n<span class=\"gp\">$ </span>python manage.py migrate\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py makemigrations\n<span class=\"gp\">...\\&gt;</span> py manage.py migrate\n</code></pre></div></div></div>\n<p>The <a class=\"reference internal\" href=\"/en/6.1/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\">makemigrations</code></a> command looks at all your available models and\ncreates migrations for whichever tables don’t already exist. <a class=\"reference internal\" href=\"/en/6.1/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>\nruns the migrations and creates tables in your database, as well as optionally\nproviding <a class=\"reference internal\" href=\"/en/6.1/topics/migrations/\"><span class=\"doc\">much richer schema control</span></a>.</p>\n</section>\n<section id=\"enjoy-the-free-api\">\n<h2>Enjoy the free API<a class=\"heading-anchor\" href=\"#enjoy-the-free-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>With that, you’ve got a free, and rich, <a class=\"reference internal\" href=\"/en/6.1/topics/db/queries/\"><span class=\"doc\">Python API</span></a>\nto access your data. The API is created on the fly, no code generation\nnecessary:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"go\"># Import the models we created from our &quot;news&quot; app</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">news.models</span> <span class=\"kn\">import</span> Article<span class=\"p\">,</span> Reporter\n\n<span class=\"go\"># No reporters are in the system yet.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet []&gt;</span>\n\n<span class=\"go\"># Create a new Reporter.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r <span class=\"o\">=</span> Reporter<span class=\"p\">(</span>full_name<span class=\"o\">=</span><span class=\"s2\">&quot;John Smith&quot;</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Save the object into the database. You have to call save() explicitly.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>save<span class=\"p\">()</span>\n\n<span class=\"go\"># Now it has an ID.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>id\n<span class=\"go\">1</span>\n\n<span class=\"go\"># Now the new reporter is in the database.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Reporter: John Smith&gt;]&gt;</span>\n\n<span class=\"go\"># Fields are represented as attributes on the Python object.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>full_name\n<span class=\"go\">&#39;John Smith&#39;</span>\n\n<span class=\"go\"># Django provides a rich database lookup API.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Reporter: John Smith&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>full_name__startswith<span class=\"o\">=</span><span class=\"s2\">&quot;John&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Reporter: John Smith&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>full_name__contains<span class=\"o\">=</span><span class=\"s2\">&quot;mith&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Reporter: John Smith&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Reporter<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n    <span class=\"o\">...</span>\n<span class=\"gr\">DoesNotExist</span>: Reporter matching query does not exist.\n\n<span class=\"x\"># Create an article.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">datetime</span> <span class=\"kn\">import</span> date\n<span class=\"gp\">&gt;&gt;&gt; </span>a <span class=\"o\">=</span> Article<span class=\"p\">(</span>\n<span class=\"gp\">... </span>    pub_date<span class=\"o\">=</span>date<span class=\"o\">.</span>today<span class=\"p\">(),</span> headline<span class=\"o\">=</span><span class=\"s2\">&quot;Django is cool&quot;</span><span class=\"p\">,</span> content<span class=\"o\">=</span><span class=\"s2\">&quot;Yeah.&quot;</span><span class=\"p\">,</span> reporter<span class=\"o\">=</span>r\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>a<span class=\"o\">.</span>save<span class=\"p\">()</span>\n\n<span class=\"go\"># Now the article is in the database.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Article<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Article: Django is cool&gt;]&gt;</span>\n\n<span class=\"go\"># Article objects get API access to related Reporter objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r <span class=\"o\">=</span> a<span class=\"o\">.</span>reporter\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>full_name\n<span class=\"go\">&#39;John Smith&#39;</span>\n\n<span class=\"go\"># And vice versa: Reporter objects get API access to Article objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>article_set<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Article: Django is cool&gt;]&gt;</span>\n\n<span class=\"go\"># The API follows relationships as far as you need, performing efficient</span>\n<span class=\"go\"># JOINs for you behind the scenes.</span>\n<span class=\"go\"># This finds all articles by a reporter whose name starts with &quot;John&quot;.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Article<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>reporter__full_name__startswith<span class=\"o\">=</span><span class=\"s2\">&quot;John&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;Article: Django is cool&gt;]&gt;</span>\n\n<span class=\"go\"># Change an object by altering its attributes and calling save().</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>full_name <span class=\"o\">=</span> <span class=\"s2\">&quot;Billy Goat&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>save<span class=\"p\">()</span>\n\n<span class=\"go\"># Delete an object with delete().</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>r<span class=\"o\">.</span>delete<span class=\"p\">()</span>\n</code></pre></div>\n</section>\n<section id=\"a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house\">\n<h2>A dynamic admin interface: it’s not just scaffolding – it’s the whole house<a class=\"heading-anchor\" href=\"#a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Once your models are defined, Django can automatically create a professional,\nproduction ready <a class=\"reference internal\" href=\"/en/6.1/ref/contrib/admin/\"><span class=\"doc\">administrative interface</span></a> –\na website that lets authenticated users add, change and delete objects. The\nonly step required is to register your model in the admin site:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/models.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Article</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    pub_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    headline <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n    content <span class=\"o\">=</span> models<span class=\"o\">.</span>TextField<span class=\"p\">()</span>\n    reporter <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Reporter<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/admin.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> admin\n\n<span class=\"kn\">from</span> <span class=\"nn\">.</span> <span class=\"kn\">import</span> models\n\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>register<span class=\"p\">(</span>models<span class=\"o\">.</span>Article<span class=\"p\">)</span>\n</code></pre></figure>\n<p>The philosophy here is that your site is edited by a staff, or a client, or\nmaybe just you – and you don’t want to have to deal with creating backend\ninterfaces only to manage content.</p>\n<p>One typical workflow in creating Django apps is to create models and get the\nadmin sites up and running as fast as possible, so your staff (or clients) can\nstart populating data. Then, develop the way data is presented to the public.</p>\n</section>\n<section id=\"design-your-urls\">\n<h2>Design your URLs<a class=\"heading-anchor\" href=\"#design-your-urls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A clean, elegant URL scheme is an important detail in a high-quality web\napplication. Django encourages beautiful URL design and doesn’t put any cruft\nin URLs, like <code class=\"docutils literal notranslate\">.php</code> or <code class=\"docutils literal notranslate\">.asp</code>.</p>\n<p>To design URLs for an app, you create a Python module called a <a class=\"reference internal\" href=\"/en/6.1/topics/http/urls/\"><span class=\"doc\">URLconf</span></a>. A table of contents for your app, it contains a mapping\nbetween URL patterns and Python callback functions. URLconfs also serve to\ndecouple URLs from Python code.</p>\n<p>Here’s what a URLconf might look like for the <code class=\"docutils literal notranslate\">Reporter</code>/<code class=\"docutils literal notranslate\">Article</code>\nexample above:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/urls.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n\n<span class=\"kn\">from</span> <span class=\"nn\">.</span> <span class=\"kn\">import</span> views\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;articles/&lt;int:year&gt;/&quot;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>year_archive<span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;articles/&lt;int:year&gt;/&lt;int:month&gt;/&quot;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>month_archive<span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;articles/&lt;int:year&gt;/&lt;int:month&gt;/&lt;int:pk&gt;/&quot;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>article_detail<span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The code above maps URL paths to Python callback functions (“views”). The path\nstrings use parameter tags to “capture” values from the URLs. When a user\nrequests a page, Django runs through each path, in order, and stops at the\nfirst one that matches the requested URL. (If none of them matches, Django\ncalls a special-case 404 view.) This is blazingly fast, because the paths are\ncompiled into regular expressions at load time.</p>\n<p>Once one of the URL patterns matches, Django calls the given view, which is a\nPython function. Each view gets passed a request object – which contains\nrequest metadata – and the values captured in the pattern.</p>\n<p>For example, if a user requested the URL “/articles/2005/05/39323/”, Django\nwould call the function <code class=\"docutils literal notranslate\">news.views.article_detail(request,\nyear=2005, month=5, pk=39323)</code>.</p>\n</section>\n<section id=\"write-your-views\">\n<h2>Write your views<a class=\"heading-anchor\" href=\"#write-your-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Each view is responsible for doing one of two things: Returning an\n<a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> object containing the content for the\nrequested page, or raising an exception such as <a class=\"reference internal\" href=\"/en/6.1/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\">Http404</code></a>.\nThe rest is up to you.</p>\n<p>Generally, a view retrieves data according to the parameters, loads a template\nand renders the template with the retrieved data. Here’s an example view for\n<code class=\"docutils literal notranslate\">year_archive</code> from above:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.shortcuts</span> <span class=\"kn\">import</span> render\n\n<span class=\"kn\">from</span> <span class=\"nn\">.models</span> <span class=\"kn\">import</span> Article\n\n\n<span class=\"k\">def</span> <span class=\"nf\">year_archive</span><span class=\"p\">(</span>request<span class=\"p\">,</span> year<span class=\"p\">):</span>\n    a_list <span class=\"o\">=</span> Article<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>pub_date__year<span class=\"o\">=</span>year<span class=\"p\">)</span>\n    context <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;year&quot;</span><span class=\"p\">:</span> year<span class=\"p\">,</span> <span class=\"s2\">&quot;article_list&quot;</span><span class=\"p\">:</span> a_list<span class=\"p\">}</span>\n    <span class=\"k\">return</span> render<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"s2\">&quot;news/year_archive.html&quot;</span><span class=\"p\">,</span> context<span class=\"p\">)</span>\n</code></pre></figure>\n<p>This example uses Django’s <a class=\"reference internal\" href=\"/en/6.1/topics/templates/\"><span class=\"doc\">template system</span></a>, which\nhas several powerful features but strives to stay simple enough for\nnon-programmers to use.</p>\n</section>\n<section id=\"design-your-templates\">\n<h2>Design your templates<a class=\"heading-anchor\" href=\"#design-your-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The code above loads the <code class=\"docutils literal notranslate\">news/year_archive.html</code> template.</p>\n<p>Django has a template search path, which allows you to minimize redundancy\namong templates. In your Django settings, you specify a list of directories to\ncheck for templates with <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a>. If a template\ndoesn’t exist in the first directory, it checks the second, and so on.</p>\n<p>Let’s say the <code class=\"docutils literal notranslate\">news/year_archive.html</code> template was found. Here’s what that\nmight look like:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">news/templates/news/year_archive.html</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;base.html&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">title</span> <span class=\"cp\">%}</span>Articles for <span class=\"cp\">{{</span> <span class=\"nv\">year</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>Articles for <span class=\"cp\">{{</span> <span class=\"nv\">year</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">article</span> <span class=\"k\">in</span> <span class=\"nv\">article_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">article.headline</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>By <span class=\"cp\">{{</span> <span class=\"nv\">article.reporter.full_name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>Published <span class=\"cp\">{{</span> <span class=\"nv\">article.pub_date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;F j, Y&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></figure>\n<p>Variables are surrounded by double-curly braces. <code class=\"docutils literal notranslate\">{{ article.headline }}</code>\nmeans “Output the value of the article’s headline attribute.” But dots aren’t\nused only for attribute lookup. They also can do dictionary-key lookup, index\nlookup and function calls.</p>\n<p>Note <code class=\"docutils literal notranslate\">{{ article.pub_date|date:&quot;F j, Y&quot; }}</code> uses a Unix-style “pipe” (the “|”\ncharacter). This is called a template filter, and it’s a way to filter the\nvalue of a variable. In this case, the date filter formats a Python datetime\nobject in the given format (as found in PHP’s date function).</p>\n<p>You can chain together as many filters as you’d like. You can write\n<a class=\"reference internal\" href=\"/en/6.1/howto/custom-template-tags/#howto-writing-custom-template-filters\"><span class=\"std std-ref\">custom template filters</span></a>. You can\nwrite <a class=\"reference internal\" href=\"/en/6.1/howto/custom-template-tags/\"><span class=\"doc\">custom template tags</span></a>, which run\ncustom Python code behind the scenes.</p>\n<p>Finally, Django uses the concept of “template inheritance”. That’s what the\n<code class=\"docutils literal notranslate\">{% extends &quot;base.html&quot; %}</code> does. It means “First load the template called\n‘base’, which has defined a bunch of blocks, and fill the blocks with the\nfollowing blocks.” In short, that lets you dramatically cut down on redundancy\nin templates: each template has to define only what’s unique to that template.</p>\n<p>Here’s what the “base.html” template, including the use of <a class=\"reference internal\" href=\"/en/6.1/howto/static-files/\"><span class=\"doc\">static files</span></a>, might look like:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">templates/base.html</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">html</span> <span class=\"na\">lang</span><span class=\"o\">=</span><span class=\"s\">&quot;en&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">title</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span><span class=\"p\">&lt;/</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">img</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">static</span> <span class=\"s1\">&#39;images/sitelogo.png&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Logo&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">html</span><span class=\"p\">&gt;</span>\n</code></pre></figure>\n<p>Simplistically, it defines the look-and-feel of the site (with the site’s\nlogo), and provides “holes” for child templates to fill. This means that a site\nredesign can be done by changing a single file – the base template.</p>\n<p>It also lets you create multiple versions of a site, with different base\ntemplates, while reusing child templates. Django’s creators have used this\ntechnique to create strikingly different mobile versions of sites by only\ncreating a new base template.</p>\n<p>Note that you don’t have to use Django’s template system if you prefer another\nsystem. While Django’s template system is particularly well-integrated with\nDjango’s model layer, nothing forces you to use it. For that matter, you don’t\nhave to use Django’s database API, either. You can use another database\nabstraction layer, you can read XML files, you can read files off disk, or\nanything you want. Each piece of Django – models, views, templates – is\ndecoupled from the next.</p>\n</section>\n<section id=\"this-is-just-the-surface\">\n<h2>This is just the surface<a class=\"heading-anchor\" href=\"#this-is-just-the-surface\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>This has been only a quick overview of Django’s functionality. Some more useful\nfeatures:</p>\n<ul class=\"simple\">\n<li><p>A <a class=\"reference internal\" href=\"/en/6.1/topics/cache/\"><span class=\"doc\">caching framework</span></a> that integrates with memcached\nor other backends.</p></li>\n<li><p>A <a class=\"reference internal\" href=\"/en/6.1/ref/contrib/syndication/\"><span class=\"doc\">syndication framework</span></a> that lets you\ncreate RSS and Atom feeds by writing a small Python class.</p></li>\n<li><p>More attractive automatically-generated admin features – this overview\nbarely scratched the surface.</p></li>\n</ul>\n<p>The next steps are for you to <a class=\"reference external\" href=\"https://www.djangoproject.com/download/\">download Django</a>, read <a class=\"reference internal\" href=\"/en/6.1/intro/tutorial01/\"><span class=\"doc\">the tutorial</span></a> and join <a class=\"reference external\" href=\"https://www.djangoproject.com/community/\">the community</a>. Thanks for your interest!</p>\n</section>","rootId":"django-at-a-glance","toc":[{"title":"Design your model","anchor":"design-your-model","children":[]},{"title":"Install it","anchor":"install-it","children":[]},{"title":"Enjoy the free API","anchor":"enjoy-the-free-api","children":[]},{"title":"A dynamic admin interface: it’s not just scaffolding – it’s the whole house","anchor":"a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house","children":[]},{"title":"Design your URLs","anchor":"design-your-urls","children":[]},{"title":"Write your views","anchor":"write-your-views","children":[]},{"title":"Design your templates","anchor":"design-your-templates","children":[]},{"title":"This is just the surface","anchor":"this-is-just-the-surface","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/6.1/intro/"}],"prev":{"docname":"intro/index","title":"Getting started","url":"/en/6.1/intro/"},"next":{"docname":"intro/install","title":"Quick install guide","url":"/en/6.1/intro/install/"},"formats":{"html":"/en/6.1/intro/overview/","markdown":"/en/6.1/intro/overview.md","json":"/en/6.1/intro/overview.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/intro/overview.txt","official":"https://docs.djangoproject.com/en/6.1/intro/overview/","inVersions":["6.1"],"inLocales":["en"]}