Django to create your first project

Getting Started with Django Demos

foreword

This document aims to organize Django's learning route from entry to entry, so as to provide others with learning.
This document is written with reference to the official technical documentation of Django. The Django version should be version 4.2.

The first step is to install Django

pip install django

Step 2 Write your first Django application

Let's learn by example.

In this tutorial, we'll walk you through the creation of a basic polling application.

It will consist of two parts:

  • A public site that allows people to view and vote in polls.
  • An admin site that allows you to add, change and delete polls.
django-admin startproject mysite

This will create a mysite directory in your current directory.

Let's see what startproject creates:

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

These files are:

  • The external mysite/root directory is the container for your project. Its name doesn't matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. manage.py You can read all the details about it in django-admin and manage.py.
  • Inside the mysite/ directory are the actual Python packages for the project. Its name is the Python package name you'll need to import anything in it (eg mysite.urls).
  • mysite/ init.py : An empty file that tells Python that this directory should be considered a Python package.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how the settings work.
  • mysite/urls.py: The URL declaration for this Django project; the "table of contents" for your Django site.
  • mysite/asgi.py: The entry point for an ASGI-compliant web server serving your project.
  • mysite/wsgi.py: The entry point for a WSGI-compliant web server serving your project.

Let's verify that your Django project is working. Change to the external mysite directory (if you haven't already), and run the following command:

python manage.py runserver

Then you can see output like this in your console.

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 03, 2023 - 05:16:39
Django version 4.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now that your environment -- a "project" -- is set up, you can start working.

Every application you write in Django includes a Python package that follows certain conventions. Django comes with a utility that automatically generates your application's basic directory structure, so you can focus on writing code instead of creating directories.

项目与应用
项目和应用程序之间有什么区别?应用程序是一种可以执行某些操作的网络应用程序——例如,博客系统、公共记录数据库或小型投票应用程序。项目是特定网站的配置和应用程序的集合。一个项目可以包含多个应用程序。一个应用程序可以在多个项目中。
您的应用程序可以位于您的Python 路径上的任何位置。在本教程中,我们将在与您的文件相同的目录中创建我们的投票应用程序 manage.py,以便它可以作为自己的顶级模块导入,而不是mysite.

To create your application, make sure you are in the same directory as manage.py and type the following command:

python manage.py startapp polls

Then let's write a simple view, first we switch to views.py in our polls folder.

Then write a simple piece of code

from django.http import HttpResponse


# Create your views here.

def index(request):
    return HttpResponse("Hello! This is a Django Project")

In this way, we have written a simple view view, but we need to add routing, so that we can access requests to him from our ip URL.

Then we need to create a urls.py file as the routing management of our polls application.

from django.urls import path
from . import views


urlpatterns = [
    path("", views.index, name="index"),
]

After adding, we need to add our application polls in the route of the whole project. So we need to write the code in urls.py under the mysite folder.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("polls/", include("polls.urls")),
    path('admin/', admin.site.urls),
]

Finally, we re-run the project and visit our http://127.0.0.1:8080/polls to see the effect.

Guess you like

Origin blog.csdn.net/weixin_50153843/article/details/130472405