{"title":"Writing your first Django app, part 1","version":"5.1","locale":"en","docname":"intro/tutorial01","url":"/en/5.1/intro/tutorial01/","canonical":"https://djangodocs.dev/en/5.1/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/5.1/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=\"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<span class=\"w\"> </span>-m<span class=\"w\"> </span>django<span class=\"w\"> </span>--version\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 -m django --version\n</code></pre></div></div></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 5.1, which supports Python 3.10 and\nlater. If the Django version doesn’t match, you can refer to the tutorial for\nyour version of Django by using the version switcher at the bottom right corner\nof this page, or update Django to the newest version. If you’re using an older\nversion of Python, check <a class=\"reference internal\" href=\"/en/5.1/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">What Python version can I use with Django?</span></a> to find a compatible\nversion of Django.</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 head over to\nthe <a class=\"reference internal\" href=\"/en/5.1/faq/help/\"><span class=\"doc\">Getting Help</span></a> section of the FAQ.</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/5.1/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 and create a new directory named <code class=\"docutils literal notranslate\"><span class=\"pre\">djangotutorial</span></code>. (This directory name\ndoesn’t matter to Django; you can rename it to anything you like.)</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-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>mkdir<span class=\"w\"> </span>djangotutorial\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-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> <span class=\"k\">mkdir</span> djangotutorial\n</code></pre></div></div></div>\n<p>Then, run the following command to bootstrap a new Django project:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-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>django-admin<span class=\"w\"> </span>startproject<span class=\"w\"> </span>mysite<span class=\"w\"> </span>djangotutorial\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-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> django-admin startproject mysite djangotutorial\n</code></pre></div></div></div>\n<p>This will create a project called <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code> inside the <code class=\"docutils literal notranslate\"><span class=\"pre\">djangotutorial</span></code>\ndirectory. If it didn’t work, see <a class=\"reference internal\" href=\"/en/5.1/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<p>Let’s look at what <a class=\"reference internal\" href=\"/en/5.1/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=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>djangotutorial/\n    manage.py\n    mysite/\n        __init__.py\n        settings.py\n        urls.py\n        asgi.py\n        wsgi.py\n</code></pre></div>\n<p>These files are:</p>\n<ul class=\"simple\">\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/5.1/ref/django-admin/\"><span class=\"doc\">django-admin and manage.py</span></a>.</p></li>\n<li><p><code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/</span></code>: A directory that 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/5.1/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/5.1/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/asgi.py</span></code>: An entry-point for ASGI-compatible web servers to\nserve your project. See <a class=\"reference internal\" href=\"/en/5.1/howto/deployment/asgi/\"><span class=\"doc\">How to deploy with ASGI</span></a> for more details.</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/5.1/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 <code class=\"file docutils literal notranslate\"><span class=\"pre\">djangotutorial</span></code>\ndirectory, if you haven’t already, and run the following commands:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-3-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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-3-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 runserver\n</code></pre></div></div></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 5.1, 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>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 “Congratulations!” page, with a rocket taking off.\nIt worked!</p>\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>(To serve the site on a different port, see the <a class=\"reference internal\" href=\"/en/5.1/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 class=\"admonition-automatic-reloading-of-djadmin-runserver admonition\">\n<p class=\"admonition-title\">Automatic reloading of <a class=\"reference internal\" href=\"/en/5.1/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 blog system, a database of\npublic records or a small 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 in 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 inside the <code class=\"docutils literal notranslate\"><span class=\"pre\">djangotutorial</span></code> folder.</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=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-4-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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>startapp<span class=\"w\"> </span>polls\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-4-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 startapp polls\n</code></pre></div></div></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=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>polls/\n    __init__.py\n    admin.py\n    apps.py\n    migrations/\n        __init__.py\n    models.py\n    tests.py\n    views.py\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=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></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=\"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 most basic view possible in Django. To access it in a browser, we\nneed to map it to a URL - and for this we need to define a URL configuration,\nor “URLconf” for short. These URL configurations are defined inside each\nDjango app, and they are Python files named <code class=\"docutils literal notranslate\"><span class=\"pre\">urls.py</span></code>.</p>\n<p>To define a URLconf for the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> app, create a file <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></code>\nwith the following content:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></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=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</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\">path</span><span class=\"p\">(</span><span class=\"s2\">&quot;&quot;</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=\"s2\">&quot;index&quot;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Your app directory should now look like:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>polls/\n    __init__.py\n    admin.py\n    apps.py\n    migrations/\n        __init__.py\n    models.py\n    tests.py\n    urls.py\n    views.py\n</code></pre></div>\n<p>The next step is to configure the global URLconf in the <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code> project to\ninclude the URLconf defined in <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code>. To do this, add an import for\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.urls.include</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></code> and insert an\n<a class=\"reference internal\" href=\"/en/5.1/ref/urls/#django.urls.include\" title=\"django.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=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></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=\"w\"> </span><span class=\"nn\">django.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">admin</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls/&quot;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls.urls&quot;</span><span class=\"p\">)),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s2\">&quot;admin/&quot;</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/5.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> function expects at least two arguments:\n<code class=\"docutils literal notranslate\"><span class=\"pre\">route</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">view</span></code>.\nThe <a class=\"reference internal\" href=\"/en/5.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> function allows referencing other URLconfs.\nWhenever Django encounters <a class=\"reference internal\" href=\"/en/5.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>, it chops off whatever\npart of the URL matched up to that point and sends the remaining string to the\nincluded URLconf for further processing.</p>\n<p>The idea behind <a class=\"reference internal\" href=\"/en/5.1/ref/urls/#django.urls.include\" title=\"django.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-urls-include admonition\">\n<p class=\"admonition-title\">When to use <a class=\"reference internal\" href=\"/en/5.1/ref/urls/#django.urls.include\" title=\"django.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.\nThe only exception is <code class=\"docutils literal notranslate\"><span class=\"pre\">admin.site.urls</span></code>, which is a pre-built URLconf\nprovided by Django for the default admin site.</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. Verify it’s working with\nthe following command:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-5-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<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-5-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 runserver\n</code></pre></div></div></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<aside class=\"admonition-page-not-found admonition\">\n<p class=\"admonition-title\">Page not found?</p>\n<p>If you get an error page here, check that you’re going to\n<a class=\"reference external\" href=\"http://localhost:8000/polls/\">http://localhost:8000/polls/</a> and not <a class=\"reference external\" href=\"http://localhost:8000/\">http://localhost:8000/</a>.</p>\n</aside>\n<p>When you’re comfortable with the basic request and response flow, read\n<a class=\"reference internal\" href=\"/en/5.1/intro/tutorial02/\"><span class=\"doc\">part 2 of this tutorial</span></a> to start working with the\ndatabase.</p>\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":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/5.1/intro/"}],"prev":{"docname":"intro/install","title":"Quick install guide","url":"/en/5.1/intro/install/"},"next":{"docname":"intro/tutorial02","title":"Writing your first Django app, part 2","url":"/en/5.1/intro/tutorial02/"},"formats":{"html":"/en/5.1/intro/tutorial01/","markdown":"/en/5.1/intro/tutorial01.md","json":"/en/5.1/intro/tutorial01.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/intro/tutorial01.txt","official":"https://docs.djangoproject.com/en/5.1/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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}