{"title":"Writing your first Django app, part 1","version":"1.11","locale":"en","docname":"intro/tutorial01","url":"/en/1.11/intro/tutorial01/","canonical":"https://djangodocs.dev/en/1.11/intro/tutorial01/","summary":"Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic poll application. It’ll consist of two parts: A public site that…","html":"<h1>Writing your first Django app, part 1<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Let’s learn by example.</p>\n<p>Throughout this tutorial, we’ll walk you through the creation of a basic\npoll application.</p>\n<p>It’ll consist of two parts:</p>\n<ul class=\"simple\">\n<li><p>A public site that lets people view polls and vote in them.</p></li>\n<li><p>An admin site that lets you add, change, and delete polls.</p></li>\n</ul>\n<p>We’ll assume you have <a class=\"reference internal\" href=\"/en/1.11/intro/install/\"><span class=\"doc\">Django installed</span></a> already. You can\ntell Django is installed and which version by running the following command\nin a shell prompt (indicated by the $ prefix):</p>\n<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<span class=\"w\"> </span>-m<span class=\"w\"> </span>django<span class=\"w\"> </span>--version\n</code></pre></div>\n<p>If Django is installed, you should see the version of your installation. If it\nisn’t, you’ll get an error telling “No module named django”.</p>\n<p>This tutorial is written for Django 1.11 and Python 3.4 or later. If the\nDjango version doesn’t match, you can refer to the tutorial for your version\nof Django by using the version switcher at the bottom right corner of this\npage, or update Django to the newest version. If you are still using Python\n2.7, you will need to adjust the code samples slightly, as described in\ncomments.</p>\n<p>See <a class=\"reference internal\" href=\"/en/1.11/topics/install/\"><span class=\"doc\">How to install Django</span></a> for advice on how to remove\nolder versions of Django and install a newer one.</p>\n<aside class=\"admonition-where-to-get-help admonition\">\n<p class=\"admonition-title\">Where to get help:</p>\n<p>If you’re having trouble going through this tutorial, please post a message\nto <a class=\"reference internal\" href=\"/en/1.11/internals/mailing-lists/#django-users-mailing-list\"><span class=\"std std-ref\">django-users</span></a> or drop by <a class=\"reference external\" href=\"irc://irc.freenode.net/django\">#django on irc.freenode.net</a> to chat with other Django users who might\nbe able to help.</p>\n</aside>\n<section id=\"creating-a-project\">\n<h2>Creating a project<a class=\"heading-anchor\" href=\"#creating-a-project\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If this is your first time using Django, you’ll have to take care of some\ninitial setup. Namely, you’ll need to auto-generate some code that establishes a\nDjango <a class=\"reference internal\" href=\"/en/1.11/glossary/#term-project\"><span class=\"xref std std-term\">project</span></a> – a collection of settings for an instance of Django,\nincluding database configuration, Django-specific options and\napplication-specific settings.</p>\n<p>From the command line, <code class=\"docutils literal notranslate\"><span class=\"pre\">cd</span></code> into a directory where you’d like to store your\ncode, then run the following command:</p>\n<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>django-admin<span class=\"w\"> </span>startproject<span class=\"w\"> </span>mysite\n</code></pre></div>\n<p>This will create a <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code> directory in your current directory. If it didn’t\nwork, see <a class=\"reference internal\" href=\"/en/1.11/faq/troubleshooting/#troubleshooting-django-admin\"><span class=\"std std-ref\">Problems running django-admin</span></a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>You’ll need to avoid naming projects after built-in Python or Django\ncomponents. In particular, this means you should avoid using names like\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> (which will conflict with Django itself) or <code class=\"docutils literal notranslate\"><span class=\"pre\">test</span></code> (which\nconflicts with a built-in Python package).</p>\n</aside>\n<aside class=\"admonition-where-should-this-code-live admonition\">\n<p class=\"admonition-title\">Where should this code live?</p>\n<p>If your background is in plain old PHP (with no use of modern frameworks),\nyou’re probably used to putting code under the Web server’s document root\n(in a place such as <code class=\"docutils literal notranslate\"><span class=\"pre\">/var/www</span></code>). With Django, you don’t do that. It’s\nnot a good idea to put any of this Python code within your Web server’s\ndocument root, because it risks the possibility that people may be able\nto view your code over the Web. That’s not good for security.</p>\n<p>Put your code in some directory <strong>outside</strong> of the document root, such as\n<code class=\"file docutils literal notranslate\"><span class=\"pre\">/home/mycode</span></code>.</p>\n</aside>\n<p>Let’s look at what <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> created:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code><span class=\"n\">mysite</span><span class=\"o\">/</span>\n    <span class=\"n\">manage</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">mysite</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">urls</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">wsgi</span><span class=\"o\">.</span><span class=\"n\">py</span>\n</code></pre></div>\n<p>These files are:</p>\n<ul class=\"simple\">\n<li><p>The outer <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/</span></code> root directory is just a container for your\nproject. Its name doesn’t matter to Django; you can rename it to anything\nyou like.</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>: A command-line utility that lets you interact with this\nDjango project in various ways. You can read all the details about\n<code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> in <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/\"><span class=\"doc\">django-admin and manage.py</span></a>.</p></li>\n<li><p>The inner <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/</span></code> directory is the actual Python package for your\nproject. Its name is the Python package name you’ll need to use to import\nanything inside it (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.urls</span></code>).</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/__init__.py</span></code>: An empty file that tells Python that this\ndirectory should be considered a Python package. If you’re a Python beginner,\nread <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/modules.html#tut-packages\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">more about packages</span></a> in the official Python docs.</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code>: Settings/configuration for this Django\nproject.  <a class=\"reference internal\" href=\"/en/1.11/topics/settings/\"><span class=\"doc\">Django settings</span></a> will tell you all about how settings\nwork.</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></code>: The URL declarations for this Django project; a\n“table of contents” of your Django-powered site. You can read more about\nURLs in <a class=\"reference internal\" href=\"/en/1.11/topics/http/urls/\"><span class=\"doc\">URL dispatcher</span></a>.</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/wsgi.py</span></code>: An entry-point for WSGI-compatible web servers to\nserve your project. See <a class=\"reference internal\" href=\"/en/1.11/howto/deployment/wsgi/\"><span class=\"doc\">How to deploy with WSGI</span></a> for more details.</p></li>\n</ul>\n</section>\n<section id=\"the-development-server\">\n<h2>The development server<a class=\"heading-anchor\" href=\"#the-development-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Let’s verify your Django project works. Change into the outer <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite</span></code> directory, if\nyou haven’t already, and run the following commands:</p>\n<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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n<p>You’ll see the following output on the command line:</p>\n<pre class=\"literal-block\">Performing system checks...\n\nSystem check identified no issues (0 silenced).\n\nYou have unapplied migrations; your app may not work properly until they are applied.\nRun 'python manage.py migrate' to apply them.\n\nAug 09, 2026 - 15:50:53\nDjango version 1.11, using settings 'mysite.settings'\nStarting development server at <a class=\"reference external\" href=\"http://127.0.0.1:8000/\">http://127.0.0.1:8000/</a>\nQuit the server with CONTROL-C.</pre>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Ignore the warning about unapplied database migrations for now; we’ll deal\nwith the database shortly.</p>\n</aside>\n<p>You’ve started the Django development server, a lightweight Web server written\npurely in Python. We’ve included this with Django so you can develop things\nrapidly, without having to deal with configuring a production server – such as\nApache – until you’re ready for production.</p>\n<p>Now’s a good time to note: <strong>don’t</strong> use this server in anything resembling a\nproduction environment. It’s intended only for use while developing. (We’re in\nthe business of making Web frameworks, not Web servers.)</p>\n<p>Now that the server’s running, visit <a class=\"reference external\" href=\"http://127.0.0.1:8000/\">http://127.0.0.1:8000/</a> with your Web\nbrowser. You’ll see a “Welcome to Django” page, in pleasant, light-blue pastel.\nIt worked!</p>\n<aside class=\"admonition-changing-the-port admonition\">\n<p class=\"admonition-title\">Changing the port</p>\n<p>By default, the <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> command starts the development server\non the internal IP at port 8000.</p>\n<p>If you want to change the server’s port, pass\nit as a command-line argument. For instance, this command starts the server\non port 8080:</p>\n<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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver<span class=\"w\"> </span><span class=\"m\">8080</span>\n</code></pre></div>\n<p>If you want to change the server’s IP, pass it along with the port. For\nexample, to listen on all available public IPs (which is useful if you are\nrunning Vagrant or want to show off your work on other computers on the\nnetwork), use:</p>\n<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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver<span class=\"w\"> </span><span class=\"m\">0</span>:8000\n</code></pre></div>\n<p><strong>0</strong> is a shortcut for <strong>0.0.0.0</strong>. Full docs for the development server\ncan be found in the <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> reference.</p>\n</aside>\n<aside class=\"admonition-automatic-reloading-of-djadmin-runserver admonition\">\n<p class=\"admonition-title\">Automatic reloading of <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a></p>\n<p>The development server automatically reloads Python code for each request\nas needed. You don’t need to restart the server for code changes to take\neffect. However, some actions like adding files don’t trigger a restart,\nso you’ll have to restart the server in these cases.</p>\n</aside>\n</section>\n<section id=\"creating-the-polls-app\">\n<h2>Creating the Polls app<a class=\"heading-anchor\" href=\"#creating-the-polls-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now that your environment – a “project” – is set up, you’re set to start\ndoing work.</p>\n<p>Each application you write in Django consists of a Python package that follows\na certain convention. Django comes with a utility that automatically generates\nthe basic directory structure of an app, so you can focus on writing code\nrather than creating directories.</p>\n<aside class=\"admonition-projects-vs-apps admonition\">\n<p class=\"admonition-title\">Projects vs. apps</p>\n<p>What’s the difference between a project and an app? An app is a Web\napplication that does something – e.g., a Weblog system, a database of\npublic records or a simple poll app. A project is a collection of\nconfiguration and apps for a particular website. A project can contain\nmultiple apps. An app can be in multiple projects.</p>\n</aside>\n<p>Your apps can live anywhere on your <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/modules.html#tut-searchpath\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">Python path</span></a>. In\nthis tutorial, we’ll create our poll app right next to your <code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>\nfile so that it can be imported as its own top-level module, rather than a\nsubmodule of <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code>.</p>\n<p>To create your app, make sure you’re in the same directory as <code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>\nand type this command:</p>\n<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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>startapp<span class=\"w\"> </span>polls\n</code></pre></div>\n<p>That’ll create a directory <code class=\"file docutils literal notranslate\"><span class=\"pre\">polls</span></code>, which is laid out like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code><span class=\"n\">polls</span><span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">apps</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">migrations</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">py</span>\n</code></pre></div>\n<p>This directory structure will house the poll application.</p>\n</section>\n<section id=\"write-your-first-view\">\n<h2>Write your first view<a class=\"heading-anchor\" href=\"#write-your-first-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Let’s write the first view. Open the file <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code>\nand put the following Python code in it:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hello, world. You&#39;re at the polls index.&quot;</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>This is the simplest view possible in Django. To call the view, we need to map\nit to a URL - and for this we need a URLconf.</p>\n<p>To create a URLconf in the polls directory, create a file called <code class=\"docutils literal notranslate\"><span class=\"pre\">urls.py</span></code>.\nYour app directory should now look like:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code><span class=\"n\">polls</span><span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">apps</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">migrations</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">urls</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">py</span>\n</code></pre></div>\n<p>In the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></code> file include the following code:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/urls.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"polls/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">url</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">index</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The next step is to point the root URLconf at the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> module. In\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></code>, add an import for <code class=\"docutils literal notranslate\"><span class=\"pre\">django.conf.urls.include</span></code> and insert\nan <a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> in the <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> list, so you have:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>mysite/urls.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"mysite/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">url</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">admin</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^admin/&#39;</span><span class=\"p\">,</span> <span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">site</span><span class=\"o\">.</span><span class=\"n\">urls</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The <a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> function allows referencing other\nURLconfs. Note that the regular expressions for the\n<a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> function doesn’t have a <code class=\"docutils literal notranslate\"><span class=\"pre\">$</span></code> (end-of-string\nmatch character) but rather a trailing slash. Whenever Django encounters\n<a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>, it chops off whatever part of the URL\nmatched up to that point and sends the remaining string to the included URLconf\nfor further processing.</p>\n<p>The idea behind <a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> is to make it easy to\nplug-and-play URLs. Since polls are in their own URLconf\n(<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></code>), they can be placed under “/polls/”, or under\n“/fun_polls/”, or under “/content/polls/”, or any other path root, and the\napp will still work.</p>\n<aside class=\"admonition-when-to-use-func-django-conf-urls-include admonition\">\n<p class=\"admonition-title\">When to use <a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a></p>\n<p>You should always use <code class=\"docutils literal notranslate\"><span class=\"pre\">include()</span></code> when you include other URL patterns.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">admin.site.urls</span></code> is the only exception to this.</p>\n</aside>\n<aside class=\"admonition-doesn-t-match-what-you-see admonition\">\n<p class=\"admonition-title\">Doesn’t match what you see?</p>\n<p>If you’re seeing <code class=\"docutils literal notranslate\"><span class=\"pre\">include(admin.site.urls)</span></code> instead of just\n<code class=\"docutils literal notranslate\"><span class=\"pre\">admin.site.urls</span></code>, you’re probably using a version of Django that\ndoesn’t match this tutorial version.  You’ll want to either switch to the\nolder tutorial or the newer Django version.</p>\n</aside>\n<p>You have now wired an <code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> view into the URLconf. Lets verify it’s\nworking, run the following command:</p>\n<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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n<p>Go to <a class=\"reference external\" href=\"http://localhost:8000/polls/\">http://localhost:8000/polls/</a> in your browser, and you should see the\ntext “<em>Hello, world. You’re at the polls index.</em>”, which you defined in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> view.</p>\n<p>The <a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> function is passed four arguments, two\nrequired: <code class=\"docutils literal notranslate\"><span class=\"pre\">regex</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">view</span></code>, and two optional: <code class=\"docutils literal notranslate\"><span class=\"pre\">kwargs</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code>.\nAt this point, it’s worth reviewing what these arguments are for.</p>\n<section id=\"url-argument-regex\">\n<h3><a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: regex<a class=\"heading-anchor\" href=\"#url-argument-regex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The term “regex” is a commonly used short form meaning “regular expression”,\nwhich is a syntax for matching patterns in strings, or in this case, url\npatterns. Django starts at the first regular expression and makes its way down\nthe list,  comparing the requested URL against each regular expression until it\nfinds one that matches.</p>\n<p>Note that these regular expressions do not search GET and POST parameters, or\nthe domain name. For example, in a request to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://www.example.com/myapp/</span></code>, the URLconf will look for <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code>. In a\nrequest to <code class=\"docutils literal notranslate\"><span class=\"pre\">https://www.example.com/myapp/?page=3</span></code>, the URLconf will also\nlook for <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code>.</p>\n<p>If you need help with regular expressions, see <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Regular_expression\">Wikipedia’s entry</a> and the\ndocumentation of the <a class=\"reference external\" href=\"https://docs.python.org/3/library/re.html#module-re\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">re</span></code></a> module. Also, the O’Reilly book “Mastering\nRegular Expressions” by Jeffrey Friedl is fantastic. In practice, however,\nyou don’t need to be an expert on regular expressions, as you really only need\nto know how to capture simple patterns. In fact, complex regexes can have poor\nlookup performance, so you probably shouldn’t rely on the full power of regexes.</p>\n<p>Finally, a performance note: these regular expressions are compiled the first\ntime the URLconf module is loaded. They’re super fast (as long as the lookups\naren’t too complex as noted above).</p>\n</section>\n<section id=\"url-argument-view\">\n<h3><a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: view<a class=\"heading-anchor\" href=\"#url-argument-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When Django finds a regular expression match, Django calls the specified view\nfunction, with an <a class=\"reference internal\" href=\"/en/1.11/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object as the first\nargument and any “captured” values from the regular expression as other\narguments. If the regex uses simple captures, values are passed as positional\narguments; if it uses named captures, values are passed as keyword arguments.\nWe’ll give an example of this in a bit.</p>\n</section>\n<section id=\"url-argument-kwargs\">\n<h3><a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: kwargs<a class=\"heading-anchor\" href=\"#url-argument-kwargs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Arbitrary keyword arguments can be passed in a dictionary to the target view. We\naren’t going to use this feature of Django in the tutorial.</p>\n</section>\n<section id=\"url-argument-name\">\n<h3><a class=\"reference internal\" href=\"/en/1.11/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: name<a class=\"heading-anchor\" href=\"#url-argument-name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Naming your URL lets you refer to it unambiguously from elsewhere in Django,\nespecially from within templates. This powerful feature allows you to make\nglobal changes to the URL patterns of your project while only touching a single\nfile.</p>\n<p>When you’re comfortable with the basic request and response flow, read\n<a class=\"reference internal\" href=\"/en/1.11/intro/tutorial02/\"><span class=\"doc\">part 2 of this tutorial</span></a> to start working with the\ndatabase.</p>\n</section>\n</section>","rootId":"writing-your-first-django-app-part-1","toc":[{"title":"Creating a project","anchor":"creating-a-project","children":[]},{"title":"The development server","anchor":"the-development-server","children":[]},{"title":"Creating the Polls app","anchor":"creating-the-polls-app","children":[]},{"title":"Write your first view","anchor":"write-your-first-view","children":[{"title":"url() argument: regex","anchor":"url-argument-regex","children":[]},{"title":"url() argument: view","anchor":"url-argument-view","children":[]},{"title":"url() argument: kwargs","anchor":"url-argument-kwargs","children":[]},{"title":"url() argument: name","anchor":"url-argument-name","children":[]}]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/1.11/intro/"}],"prev":{"docname":"intro/install","title":"Quick install guide","url":"/en/1.11/intro/install/"},"next":{"docname":"intro/tutorial02","title":"Writing your first Django app, part 2","url":"/en/1.11/intro/tutorial02/"},"formats":{"html":"/en/1.11/intro/tutorial01/","markdown":"/en/1.11/intro/tutorial01.md","json":"/en/1.11/intro/tutorial01.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/intro/tutorial01.txt","official":"https://docs.djangoproject.com/en/1.11/intro/tutorial01/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}