{"title":"Writing your first Django app, part 1","version":"1.8","locale":"en","docname":"intro/tutorial01","url":"/en/1.8/intro/tutorial01/","canonical":"https://djangodocs.dev/en/1.8/intro/tutorial01/","summary":"Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic poll application.","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.8/intro/install/\"><span class=\"doc\">Django installed</span></a> already. You can\ntell Django is installed and which version by running 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 -c <span class=\"s2\">&quot;import django; print(django.get_version())&quot;</span>\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.8 and Python 3.2 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.8/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\" role=\"note\">\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.8/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.8/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\">cd</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 startproject mysite\n</code></pre></div>\n<p>This will create a <code class=\"docutils literal notranslate\">mysite</code> directory in your current directory. If it didn’t\nwork, see <a class=\"reference internal\" href=\"/en/1.8/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\">django</code> (which will conflict with Django itself) or <code class=\"docutils literal notranslate\">test</code> (which\nconflicts with a built-in Python package).</p>\n</aside>\n<aside class=\"admonition-where-should-this-code-live admonition\" role=\"note\">\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\">/var/www</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\">/home/mycode</code>.</p>\n</aside>\n<p>Let’s look at what <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</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>mysite<span class=\"o\">/</span>\n    manage<span class=\"o\">.</span>py\n    mysite<span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n        settings<span class=\"o\">.</span>py\n        urls<span class=\"o\">.</span>py\n        wsgi<span class=\"o\">.</span>py\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\">mysite/</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\">manage.py</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\">manage.py</code> in <a class=\"reference internal\" href=\"/en/1.8/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\">mysite/</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\">mysite.urls</code>).</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/__init__.py</code>: An empty file that tells Python that this\ndirectory should be considered a Python package. (Read <a class=\"reference external\" href=\"https://docs.python.org/tutorial/modules.html#packages\">more about\npackages</a> in the official Python docs if you’re a Python beginner.)</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/settings.py</code>: Settings/configuration for this Django\nproject.  <a class=\"reference internal\" href=\"/en/1.8/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\">mysite/urls.py</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.8/topics/http/urls/\"><span class=\"doc\">URL dispatcher</span></a>.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/wsgi.py</code>: An entry-point for WSGI-compatible web servers to\nserve your project. See <a class=\"reference internal\" href=\"/en/1.8/howto/deployment/wsgi/\"><span class=\"doc\">How to deploy with WSGI</span></a> for more details.</p></li>\n</ul>\n<section id=\"database-setup\">\n<h3>Database setup<a class=\"heading-anchor\" href=\"#database-setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Now, open up <code class=\"file docutils literal notranslate\">mysite/settings.py</code>. It’s a normal Python module with\nmodule-level variables representing Django settings.</p>\n<p>By default, the configuration uses SQLite. If you’re new to databases, or\nyou’re just interested in trying Django, this is the easiest choice. SQLite is\nincluded in Python, so you won’t need to install anything else to support your\ndatabase. When starting your first real project, however, you may want to use a\nmore robust database like PostgreSQL, to avoid database-switching headaches\ndown the road.</p>\n<p>If you wish to use another database, install the appropriate <a class=\"reference internal\" href=\"/en/1.8/topics/install/#database-installation\"><span class=\"std std-ref\">database\nbindings</span></a>, and change the following keys in the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> <code class=\"docutils literal notranslate\">'default'</code> item to match your database connection\nsettings:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\">ENGINE</code></a> – Either\n<code class=\"docutils literal notranslate\">'django.db.backends.sqlite3'</code>,\n<code class=\"docutils literal notranslate\">'django.db.backends.postgresql_psycopg2'</code>,\n<code class=\"docutils literal notranslate\">'django.db.backends.mysql'</code>, or\n<code class=\"docutils literal notranslate\">'django.db.backends.oracle'</code>. Other backends are <a class=\"reference internal\" href=\"/en/1.8/ref/databases/#third-party-notes\"><span class=\"std std-ref\">also available</span></a>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> – The name of your database. If you’re using SQLite, the\ndatabase will be a file on your computer; in that case, <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a>\nshould be the full absolute path, including filename, of that file. The\ndefault value, <code class=\"docutils literal notranslate\">os.path.join(BASE_DIR, 'db.sqlite3')</code>, will store the file\nin your project directory.</p></li>\n</ul>\n<p>If you are not using SQLite as your database, additional settings such as <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</code></a>, <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">PASSWORD</code></a>, <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\">HOST</code></a> must be added.\nFor more details, see the reference documentation for <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you’re using PostgreSQL or MySQL, make sure you’ve created a database by\nthis point. Do that with “<code class=\"docutils literal notranslate\">CREATE DATABASE database_name;</code>” within your\ndatabase’s interactive prompt.</p>\n<p>If you’re using SQLite, you don’t need to create anything beforehand - the\ndatabase file will be created automatically when it is needed.</p>\n</aside>\n<p>While you’re editing <code class=\"file docutils literal notranslate\">mysite/settings.py</code>, set <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\">TIME_ZONE</code></a> to\nyour time zone.</p>\n<p>Also, note the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting at the top of the file. That\nholds the names of all Django applications that are activated in this Django\ninstance. Apps can be used in multiple projects, and you can package and\ndistribute them for use by others in their projects.</p>\n<p>By default, <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> contains the following apps, all of which\ncome with Django:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.admin</code></a> – The admin site. You’ll use it in <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial02/\"><span class=\"doc\">part 2\nof this tutorial</span></a>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.auth</code></a> – An authentication system.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/contenttypes/#module-django.contrib.contenttypes\" title=\"django.contrib.contenttypes: Provides generic interface to installed models.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.contenttypes</code></a> – A framework for content types.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/topics/http/sessions/#module-django.contrib.sessions\" title=\"django.contrib.sessions: Provides session management for Django projects.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.sessions</code></a> – A session framework.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/messages/#module-django.contrib.messages\" title=\"django.contrib.messages: Provides cookie- and session-based temporary message storage.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.messages</code></a> – A messaging framework.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.staticfiles</code></a> – A framework for managing\nstatic files.</p></li>\n</ul>\n<p>These applications are included by default as a convenience for the common case.</p>\n<p>Some of these applications make use of at least one database table, though,\nso we need to create the tables in the database before we can use them. To do\nthat, 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 manage.py migrate\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> command looks at the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting\nand creates any necessary database tables according to the database settings\nin your <code class=\"file docutils literal notranslate\">mysite/settings.py</code> file and the database migrations shipped\nwith the app (we’ll cover those later). You’ll see a message for each\nmigration it applies. If you’re interested, run the command-line client for your\ndatabase and type <code class=\"docutils literal notranslate\">\\dt</code> (PostgreSQL), <code class=\"docutils literal notranslate\">SHOW TABLES;</code> (MySQL), <code class=\"docutils literal notranslate\">.schema</code>\n(SQLite), or <code class=\"docutils literal notranslate\">SELECT TABLE_NAME FROM USER_TABLES;</code> (Oracle) to display the\ntables Django created.</p>\n<aside class=\"admonition-for-the-minimalists admonition\" role=\"note\">\n<p class=\"admonition-title\">For the minimalists</p>\n<p>Like we said above, the default applications are included for the common\ncase, but not everybody needs them. If you don’t need any or all of them,\nfeel free to comment-out or delete the appropriate line(s) from\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> before running <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>. The\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> command will only run migrations for apps in\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>.</p>\n</aside>\n</section>\n<section id=\"the-development-server\">\n<h3>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></h3>\n<p>Let’s verify your Django project works. Change into the outer <code class=\"file docutils literal notranslate\">mysite</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 manage.py 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\n0 errors found\nAug 09, 2026 - 15:50:53\nDjango version 1.8, 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<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\" role=\"note\">\n<p class=\"admonition-title\">Changing the port</p>\n<p>By default, the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</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 manage.py runserver <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. So to\nlisten on all public IPs (useful if you want to show off your work on other\ncomputers on your network), 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 manage.py runserver <span class=\"m\">0</span>.0.0.0:8000\n</code></pre></div>\n<p>Full docs for the development server can be found in the\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a> reference.</p>\n</aside>\n<aside class=\"admonition-automatic-reloading-of-djadmin-runserver admonition\" role=\"note\">\n<p class=\"admonition-title\">Automatic reloading of <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</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>\n<section id=\"creating-models\">\n<span id=\"id1\"></span><h2>Creating models<a class=\"heading-anchor\" href=\"#creating-models\"><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\" role=\"note\">\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 Web site. 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/tutorial/modules.html#the-module-search-path\">Python path</a>. In this tutorial, we’ll\ncreate our poll app right next to your <code class=\"file docutils literal notranslate\">manage.py</code> file so that it can be\nimported as its own top-level module, rather than a submodule of <code class=\"docutils literal notranslate\">mysite</code>.</p>\n<p>To create your app, make sure you’re in the same directory as <code class=\"file docutils literal notranslate\">manage.py</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 manage.py startapp polls\n</code></pre></div>\n<p>That’ll create a directory <code class=\"file docutils literal notranslate\">polls</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>polls<span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    admin<span class=\"o\">.</span>py\n    migrations<span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    models<span class=\"o\">.</span>py\n    tests<span class=\"o\">.</span>py\n    views<span class=\"o\">.</span>py\n</code></pre></div>\n<p>This directory structure will house the poll application.</p>\n<p>The first step in writing a database Web app in Django is to define your models\n– essentially, your database layout, with additional metadata.</p>\n<aside class=\"admonition-philosophy admonition\" role=\"note\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>A model is the single, definitive source of truth about your data. It contains\nthe essential fields and behaviors of the data you’re storing. Django follows\nthe <a class=\"reference internal\" href=\"/en/1.8/misc/design-philosophies/#dry\"><span class=\"std std-ref\">DRY Principle</span></a>. The goal is to define your data model in one\nplace and automatically derive things from it.</p>\n<p>This includes the migrations - unlike in Ruby On Rails, for example, migrations\nare entirely derived from your models file, and are essentially just a\nhistory that Django can roll through to update your database schema to\nmatch your current models.</p>\n</aside>\n<p>In our simple poll app, we’ll create two models: <code class=\"docutils literal notranslate\">Question</code> and <code class=\"docutils literal notranslate\">Choice</code>.\nA <code class=\"docutils literal notranslate\">Question</code> has a question and a publication date. A <code class=\"docutils literal notranslate\">Choice</code> has two fields:\nthe text of the choice and a vote tally. Each <code class=\"docutils literal notranslate\">Choice</code> is associated with a\n<code class=\"docutils literal notranslate\">Question</code>.</p>\n<p>These concepts are represented by simple Python classes. Edit the\n<code class=\"file docutils literal notranslate\">polls/models.py</code> file so it looks like this:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.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/models.py\"><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\">Question</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    question_text <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    pub_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateTimeField<span class=\"p\">(</span><span class=\"s1\">&#39;date published&#39;</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Choice</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    question <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Question<span class=\"p\">)</span>\n    choice_text <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    votes <span class=\"o\">=</span> models<span class=\"o\">.</span>IntegerField<span class=\"p\">(</span>default<span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>The code is straightforward. Each model is represented by a class that\nsubclasses <a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\">django.db.models.Model</code></a>. Each model has a number of class\nvariables, each of which represents a database field in the model.</p>\n<p>Each field is represented by an instance of a <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a>\nclass – e.g., <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> for character fields and\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\">DateTimeField</code></a> for datetimes. This tells Django what\ntype of data each field holds.</p>\n<p>The name of each <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a> instance (e.g. <code class=\"docutils literal notranslate\">question_text</code> or\n<code class=\"docutils literal notranslate\">pub_date</code>) is the field’s name, in machine-friendly format. You’ll use this\nvalue in your Python code, and your database will use it as the column name.</p>\n<p>You can use an optional first positional argument to a\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a> to designate a human-readable name. That’s used\nin a couple of introspective parts of Django, and it doubles as documentation.\nIf this field isn’t provided, Django will use the machine-readable name. In this\nexample, we’ve only defined a human-readable name for <code class=\"docutils literal notranslate\">Question.pub_date</code>. For all\nother fields in this model, the field’s machine-readable name will suffice as\nits human-readable name.</p>\n<p>Some <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a> classes have required arguments.\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a>, for example, requires that you give it a\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a>. That’s used not only in the\ndatabase schema, but in validation, as we’ll soon see.</p>\n<p>A <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a> can also have various optional arguments; in\nthis case, we’ve set the <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.Field.default\" title=\"django.db.models.Field.default\"><code class=\"xref py py-attr docutils literal notranslate\">default</code></a> value of\n<code class=\"docutils literal notranslate\">votes</code> to 0.</p>\n<p>Finally, note a relationship is defined, using\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>. That tells Django each <code class=\"docutils literal notranslate\">Choice</code> is related\nto a single <code class=\"docutils literal notranslate\">Question</code>. Django supports all the common database relationships:\nmany-to-one, many-to-many and one-to-one.</p>\n</section>\n<section id=\"activating-models\">\n<h2>Activating models<a class=\"heading-anchor\" href=\"#activating-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>That small bit of model code gives Django a lot of information. With it, Django\nis able to:</p>\n<ul class=\"simple\">\n<li><p>Create a database schema (<code class=\"docutils literal notranslate\">CREATE TABLE</code> statements) for this app.</p></li>\n<li><p>Create a Python database-access API for accessing <code class=\"docutils literal notranslate\">Question</code> and <code class=\"docutils literal notranslate\">Choice</code> objects.</p></li>\n</ul>\n<p>But first we need to tell our project that the <code class=\"docutils literal notranslate\">polls</code> app is installed.</p>\n<aside class=\"admonition-philosophy admonition\" role=\"note\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>Django apps are “pluggable”: You can use an app in multiple projects, and\nyou can distribute apps, because they don’t have to be tied to a given\nDjango installation.</p>\n</aside>\n<p>Edit the <code class=\"file docutils literal notranslate\">mysite/settings.py</code> file again, and change the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting to include the string <code class=\"docutils literal notranslate\">'polls'</code>. So it’ll\nlook like this:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>mysite/settings.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/settings.py\"><code>INSTALLED_APPS <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s1\">&#39;django.contrib.admin&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.contenttypes&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.sessions&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.messages&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.staticfiles&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;polls&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></figure>\n<p>Now Django knows to include the <code class=\"docutils literal notranslate\">polls</code> app. Let’s run another 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 manage.py makemigrations polls\n</code></pre></div>\n<p>You should see something similar to the following:</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>Migrations for &#39;polls&#39;:\n  0001_initial.py:\n    - Create model Question\n    - Create model Choice\n    - Add field question to choice\n</code></pre></div>\n<p>By running <code class=\"docutils literal notranslate\">makemigrations</code>, you’re telling Django that you’ve made\nsome changes to your models (in this case, you’ve made new ones) and that\nyou’d like the changes to be stored as a <em>migration</em>.</p>\n<p>Migrations are how Django stores changes to your models (and thus your\ndatabase schema) - they’re just files on disk. You can read the migration\nfor your new model if you like; it’s the file\n<code class=\"docutils literal notranslate\">polls/migrations/0001_initial.py</code>. Don’t worry, you’re not expected to read\nthem every time Django makes one, but they’re designed to be human-editable\nin case you want to manually tweak how Django changes things.</p>\n<p>There’s a command that will run the migrations for you and manage your database\nschema automatically - that’s called <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>, and we’ll come to it in a\nmoment - but first, let’s see what SQL that migration would run. The\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-sqlmigrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">sqlmigrate</code></a> command takes migration names and returns their SQL:</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 manage.py sqlmigrate polls <span class=\"m\">0001</span>\n</code></pre></div>\n<p>You should see something similar to the following (we’ve reformatted it for\nreadability):</p>\n<div class=\"code-block\" data-language=\"sql\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">SQL</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=\"SQL code\"><code><span class=\"k\">BEGIN</span><span class=\"p\">;</span>\n<span class=\"k\">CREATE</span> <span class=\"k\">TABLE</span> <span class=\"ss\">&quot;polls_choice&quot;</span> <span class=\"p\">(</span>\n    <span class=\"ss\">&quot;id&quot;</span> <span class=\"nb\">serial</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span> <span class=\"k\">PRIMARY</span> <span class=\"k\">KEY</span><span class=\"p\">,</span>\n    <span class=\"ss\">&quot;choice_text&quot;</span> <span class=\"nb\">varchar</span><span class=\"p\">(</span><span class=\"mi\">200</span><span class=\"p\">)</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span><span class=\"p\">,</span>\n    <span class=\"ss\">&quot;votes&quot;</span> <span class=\"nb\">integer</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span>\n<span class=\"p\">);</span>\n<span class=\"k\">CREATE</span> <span class=\"k\">TABLE</span> <span class=\"ss\">&quot;polls_question&quot;</span> <span class=\"p\">(</span>\n    <span class=\"ss\">&quot;id&quot;</span> <span class=\"nb\">serial</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span> <span class=\"k\">PRIMARY</span> <span class=\"k\">KEY</span><span class=\"p\">,</span>\n    <span class=\"ss\">&quot;question_text&quot;</span> <span class=\"nb\">varchar</span><span class=\"p\">(</span><span class=\"mi\">200</span><span class=\"p\">)</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span><span class=\"p\">,</span>\n    <span class=\"ss\">&quot;pub_date&quot;</span> <span class=\"k\">timestamp</span> <span class=\"k\">with</span> <span class=\"k\">time</span> <span class=\"k\">zone</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span>\n<span class=\"p\">);</span>\n<span class=\"k\">ALTER</span> <span class=\"k\">TABLE</span> <span class=\"ss\">&quot;polls_choice&quot;</span> <span class=\"k\">ADD</span> <span class=\"k\">COLUMN</span> <span class=\"ss\">&quot;question_id&quot;</span> <span class=\"nb\">integer</span> <span class=\"k\">NOT</span> <span class=\"k\">NULL</span><span class=\"p\">;</span>\n<span class=\"k\">ALTER</span> <span class=\"k\">TABLE</span> <span class=\"ss\">&quot;polls_choice&quot;</span> <span class=\"k\">ALTER</span> <span class=\"k\">COLUMN</span> <span class=\"ss\">&quot;question_id&quot;</span> <span class=\"k\">DROP</span> <span class=\"k\">DEFAULT</span><span class=\"p\">;</span>\n<span class=\"k\">CREATE</span> <span class=\"k\">INDEX</span> <span class=\"ss\">&quot;polls_choice_7aa0f6ee&quot;</span> <span class=\"k\">ON</span> <span class=\"ss\">&quot;polls_choice&quot;</span> <span class=\"p\">(</span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"p\">);</span>\n<span class=\"k\">ALTER</span> <span class=\"k\">TABLE</span> <span class=\"ss\">&quot;polls_choice&quot;</span>\n  <span class=\"k\">ADD</span> <span class=\"k\">CONSTRAINT</span> <span class=\"ss\">&quot;polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id&quot;</span>\n    <span class=\"k\">FOREIGN</span> <span class=\"k\">KEY</span> <span class=\"p\">(</span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">REFERENCES</span> <span class=\"ss\">&quot;polls_question&quot;</span> <span class=\"p\">(</span><span class=\"ss\">&quot;id&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">DEFERRABLE</span> <span class=\"k\">INITIALLY</span> <span class=\"k\">DEFERRED</span><span class=\"p\">;</span>\n\n<span class=\"k\">COMMIT</span><span class=\"p\">;</span>\n</code></pre></div>\n<p>Note the following:</p>\n<ul class=\"simple\">\n<li><p>The exact output will vary depending on the database you are using. The\nexample above is generated for PostgreSQL.</p></li>\n<li><p>Table names are automatically generated by combining the name of the app\n(<code class=\"docutils literal notranslate\">polls</code>) and the lowercase name of the model – <code class=\"docutils literal notranslate\">question</code> and\n<code class=\"docutils literal notranslate\">choice</code>. (You can override this behavior.)</p></li>\n<li><p>Primary keys (IDs) are added automatically. (You can override this, too.)</p></li>\n<li><p>By convention, Django appends <code class=\"docutils literal notranslate\">&quot;_id&quot;</code> to the foreign key field name.\n(Yes, you can override this, as well.)</p></li>\n<li><p>The foreign key relationship is made explicit by a <code class=\"docutils literal notranslate\">FOREIGN KEY</code>\nconstraint. Don’t worry about the <code class=\"docutils literal notranslate\">DEFERRABLE</code> parts; that’s just telling\nPostgreSQL to not enforce the foreign key until the end of the transaction.</p></li>\n<li><p>It’s tailored to the database you’re using, so database-specific field types\nsuch as <code class=\"docutils literal notranslate\">auto_increment</code> (MySQL), <code class=\"docutils literal notranslate\">serial</code> (PostgreSQL), or <code class=\"docutils literal notranslate\">integer\nprimary key autoincrement</code> (SQLite) are handled for you automatically. Same\ngoes for the quoting of field names – e.g., using double quotes or\nsingle quotes.</p></li>\n<li><p>The <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-sqlmigrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">sqlmigrate</code></a> command doesn’t actually run the migration on your\ndatabase - it just prints it to the screen so that you can see what SQL\nDjango thinks is required. It’s useful for checking what Django is going to\ndo or if you have database administrators who require SQL scripts for\nchanges.</p></li>\n</ul>\n<p>If you’re interested, you can also run\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-check\"><code class=\"xref std std-djadmin docutils literal notranslate\">python manage.py check</code></a>; this checks for any problems in\nyour project without making migrations or touching the database.</p>\n<p>Now, run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> again to create those model tables in your database:</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 manage.py migrate\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Synchronize unmigrated apps: staticfiles, messages</span>\n<span class=\"go\">  Apply all migrations: admin, contenttypes, polls, auth, sessions</span>\n<span class=\"go\">Synchronizing apps without migrations:</span>\n<span class=\"go\">  Creating tables...</span>\n<span class=\"go\">    Running deferred SQL...</span>\n<span class=\"go\">  Installing custom SQL...</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Applying &lt;migration name&gt;... OK</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> command takes all the migrations that haven’t been\napplied (Django tracks which ones are applied using a special table in your\ndatabase called <code class=\"docutils literal notranslate\">django_migrations</code>) and runs them against your database -\nessentially, synchronizing the changes you made to your models with the schema\nin the database.</p>\n<p>Migrations are very powerful and let you change your models over time, as you\ndevelop your project, without the need to delete your database or tables and\nmake new ones - it specializes in upgrading your database live, without\nlosing data. We’ll cover them in more depth in a later part of the tutorial,\nbut for now, remember the three-step guide to making model changes:</p>\n<ul class=\"simple\">\n<li><p>Change your models (in <code class=\"docutils literal notranslate\">models.py</code>).</p></li>\n<li><p>Run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\">python manage.py makemigrations</code></a> to create\nmigrations for those changes</p></li>\n<li><p>Run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">python manage.py migrate</code></a> to apply those changes to\nthe database.</p></li>\n</ul>\n<p>The reason that there are separate commands to make and apply migrations is\nbecause you’ll commit migrations to your version control system and ship them\nwith your app; they not only make your development easier, they’re also\nuseable by other developers and in production.</p>\n<p>Read the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">django-admin documentation</span></a> for full\ninformation on what the <code class=\"docutils literal notranslate\">manage.py</code> utility can do.</p>\n</section>\n<section id=\"playing-with-the-api\">\n<h2>Playing with the API<a class=\"heading-anchor\" href=\"#playing-with-the-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now, let’s hop into the interactive Python shell and play around with the free\nAPI Django gives you. To invoke the Python shell, use 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 manage.py shell\n</code></pre></div>\n<p>We’re using this instead of simply typing “python”, because <code class=\"file docutils literal notranslate\">manage.py</code>\nsets the <code class=\"docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code> environment variable, which gives Django\nthe Python import path to your <code class=\"file docutils literal notranslate\">mysite/settings.py</code> file.</p>\n<aside class=\"admonition-bypassing-manage-py admonition\" role=\"note\">\n<p class=\"admonition-title\">Bypassing manage.py</p>\n<p>If you’d rather not use <code class=\"file docutils literal notranslate\">manage.py</code>, no problem. Just set the\n<span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/en/1.8/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a> environment variable to\n<code class=\"docutils literal notranslate\">mysite.settings</code>, start a plain Python shell, and set up Django:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span> <span class=\"nn\">django</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>django<span class=\"o\">.</span>setup<span class=\"p\">()</span>\n</code></pre></div>\n<p>If this raises an <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#AttributeError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">AttributeError</code></a>, you’re probably using\na version of Django that doesn’t match this tutorial version. You’ll want\nto either switch to the older tutorial or the newer Django version.</p>\n<p>You must run <code class=\"docutils literal notranslate\">python</code> from the same directory <code class=\"file docutils literal notranslate\">manage.py</code> is in,\nor ensure that directory is on the Python path, so that <code class=\"docutils literal notranslate\">import mysite</code>\nworks.</p>\n<p>For more information on all of this, see the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">django-admin\ndocumentation</span></a>.</p>\n</aside>\n<p>Once you’re in the shell, explore the <a class=\"reference internal\" href=\"/en/1.8/topics/db/queries/\"><span class=\"doc\">database API</span></a>:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">polls.models</span> <span class=\"kn\">import</span> Question<span class=\"p\">,</span> Choice   <span class=\"c1\"># Import the model classes we just wrote.</span>\n\n<span class=\"go\"># No questions are in the system yet.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">[]</span>\n\n<span class=\"go\"># Create a new Question.</span>\n<span class=\"go\"># Support for time zones is enabled in the default settings file, so</span>\n<span class=\"go\"># Django expects a datetime with tzinfo for pub_date. Use timezone.now()</span>\n<span class=\"go\"># instead of datetime.datetime.now() and it will do the right thing.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.utils</span> <span class=\"kn\">import</span> timezone\n<span class=\"gp\">&gt;&gt;&gt; </span>q <span class=\"o\">=</span> Question<span class=\"p\">(</span>question_text<span class=\"o\">=</span><span class=\"s2\">&quot;What&#39;s new?&quot;</span><span class=\"p\">,</span> pub_date<span class=\"o\">=</span>timezone<span class=\"o\">.</span>now<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>q<span class=\"o\">.</span>save<span class=\"p\">()</span>\n\n<span class=\"go\"># Now it has an ID. Note that this might say &quot;1L&quot; instead of &quot;1&quot;, depending</span>\n<span class=\"go\"># on which database you&#39;re using. That&#39;s no biggie; it just means your</span>\n<span class=\"go\"># database backend prefers to return integers as Python long integer</span>\n<span class=\"go\"># objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>id\n<span class=\"go\">1</span>\n\n<span class=\"go\"># Access model field values via Python attributes.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>question_text\n<span class=\"go\">&quot;What&#39;s new?&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>pub_date\n<span class=\"go\">datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=&lt;UTC&gt;)</span>\n\n<span class=\"go\"># Change values by changing the attributes, then calling save().</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>question_text <span class=\"o\">=</span> <span class=\"s2\">&quot;What&#39;s up?&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>save<span class=\"p\">()</span>\n\n<span class=\"go\"># objects.all() displays all the questions in the database.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">[&lt;Question: Question object&gt;]</span>\n</code></pre></div>\n<p>Wait a minute. <code class=\"docutils literal notranslate\">&lt;Question: Question object&gt;</code> is, utterly, an unhelpful representation\nof this object. Let’s fix that by editing the <code class=\"docutils literal notranslate\">Question</code> model (in the\n<code class=\"docutils literal notranslate\">polls/models.py</code> file) and adding a\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\">__str__()</code></a> method to both <code class=\"docutils literal notranslate\">Question</code> and\n<code class=\"docutils literal notranslate\">Choice</code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.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/models.py\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Question</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>              <span class=\"c1\"># __unicode__ on Python 2</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>question_text\n\n<span class=\"k\">class</span> <span class=\"nc\">Choice</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>              <span class=\"c1\"># __unicode__ on Python 2</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>choice_text\n</code></pre></figure>\n<p>It’s important to add <a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\">__str__()</code></a> methods to your\nmodels, not only for your own convenience when dealing with the interactive\nprompt, but also because objects’ representations are used throughout Django’s\nautomatically-generated admin.</p>\n<aside class=\"admonition-str-or-unicode admonition\" role=\"note\">\n<p class=\"admonition-title\"><code class=\"docutils literal notranslate\">__str__</code> or <code class=\"docutils literal notranslate\">__unicode__</code>?</p>\n<p>On Python 3, it’s easy, just use\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\">__str__()</code></a>.</p>\n<p>On Python 2, you should define <a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__unicode__\" title=\"django.db.models.Model.__unicode__\"><code class=\"xref py py-meth docutils literal notranslate\">__unicode__()</code></a>\nmethods returning <code class=\"docutils literal notranslate\">unicode</code> values instead. Django models have a default\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\">__str__()</code></a> method that calls\n<a class=\"reference internal\" href=\"/en/1.8/ref/models/instances/#django.db.models.Model.__unicode__\" title=\"django.db.models.Model.__unicode__\"><code class=\"xref py py-meth docutils literal notranslate\">__unicode__()</code></a> and converts the result to a\nUTF-8 bytestring. This means that <code class=\"docutils literal notranslate\">unicode(p)</code> will return a Unicode\nstring, and <code class=\"docutils literal notranslate\">str(p)</code> will return a bytestring, with characters encoded\nas UTF-8. Python does the opposite: <code class=\"docutils literal notranslate\">object</code> has a <code class=\"docutils literal notranslate\">__unicode__</code>\nmethod that calls <code class=\"docutils literal notranslate\">__str__</code> and interprets the result as an ASCII\nbytestring. This difference can create confusion.</p>\n<p>If all of this is gibberish to you, just use Python 3.</p>\n</aside>\n<p>Note these are normal Python methods. Let’s add a custom method, just for\ndemonstration:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.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/models.py\"><code><span class=\"kn\">import</span> <span class=\"nn\">datetime</span>\n\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.utils</span> <span class=\"kn\">import</span> timezone\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Question</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">def</span> <span class=\"nf\">was_published_recently</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>pub_date <span class=\"o\">&gt;=</span> timezone<span class=\"o\">.</span>now<span class=\"p\">()</span> <span class=\"o\">-</span> datetime<span class=\"o\">.</span>timedelta<span class=\"p\">(</span>days<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Note the addition of <code class=\"docutils literal notranslate\">import datetime</code> and <code class=\"docutils literal notranslate\">from django.utils import\ntimezone</code>, to reference Python’s standard <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#module-datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">datetime</code></a> module and Django’s\ntime-zone-related utilities in <a class=\"reference internal\" href=\"/en/1.8/ref/utils/#module-django.utils.timezone\" title=\"django.utils.timezone: Timezone support.\"><code class=\"xref py py-mod docutils literal notranslate\">django.utils.timezone</code></a>, respectively. If\nyou aren’t familiar with time zone handling in Python, you can learn more in\nthe <a class=\"reference internal\" href=\"/en/1.8/topics/i18n/timezones/\"><span class=\"doc\">time zone support docs</span></a>.</p>\n<p>Save these changes and start a new Python interactive shell by running\n<code class=\"docutils literal notranslate\">python manage.py shell</code> again:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">polls.models</span> <span class=\"kn\">import</span> Question<span class=\"p\">,</span> Choice\n\n<span class=\"go\"># Make sure our __str__() addition worked.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">[&lt;Question: What&#39;s up?&gt;]</span>\n\n<span class=\"go\"># Django provides a rich database lookup API that&#39;s entirely driven by</span>\n<span class=\"go\"># keyword arguments.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<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;Question: What&#39;s up?&gt;]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>question_text__startswith<span class=\"o\">=</span><span class=\"s1\">&#39;What&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[&lt;Question: What&#39;s up?&gt;]</span>\n\n<span class=\"go\"># Get the question that was published this year.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.utils</span> <span class=\"kn\">import</span> timezone\n<span class=\"gp\">&gt;&gt;&gt; </span>current_year <span class=\"o\">=</span> timezone<span class=\"o\">.</span>now<span class=\"p\">()</span><span class=\"o\">.</span>year\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pub_date__year<span class=\"o\">=</span>current_year<span class=\"p\">)</span>\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># Request an ID that doesn&#39;t exist, this will raise an exception.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<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>: Question matching query does not exist.\n\n<span class=\"x\"># Lookup by a primary key is the most common case, so Django provides a</span>\n<span class=\"x\"># shortcut for primary-key exact lookups.</span>\n<span class=\"x\"># The following is identical to Question.objects.get(id=1).</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># Make sure our custom method worked.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q <span class=\"o\">=</span> Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>was_published_recently<span class=\"p\">()</span>\n<span class=\"go\">True</span>\n\n<span class=\"go\"># Give the Question a couple of Choices. The create call constructs a new</span>\n<span class=\"go\"># Choice object, does the INSERT statement, adds the choice to the set</span>\n<span class=\"go\"># of available choices and returns the new Choice object. Django creates</span>\n<span class=\"go\"># a set to hold the &quot;other side&quot; of a ForeignKey relation</span>\n<span class=\"go\"># (e.g. a question&#39;s choice) which can be accessed via the API.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q <span class=\"o\">=</span> Question<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Display any choices from the related object set -- none so far.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">[]</span>\n\n<span class=\"go\"># Create three choices.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>create<span class=\"p\">(</span>choice_text<span class=\"o\">=</span><span class=\"s1\">&#39;Not much&#39;</span><span class=\"p\">,</span> votes<span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Choice: Not much&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>create<span class=\"p\">(</span>choice_text<span class=\"o\">=</span><span class=\"s1\">&#39;The sky&#39;</span><span class=\"p\">,</span> votes<span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Choice: The sky&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>create<span class=\"p\">(</span>choice_text<span class=\"o\">=</span><span class=\"s1\">&#39;Just hacking again&#39;</span><span class=\"p\">,</span> votes<span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Choice objects have API access to their related Question objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>question\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># And vice versa: Question objects get access to Choice objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">[&lt;Choice: Not much&gt;, &lt;Choice: The sky&gt;, &lt;Choice: Just hacking again&gt;]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>count<span class=\"p\">()</span>\n<span class=\"go\">3</span>\n\n<span class=\"go\"># The API automatically follows relationships as far as you need.</span>\n<span class=\"go\"># Use double underscores to separate relationships.</span>\n<span class=\"go\"># This works as many levels deep as you want; there&#39;s no limit.</span>\n<span class=\"go\"># Find all Choices for any question whose pub_date is in this year</span>\n<span class=\"go\"># (reusing the &#39;current_year&#39; variable we created above).</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Choice<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>question__pub_date__year<span class=\"o\">=</span>current_year<span class=\"p\">)</span>\n<span class=\"go\">[&lt;Choice: Not much&gt;, &lt;Choice: The sky&gt;, &lt;Choice: Just hacking again&gt;]</span>\n\n<span class=\"go\"># Let&#39;s delete one of the choices. Use delete() for that.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> q<span class=\"o\">.</span>choice_set<span class=\"o\">.</span>filter<span class=\"p\">(</span>choice_text__startswith<span class=\"o\">=</span><span class=\"s1\">&#39;Just hacking&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>delete<span class=\"p\">()</span>\n</code></pre></div>\n<p>For more information on model relations, see <a class=\"reference internal\" href=\"/en/1.8/ref/models/relations/\"><span class=\"doc\">Accessing related objects</span></a>. For more on how to use double underscores to perform\nfield lookups via the API, see <a class=\"reference internal\" href=\"/en/1.8/topics/db/queries/#field-lookups-intro\"><span class=\"std std-ref\">Field lookups</span></a>. For\nfull details on the database API, see our <a class=\"reference internal\" href=\"/en/1.8/topics/db/queries/\"><span class=\"doc\">Database API reference</span></a>.</p>\n<p>When you’re comfortable with the API, read <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial02/\"><span class=\"doc\">part 2 of this tutorial</span></a> to get Django’s automatic admin working.</p>\n</section>","rootId":"writing-your-first-django-app-part-1","toc":[{"title":"Creating a project","anchor":"creating-a-project","children":[{"title":"Database setup","anchor":"database-setup","children":[]},{"title":"The development server","anchor":"the-development-server","children":[]}]},{"title":"Creating models","anchor":"creating-models","children":[]},{"title":"Activating models","anchor":"activating-models","children":[]},{"title":"Playing with the API","anchor":"playing-with-the-api","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/1.8/intro/"}],"prev":{"docname":"intro/install","title":"Quick install guide","url":"/en/1.8/intro/install/"},"next":{"docname":"intro/tutorial02","title":"Writing your first Django app, part 2","url":"/en/1.8/intro/tutorial02/"},"formats":{"html":"/en/1.8/intro/tutorial01/","markdown":"/en/1.8/intro/tutorial01.md","json":"/en/1.8/intro/tutorial01.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/intro/tutorial01.txt","official":"https://docs.djangoproject.com/en/1.8/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"]}