添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Toggle theme (current theme: auto)
Toggle theme (current theme: light)
Toggle theme (current theme: dark)
Toggle Light / Dark / Auto color theme

Until September 10, get PyCharm for 30% off!

Become a Sponsor

Writing your first Django app, part 1

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 6.1, which supports Python 3.12 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.

    Where to get help:

    If you’re having trouble going through this tutorial, please head over to the Getting Help section of the FAQ.

    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 project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

    From the command line, cd into a directory where you’d like to store your code and run the following command to bootstrap a new Django project:

    $ django-admin startproject mysite djangotutorial
    

    This will create a directory djangotutorial with a project called mysite inside. The directory name doesn’t matter to Django; you can rename it to anything you like. If it didn’t work, see Problems running django-admin.

    You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itself) or test (which conflicts with a built-in Python package).

    Let’s look at what startproject created:

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

    These files are:

  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.

  • mysite/: A directory that 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. mysite.urls).

  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.

  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.

  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

  • The development server

    Let’s verify your Django project works. Change into the djangotutorial directory, if you haven’t already, and run the following commands:

    $ python manage.py runserver
    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.
    September 08, 2026 - 15:50:53
    Django version 6.1, using settings 'mysite.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.
    WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead.
    For more information on production servers see: https://docs.djangoproject.com/en/6.1/howto/deployment/

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

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

    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.)

    (To serve the site on a different port, see the runserver reference.)

    Automatic reloading of 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.

    Creating the Polls app

    Now that your environment – a “project” – is set up, you’re set to start doing work.

    Each application you write in Django consists of a Python package that follows a certain convention. Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.

    Projects vs. apps

    What’s the difference between a project and an app? An app is a web application that does something – e.g., a blog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

    Your apps can live anywhere in your Python path. In this tutorial, we’ll create our poll app inside the djangotutorial folder.

    To create your app, make sure you’re in the same directory as manage.py and type this command:

    $ python manage.py startapp polls
    

    Write your first view

    Let’s write the first view. Open the file polls/views.py and put the following Python code in it:

    polls/views.py
    from django.http import HttpResponse
    def index(request):
        return HttpResponse("Hello, world. You're at the polls index.")
    

    This is the most basic view possible in Django. To access it in a browser, we need to map it to a URL - and for this we need to define a URL configuration, or “URLconf” for short. These URL configurations are defined inside each Django app, and they are Python files named urls.py.

    To define a URLconf for the polls app, create a file polls/urls.py with the following content:

    polls/urls.py
    from django.urls import path
    from . import views
    urlpatterns = [
        path("", views.index, name="index"),
    

    Your app directory should now look like:

    polls/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
        urls.py
        views.py
    

    The next step is to configure the root URLconf in the mysite project to include the URLconf defined in polls.urls. To do this, add an import for django.urls.include in mysite/urls.py and insert an include() in the urlpatterns list, so you have:

    mysite/urls.py
    from django.contrib import admin
    from django.urls import include, path
    urlpatterns = [
        path("polls/", include("polls.urls")),
        path("admin/", admin.site.urls),
    

    The path() function expects at least two arguments: route and view. The include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

    The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.

    When to use include()

    You should always use include() when you include other URL patterns. The only exception is admin.site.urls, which is a pre-built URLconf provided by Django for the default admin site.

    You have now wired an index view into the URLconf. Verify it’s working with the following command:

    $ python manage.py runserver
    

    Go to http://localhost:8000/polls/ in your browser, and you should see the text “Hello, world. You’re at the polls index.”, which you defined in the index view.

    Page not found?

    If you get an error page here, check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/.

    When you’re comfortable with the basic request and response flow, read part 2 of this tutorial to start working with the database.

    Try the FAQ — it's got answers to many common questions.
    Index, Module Index, or Table of Contents
    Handy when looking for specific information.
    Django Discord Server
    Join the Django Discord Community.
    Official Django Forum
    Join the community on the Django Forum.
    Ticket tracker
    Report bugs with Django or Django documentation in our ticket tracker.
  • JetBrains
  • JetBrains delivers intelligent software solutions that make developers more productive by simplifying their challenging tasks, automating the routine, and helping them adopt the best development practices. PyCharm is the Python IDE for Professional Developers by JetBrains providing a complete set of tools for productive Python, Web and scientific development.
  • Kraken Tech
  • Kraken is the most-loved operating system for energy. Powered by our Utility-Grade AI™ and deep industry know-how, we help utilities transform their technology and operations so they can lead the energy transition. Delivering better outcomes from generation through distribution to supply, Kraken powers 70+ million accounts worldwide, and is on a mission to make a big, green dent in the universe.
  • PostHog
  • PostHog is an all-in-one developer platform for building successful products. We provide product analytics, web analytics, session replay, error tracking, feature flags, experiments, surveys, LLM observability, data warehouse, CDP, workflows, logs, and an AI product assistant to help debug your code, ship features faster, and keep all your usage and customer data in one stack.