Django instance - static access

Keywords: Django JQuery Python

Project framework

Mydjango
    blog
        migrations
        static
            jquery-3.3.1.slim.min.js
        templates
            index.html
            login.html
        admin.py
        apps.py
        models.py
        tests.py
        urls.py
        views.py
    Mydjango
        settings.py
        urls.py
        wsgi.py
    
    dbsqlite3
    manage.py

Create project

Create projects and project applications

django-admin startproject Mydjango
cd Mydjango
python manage.py startapp blog

Create template directory and template file of HTML blog / templates > index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello django</title>
    <script src="/static/jquery-3.3.1.slim.min.js"></script>
    <!--
    or
    <script src="{ % static 'jquery-3.3.1.slim.min.js' %}"></scriptsrc>
    -->
</head>
<body>
    <h1>present time {{ t }}</h1>       <!-- use{{Variable name}}Quote views Variables in functions are displayed on the front page-->
<script>
    $("h1").css("color","green")
</script>
</body>
</html>

Create template directory and template file of HTML blog / templates > login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login system</title>
</head>
<body>
<h1>Login system</h1>
</hr>

<!-- POST Method submit data to specified url If homology can be ignored IP address-->
<!--The path is filled in by alias, which is convenient for later maintenance and change-->
<form action="http://127.0.0.1:8000{% url 'reg' %}" method="post">
    <p>User name<input type="text" name="user"></p>
    <p>dense &nbsp code<input type="password" name="passwd"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

Mydjango/blog/static/apps.py

from django.apps import AppConfig


class BlogConfig(AppConfig):
    name = 'blog'

Mydjango/Mydjango/settings.py

==>Note: secret key is the unique identification code, which cannot be changed or copied for other purposes. Each item has its own secret key code==

# -*- coding:utf8 -*-
import os

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # New project application
    'blog',
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    #Temporarily cancel view Middleware
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        # html file storage path
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
# Access alias, js jquery css and other files when they are imported
STATIC_URL = '/static/'
# js jquery css and other file storage paths
STATICFILES_DIRS=(
    os.path.join(BASE_DIR,"blog/static"),
    )

MyDjango/blog/views.py

# -*- coding:utf8 -*-
from django.shortcuts import render,HttpResponse
import time
# Create your views here.

def Show_time(requset):                                     #Page presentation and CSS testing
    t = time.ctime()
    return render(requset,"index.html",locals())            #Return to the specified page content

def Article_txt(requset,num):                               #Unnamed group regular test
    # The second parameter is what regular expressions group
    # If two or more packets are transmitted, the corresponding number of parameters is required
    # Unnamed group parameter name can be customized
    return HttpResponse(num)

def Title_txt(requset,id):                                  #Named group regular test
    # The second parameter is what regular expressions group
    # If two or more packets are transmitted, the corresponding number of parameters is required
    # Named group parameter must be consistent with urls.py group name
    return HttpResponse(id)

def User_login(requset):                                    #Log in to submit numerical test
    if requset.method == "POST":                            #Judge view function submission type
        print(requset.POST.get("user"))
        print(requset.POST.get("passwd"))
        return HttpResponse("Post login page display test!")         #Return to specified content
    else:
        return render(requset,"login.html",locals())        #Return to the specified page content

Mydjango/Mydjango/urls.py global primary URLs

# -*- coding:utf8 -*-
from django.contrib import admin
from django.urls import path,re_path            # The introduction of urls path module and re path module
from django.urls import include                 # Introducing url distribution include module
from blog import views                          # Introduce project application function module

urlpatterns = [
    path('admin/', admin.site.urls),                        #System default
    path('blog/', views.Show_time),                         #Normal URL application
    path('login/', views.User_login,name="reg"),            #Normal URL app, enable alias
    re_path(r'^article',include('blog.urls')),              #Regular matching is found in the specified include file
]

Mydjango/Mydjango/blog/urls.py project application sub URLs

# -*- coding:utf8 -*-

from django.contrib import admin
from django.urls import path,re_path
# The introduction of urls path module and re path module

from blog import views
# Introduce project application function module

urlpatterns = [
    re_path(r'(\d{4})',views.Article_txt),          #url application with regular expression [unnamed group]
    re_path(r'(?P<id>[a-z]{4})',views.Title_txt),   #url application with regular expression [named group]
    #Regular expressions are grouped, and the grouping content is the parameter of post function
    #If there are two or more groups, they are also enclosed in brackets and passed to the post function as parameters
    # Formal declaration group name of? < name >
]

Project testing

Startup project

python manage.py runserver

Access test (path common)

http://127.0.0.1:8000/blog/

Access test (regular use of re? Path)

http://127.0.0.1:8000/article/1234
# Unnamed group
http://127.0.0.1:8000/article/abcd
# Named grouping

Simulated landing test

http://127.0.0.1:8000/login/

Posted by jbulaswad on Thu, 02 Jan 2020 10:39:33 -0800