Django's simple command and implementation of the first page

Keywords: Python Django Database shell

1, Create a python virtual environment and install django

2, Create a django project

Command: Django admin startproject project name

eg:

G:\django_learn>workon testvir
(testvir) G:\django_learn>django-admin startproject django_learn

After execution, a django project will be created successfully and the directory will be generated

--- django_learn

        ---settings.py
        ---url.py
        ---wsgi.py

--- manage.py
  1. settinngs.py
    Contains the default settings for the project, including database information, debug flags, and other working variables
  2. url.py
    Responsible for mapping URL patterns to applications
  3. wsgi.py
    Be responsible for the launch of the project
  4. manage.py
    Tools in Django project, through which you can call django shell, database, etc.

3, Create an app

Command: python manage.py startapp application name

eg:

(testvir) G:\django_learn\django_learn>python manage.py startapp blog

(testvir) G:\django_learn\django_learn>dir

Successfully created an app named blog containing

admin.py
apps.py
migrations
models.py
tests.py
views.py
__init__.py
  1. views.py
    View operation file

4, Making the first page with django

  1. Configure settings.py file to import app

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog'  # Import blog into settings.py
    ]
  2. Configure url.py file, set route

    from django.conf.urls import url
    from django.contrib import admin
    from blog import views
    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^time_show/', views.time_show),
    ]
  3. Configure views.py, set views

    import time
    from django.shortcuts import HttpResponse
    # Create your views here.
    def time_show(request):
    return HttpResponse(time.asctime())

5, Launch django project
Command: python manage.py runserver
Default port 8000
eg:

(testvir) G:\django_learn\django_learn>python manage.py runserver
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.
May 03, 2018 - 22:54:14
Django version 1.9.8, using settings 'django_learn.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Enter http://127.0.0.1:8000/time in the browser/
As shown in the figure below

Posted by dannylewin on Mon, 23 Mar 2020 08:51:47 -0700