# Writing your first Django app, part 2

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 lets people view polls and vote in them.
An admin site that lets you add, change, and delete polls.
We’ll assume you have Django installed already. You can tell Django is installed and which version by running the following command in a shell prompt (indicated by the $ prefix):

/ 
$ python -m django --version
If Django is installed, you should see the version of your installation. If it isn’t, you’ll get an error telling “No module named django”.

This tutorial is written for Django 4.1, which supports Python 3.8 and later. If the Django version doesn’t match, you can refer to the tutorial for your version of Django by using the version switcher at the bottom right corner of this page, or update Django to the newest version. If you’re using an older version of Python, check What Python version can I use with Django? to find a compatible version of Django.

See How to install Django for advice on how to remove older versions of Django and install a newer one.

# Database setup

Now, open up `<span class="pre">mysite/settings.py</span>`. It’s a normal Python module with module-level variables representing Django settings.

By default, the configuration uses SQLite. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database. When starting your first real project, however, you may want to use a more scalable database like PostgreSQL, to avoid database-switching headaches down the road.

If you wish to use another database, install the appropriate [<span class="std std-ref">database bindings</span>](https://docs.djangoproject.com/en/4.1/topics/install/#database-installation) and change the following keys in the [`<span class="pre">DATABASES</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-DATABASES) `<span class="pre">'default'</span>` item to match your database connection settings:

- [`<span class="pre">ENGINE</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-DATABASE-ENGINE) – Either `<span class="pre">'django.db.backends.sqlite3'</span>`, `<span class="pre">'django.db.backends.postgresql'</span>`, `<span class="pre">'django.db.backends.mysql'</span>`, or `<span class="pre">'django.db.backends.oracle'</span>`. Other backends are [<span class="std std-ref">also available</span>](https://docs.djangoproject.com/en/4.1/ref/databases/#third-party-notes).
- [`<span class="pre">NAME</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-NAME) – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, [`<span class="pre">NAME</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-NAME) should be the full absolute path, including filename, of that file. The default value, `<span class="pre">BASE_DIR</span> <span class="pre">/</span> <span class="pre">'db.sqlite3'</span>`, will store the file in your project directory.

If you are not using SQLite as your database, additional settings such as [`<span class="pre">USER</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-USER), [`<span class="pre">PASSWORD</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-PASSWORD), and [`<span class="pre">HOST</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-HOST) must be added. For more details, see the reference documentation for [`<span class="pre">DATABASES</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-DATABASES).

> For databases other than SQLite
> 
> If you’re using a database besides SQLite, make sure you’ve created a database by this point. Do that with “`<span class="pre">CREATE</span> <span class="pre">DATABASE</span> <span class="pre">database_name;</span>`” within your database’s interactive prompt.
> 
> Also make sure that the database user provided in `<span class="pre">mysite/settings.py</span>` has “create database” privileges. This allows automatic creation of a [<span class="std std-ref">test database</span>](https://docs.djangoproject.com/en/4.1/topics/testing/overview/#the-test-database) which will be needed in a later tutorial.
> 
> If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.

While you’re editing mysite/settings.py, set TIME\_ZONE to your time zone.

Also, note the INSTALLED\_APPS setting at the top of the file. That holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.

By default, INSTALLED\_APPS contains the following apps, all of which come with Django:

django.contrib.admin – The admin site. You’ll use it shortly.  
django.contrib.auth – An authentication system.  
django.contrib.contenttypes – A framework for content types.  
django.contrib.sessions – A session framework.  
django.contrib.messages – A messaging framework.  
django.contrib.staticfiles – A framework for managing static files.  
These applications are included by default as a convenience for the common case.

Some of these applications make use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

<div class="console-block" id="bkmrk-%24-python-manage.py-m"><section class="c-content-unix" id="bkmrk-%24-python-manage.py-m-0">```
$ python manage.py migrate

```

The [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) command looks at the [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) setting and creates any necessary database tables according to the database settings in your `<span class="pre">mysite/settings.py</span>` file and the database migrations shipped with the app (we’ll cover those later). You’ll see a message for each migration it applies. If you’re interested, run the command-line client for your database and type `<span class="pre">\dt</span>` (PostgreSQL), `<span class="pre">SHOW</span> <span class="pre">TABLES;</span>` (MariaDB, MySQL), `<span class="pre">.tables</span>` (SQLite), or `<span class="pre">SELECT</span> <span class="pre">TABLE_NAME</span> <span class="pre">FROM</span> <span class="pre">USER_TABLES;</span>` (Oracle) to display the tables Django created.

<span style="color: rgb(224, 62, 45);">For the minimalists</span>

<span style="color: rgb(224, 62, 45);">Like we said above, the default applications are included for the common case, but not everybody needs them. If you don’t need any or all of them, feel free to comment-out or delete the appropriate line(s) from [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) before running [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate). The [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) command will only run migrations for apps in [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS).</span>

</section></div><div class="console-block" id="bkmrk-"><section class="c-content-unix" id="bkmrk--0"><div class="highlight-console notranslate"><div class="highlight"></div></div></section></div>

# Creating models

Now we’ll define your models – essentially, your database layout, with additional metadata.

> Philosophy
> 
> A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Django follows the [<span class="std std-ref">DRY Principle</span>](https://docs.djangoproject.com/en/4.1/misc/design-philosophies/#dry). The goal is to define your data model in one place and automatically derive things from it.
> 
> This includes the migrations - unlike in Ruby On Rails, for example, migrations are entirely derived from your models file, and are essentially a history that Django can roll through to update your database schema to match your current models.

In our poll app, we’ll create two models: `<span class="pre">Question</span>` and `<span class="pre">Choice</span>`. A `<span class="pre">Question</span>` has a question and a publication date. A `<span class="pre">Choice</span>` has two fields: the text of the choice and a vote tally. Each `<span class="pre">Choice</span>` is associated with a `<span class="pre">Question</span>`.

These concepts are represented by Python classes. Edit the `<span class="pre">polls/models.py</span>` file so it looks like this:

<div class="literal-block-wrapper docutils container" id="bkmrk-polls%2Fmodels.py%C2%B6"><div class="literal-block-wrapper docutils container"><div class="code-block-caption"><span class="caption-text">`<span class="pre">polls/models.py</span>`</span>[¶](https://docs.djangoproject.com/en/4.1/intro/tutorial02/#id2 "Permalink to this code")</div><div class="highlight-python notranslate"><div class="highlight"></div></div></div></div>```
from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
```

Here, each model is represented by a class that subclasses [`<span class="pre">django.db.models.Model</span>`](https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model "django.db.models.Model"). Each model has a number of class variables, each of which represents a database field in the model.

Each field is represented by an instance of a [`<span class="pre">Field</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field "django.db.models.Field") class – e.g., [`<span class="pre">CharField</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.CharField "django.db.models.CharField") for character fields and [`<span class="pre">DateTimeField</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.DateTimeField "django.db.models.DateTimeField") for datetimes. This tells Django what type of data each field holds.

The name of each [`<span class="pre">Field</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field "django.db.models.Field") instance (e.g. `<span class="pre">question_text</span>` or `<span class="pre">pub_date</span>`) is the field’s name, in machine-friendly format. You’ll use this value in your Python code, and your database will use it as the column name.

You can use an optional first positional argument to a [`<span class="pre">Field</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field "django.db.models.Field") to designate a human-readable name. That’s used in a couple of introspective parts of Django, and it doubles as documentation. If this field isn’t provided, Django will use the machine-readable name. In this example, we’ve only defined a human-readable name for `<span class="pre">Question.pub_date</span>`. For all other fields in this model, the field’s machine-readable name will suffice as its human-readable name.

Some [`<span class="pre">Field</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field "django.db.models.Field") classes have required arguments. [`<span class="pre">CharField</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.CharField "django.db.models.CharField"), for example, requires that you give it a [`<span class="pre">max_length</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.CharField.max_length "django.db.models.CharField.max_length"). That’s used not only in the database schema, but in validation, as we’ll soon see.

A [`<span class="pre">Field</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field "django.db.models.Field") can also have various optional arguments; in this case, we’ve set the [`<span class="pre">default</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field.default "django.db.models.Field.default") value of `<span class="pre">votes</span>` to 0.

Finally, note a relationship is defined, using [`<span class="pre">ForeignKey</span>`](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey "django.db.models.ForeignKey"). That tells Django each `<span class="pre">Choice</span>` is related to a single `<span class="pre">Question</span>`. Django supports all the common database relationships: many-to-one, many-to-many, and one-to-one.

# Activating models

That small bit of model code gives Django a lot of information. With it, Django is able to:

- Create a database schema (`<span class="pre">CREATE</span> <span class="pre">TABLE</span>` statements) for this app.
- Create a Python database-access API for accessing `<span class="pre">Question</span>` and `<span class="pre">Choice</span>` objects.

But first we need to tell our project that the `<span class="pre">polls</span>` app is installed.

> Philosophy
> 
> Django apps are “pluggable”: You can use an app in multiple projects, and you can distribute apps, because they don’t have to be tied to a given Django installation.

To include the app in our project, we need to add a reference to its configuration class in the [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) setting. The `<span class="pre">PollsConfig</span>` class is in the `<span class="pre">polls/apps.py</span>` file, so its dotted path is `<span class="pre">'polls.apps.PollsConfig'</span>`. Edit the `<span class="pre">mysite/settings.py</span>` file and add that dotted path to the [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) setting. It’ll look like this:

<div class="literal-block-wrapper docutils container" id="bkmrk-mysite%2Fsettings.py%C2%B6"><div class="literal-block-wrapper docutils container"><div class="code-block-caption"><span class="caption-text">`<span class="pre">mysite/settings.py</span>`</span>[¶](https://docs.djangoproject.com/en/4.1/intro/tutorial02/#id3 "Permalink to this code")</div><div class="highlight-python notranslate"><div class="highlight"></div></div></div></div>```
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
```

Now Django knows to include the `<span class="pre">polls</span>` app. Let’s run another command:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag"><label for="c-tab-1-unix" title="Linux/macOS">/</label> <label for="c-tab-1-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-m">```
$ python manage.py makemigrations polls
```

</section></div>You should see something similar to the following:

```
Migrations for 'polls':
  polls/migrations/0001_initial.py
    - Create model Question
    - Create model Choice
```

By running `<span class="pre">makemigrations</span>`, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a *migration*.

Migrations are how Django stores changes to your models (and thus your database schema) - they’re files on disk. You can read the migration for your new model if you like; it’s the file `<span class="pre">polls/migrations/0001_initial.py</span>`. Don’t worry, you’re not expected to read them every time Django makes one, but they’re designed to be human-editable in case you want to manually tweak how Django changes things.

There’s a command that will run the migrations for you and manage your database schema automatically - that’s called [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate), and we’ll come to it in a moment - but first, let’s see what SQL that migration would run. The [`<span class="pre">sqlmigrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-sqlmigrate) command takes migration names and returns their SQL:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag-0"><label for="c-tab-2-unix" title="Linux/macOS">/</label> <label for="c-tab-2-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-s">```
$ python manage.py sqlmigrate polls 0001
```

</section></div>You should see something similar to the following (we’ve reformatted it for readability):

```
BEGIN;
--
-- Create model Question
--
CREATE TABLE "polls_question" (
    "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    "question_text" varchar(200) NOT NULL,
    "pub_date" timestamp with time zone NOT NULL
);
--
-- Create model Choice
--
CREATE TABLE "polls_choice" (
    "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL,
    "question_id" bigint NOT NULL
);
ALTER TABLE "polls_choice"
  ADD CONSTRAINT "polls_choice_question_id_c5b4b260_fk_polls_question_id"
    FOREIGN KEY ("question_id")
    REFERENCES "polls_question" ("id")
    DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "polls_choice_question_id_c5b4b260" ON "polls_choice" ("question_id");

COMMIT;
```

Note the following:

- The exact output will vary depending on the database you are using. The example above is generated for PostgreSQL.
- Table names are automatically generated by combining the name of the app (`<span class="pre">polls</span>`) and the lowercase name of the model – `<span class="pre">question</span>` and `<span class="pre">choice</span>`. (You can override this behavior.)
- Primary keys (IDs) are added automatically. (You can override this, too.)
- By convention, Django appends `<span class="pre">"_id"</span>` to the foreign key field name. (Yes, you can override this, as well.)
- The foreign key relationship is made explicit by a `<span class="pre">FOREIGN</span> <span class="pre">KEY</span>` constraint. Don’t worry about the `<span class="pre">DEFERRABLE</span>` parts; it’s telling PostgreSQL to not enforce the foreign key until the end of the transaction.
- It’s tailored to the database you’re using, so database-specific field types such as `<span class="pre">auto_increment</span>` (MySQL), `<span class="pre">bigint</span> <span class="pre">PRIMARY</span> <span class="pre">KEY</span> <span class="pre">GENERATED</span> <span class="pre">BY</span> <span class="pre">DEFAULT</span> <span class="pre">AS</span> <span class="pre">IDENTITY</span>` (PostgreSQL), or `<span class="pre">integer</span> <span class="pre">primary</span> <span class="pre">key</span> <span class="pre">autoincrement</span>` (SQLite) are handled for you automatically. Same goes for the quoting of field names – e.g., using double quotes or single quotes.
- The [`<span class="pre">sqlmigrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-sqlmigrate) command doesn’t actually run the migration on your database - instead, it prints it to the screen so that you can see what SQL Django thinks is required. It’s useful for checking what Django is going to do or if you have database administrators who require SQL scripts for changes.

If you’re interested, you can also run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">check</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-check); this checks for any problems in your project without making migrations or touching the database.

Now, run [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) again to create those model tables in your database:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag-1"><label for="c-tab-3-unix" title="Linux/macOS">/</label> <label for="c-tab-3-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-m-0">```
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Rendering model states... DONE
  Applying polls.0001_initial... OK
```

</section></div>The [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) command takes all the migrations that haven’t been applied (Django tracks which ones are applied using a special table in your database called `<span class="pre">django_migrations</span>`) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.

Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones - it specializes in upgrading your database live, without losing data. We’ll cover them in more depth in a later part of the tutorial, but for now, remember the three-step guide to making model changes:

- Change your models (in `<span class="pre">models.py</span>`).
- Run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">makemigrations</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-makemigrations) to create migrations for those changes
- Run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) to apply those changes to the database.

The reason that there are separate commands to make and apply migrations is because you’ll commit migrations to your version control system and ship them with your app; they not only make your development easier, they’re also usable by other developers and in production.

Read the [<span class="doc">django-admin documentation</span>](https://docs.djangoproject.com/en/4.1/ref/django-admin/) for full information on what the `<span class="pre">manage.py</span>` utility can do.

# Creating a project

If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django [<span class="xref std std-term">project</span>](https://docs.djangoproject.com/en/4.1/glossary/#term-project) – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

From the command line, `<span class="pre">cd</span>` into a directory where you’d like to store your code, then run the following command:

<div class="console-block" id="bkmrk-%24-django-admin-start"><section class="c-content-unix" id="bkmrk-%24-django-admin-start-0">```
$ django-admin startproject mysite
```

</section></div>This will create a `<span class="pre">mysite</span>` directory in your current directory. If it didn’t work, see [<span class="std std-ref">Problems running django-admin</span>](https://docs.djangoproject.com/en/4.1/faq/troubleshooting/#troubleshooting-django-admin).

> Note
> 
> You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like `<span class="pre">django</span>` (which will conflict with Django itself) or `<span class="pre">test</span>` (which conflicts with a built-in Python package).

> Where should this code live?
> 
> If your background is in plain old PHP (with no use of modern frameworks), you’re probably used to putting code under the web server’s document root (in a place such as `<span class="pre">/var/www</span>`). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your web server’s document root, because it risks the possibility that people may be able to view your code over the web. That’s not good for security.
> 
> Put your code in some directory **outside** of the document root, such as `<span class="pre">/home/mycode</span>`.

Let’s look at what [`<span class="pre">startproject</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-startproject) created:

```
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
```

These files are:

<div class="section" id="bkmrk-the-outer%C2%A0mysite%2F%C2%A0ro">- The outer `<span class="pre">mysite/</span>` root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
- `<span class="pre">manage.py</span>`: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about `<span class="pre">manage.py</span>` in [<span class="doc">django-admin and manage.py</span>](https://docs.djangoproject.com/en/4.1/ref/django-admin/).
- The inner `<span class="pre">mysite/</span>` directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. `<span class="pre">mysite.urls</span>`).
- `<span class="pre">mysite/__init__.py</span>`: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read [<span class="xref std std-ref">more about packages</span>](https://docs.python.org/3/tutorial/modules.html#tut-packages "(in Python v3.11)") in the official Python docs.
- `<span class="pre">mysite/settings.py</span>`: Settings/configuration for this Django project. [<span class="doc">Django settings</span>](https://docs.djangoproject.com/en/4.1/topics/settings/) will tell you all about how settings work.
- `<span class="pre">mysite/urls.py</span>`: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in [<span class="doc">URL dispatcher</span>](https://docs.djangoproject.com/en/4.1/topics/http/urls/).
- `<span class="pre">mysite/asgi.py</span>`: An entry-point for ASGI-compatible web servers to serve your project. See [<span class="doc">How to deploy with ASGI</span>](https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/) for more details.
- `<span class="pre">mysite/wsgi.py</span>`: An entry-point for WSGI-compatible web servers to serve your project. See [<span class="doc">How to deploy with WSGI</span>](https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/) for more details.

</div><div class="section" id="bkmrk-"><div class="section"><span id="bkmrk--0"></span></div></div>

# The development server

Let’s verify your Django project works. Change into the outer `<span class="pre">mysite</span>` directory, if you haven’t already, and run the following commands:

<div class="console-block" id="bkmrk-%24-python-manage.py-r"><section class="c-content-unix" id="bkmrk-%24-python-manage.py-r-0">```
$ python manage.py runserver
```

</section></div>You’ll see the following output on the command line:

```
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

February 16, 2023 - 15:50:53
Django version 4.1, using settings 'mysite.settings'
Starting development server at <a class="reference external" href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a>
Quit the server with CONTROL-C.
```

> Note
> 
> Ignore the warning about unapplied database migrations for now; we’ll deal with the database shortly.

You’ve started the Django development server, a lightweight web server written purely in Python. We’ve included this with Django so you can develop things rapidly, without having to deal with configuring a production server – such as Apache – until you’re ready for production.

Now’s a good time to note: **don’t** use this server in anything resembling a production environment. It’s intended only for use while developing. (We’re in the business of making web frameworks, not web servers.)

Now that the server’s running, visit [http://127.0.0.1:8000/](http://127.0.0.1:8000/) with your web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!

> Changing the port
> 
> By default, the [`<span class="pre">runserver</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-runserver) command starts the development server on the internal IP at port 8000.
> 
> If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
> 
> <div class="console-block" id="bkmrk-%24-python-manage.py-r-1"><label for="c-tab-3-unix" title="Linux/macOS">  
> </label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-r-2">```
> $ python manage.py runserver 8080
> ```
> 
> </section></div>If you want to change the server’s IP, pass it along with the port. For example, to listen on all available public IPs (which is useful if you are running Vagrant or want to show off your work on other computers on the network), use:
> 
> <div class="console-block" id="bkmrk-%24-python-manage.py-r-3"><label for="c-tab-4-unix" title="Linux/macOS">  
> </label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-r-4">```
> $ python manage.py runserver 0.0.0.0:8000
> ```
> 
> </section></div>Full docs for the development server can be found in the [`<span class="pre">runserver</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-runserver) reference.

> Automatic reloading of [`<span class="pre">runserver</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-runserver)
> 
> The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.