Management file and urls file in Django

Keywords: Programming Django Python shell Database

1. django-admin command

  • Enter django-admin at the command prompt to view subcommands

[django]
 check
 compilemessages
  createcachetable
 dbshell
 diffsettings
 dumpdata
 flush
 inspectdb
 loaddata
 makemessages
 makemigrations
 migrate
 runserver
 sendtestemail
 shell
 showmigrations
 sqlflush
 sqlmigrate
 sqlsequencereset
 squashmigrations
 startapp
 startproject   # Create project
 test
 testserver

2. Management.py file

  • Run the manger.py file at the command prompt to view subcommands (enter the project directory, run python) manage.py)

[auth]
 changepassword   # Change Password
 createsuperuser   # Creating Super Users

[contenttypes]
 remove_stale_contenttypes

[django]
 check
 compilemessages
 createcachetable
 dbshell   # Enter the interaction mode of Django data shell
 diffsettings
 dumpdata
 flush
 inspectdb   # Export data tables directly to Models
 loaddata
 makemessages
 makemigrations   # Create a log file for the database and record changes to Models
 migrate   # Synchronize database log files to the database
 sendtestemail
 shell
 showmigrations
 sqlflush
 sqlmigrate
 sqlsequencereset
 squashmigrations
 startapp   # Create application
 startproject
 test
 testserver

[sessions]
 clearsessions

[staticfiles]
 collectstatic
 findstatic
 runserver   # Startup service

python manage.py migrate (subcommand)
python manage.py Create superuser (subcommand)
(my_env) F:\web\pythonweb>python manage.py createsuperuser
leave blank to use'administrator': liang277*
E-mail address: liang277*@163.com
  Password:
  Password (again):
  Superuser created successfully.
_Input in browser: 127.0.0.1:8000/admin

Login interface

Post-login interface

2. Use of urls.py file

  • The function of urls.py file
    • urls.py defaults to include all address mappings in the main folder
    • Every user's request matches the address in urls.py
    • After the match is successful, the corresponding handler (view view view) is executed.
  • Test urls.py file
    • Create views.py file in the project home folder to include all defined views (handlers)
"""views.py"""
from django.http import HttpResponse

# View, process user requests and give responses
def print_views(request):  # Request: Represents the user's request information
    return HttpResponse("hello world")  # HttpResponse responds to client content

def index_view(request):
    return HttpResponse("home page")


def show_view(request, str):
    return HttpResponse(str)
"""urls.py"""
from django.contrib import admin
from django.urls import path

from .views import *   # Import the views module in the current directory

urlpatterns = [
    path('admin/', admin.site.urls),
    path('print/', print_view),  # 127.0.0.1:8000/print calls print_view function in views file
    path('', index_view),    # 127.0.0.0:8000 calls the index_view function in the views file to show the home page.
    path('show/', show_view, {'str':'20180426'})  # When passing parameters to show_view function, note that the keys of the dictionary should be the same as the formal parameters of the view function.
]

3. The difference between path function and url function

  • path function (default function in urls file in Django version 2.2)
    • path(route, view, kwargs=None, name=None)
    • route: url routing of requests
    • View: View functions processed by path
    • kwargs: dictionary, used to pass arguments to views functions
    • Name: string, giving path () an individual name, used mainly in the template with {% path%}
  • Pass parameters to view through path function
    • Through kwargs, note that the keys of the dictionary should be consistent with the formal parameters of the view function.
from django.contrib import admin
from django.urls import path   

from .views import *

urlpatterns = [
    path('admin/', admin.site.urls),
    path('print/', print_view), 
    path('', index_view),   
    path('show/', show_view, {'str':'20180426'})
]
  • url function (default function in urls file in Django version 1.11)
    • url(regex, view, kwargs=None, name=None)
    • regex: url routing using regular expressions to match requests
    • views: View functions processed by urlh
    • kwargs: dictionary, used to pass arguments to views functions
    • Name: string, giving path () an individual name, used mainly in the template with {% path%}
  • Pass parameters to view through url function
    • Use subgroup parameterization of regular expressions and parentheses (path function is not supported)
    • Through kwargs, note that the keys of the dictionary should be consistent with the formal parameters of the view function.
from django.contrib import admin
from django.urls import path   # Import path function
from django.conf.urls import url  # Import url function

from .views import *

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^print/$', print_view),  # 127.0.0.1:8000/print calls print_view function in views file
    url(r'^$', index_view),   # 127.0.0.0:8000 calls the index_view function in the views file to show the home page.
    url(r'show/(\d+)', show_view)   # The parameters can be any number, and when the view is received, it is received in a string format.
]

Posted by miro on Fri, 26 Apr 2019 20:48:36 -0700