Django
Following the framework of the MVC design pattern, MVC is short for the three words Model, View, Controller.Represents models, views, and controllers.
Create Project
- From the command line, first step into a virtual environment where Django is installed
- Through pycharm
#Create Project 1. Command line: django-admin startproject [project name] 2. pycharm mode: file->create project->select django.Then specify the path to the project and the python interpreter. #Run the project python manage.py runserver Port Python management.py runserver 8088 can be added Public Access Python management.py runserver 0.0.0.0 8088
Introduction to project structure
- Manage.py: The interaction with the project is based on this file, python manage.py [subcommand]
- settings.py: The settings for this project, where all project-related configurations are placed
- urls.py: Used to configure URL routing, such as accessing http://127.0.0.1/news/is accessing the news list page, which needs to be done in this file.
- wsgi.py: A web server entry for a project compatible with the WSGI protocol that needs to be used when deploying and, in general, not modified.
Create app
python manage.py startapp django_1
View Functions
- The first argument to the view function must be request, which cannot be fewer
- The return value of the view function must be an object of a subclass of django.http.response.HttpResponseBase.
URL Pass-through Parameters
- url mapping
- Looking for a map in the urls.py file is because ROOT_URLCONF is configured as urls.py in the settings.py file.All django s go to urls.py to find them.
- All of our mappings in urls.py should be placed in the urlpatterns variable.
- All mappings are not written casually, but are wrapped using either the path function or the re_path function.
- url pass parameter
- Use variables in the url: in the first parameter of path, use < parameter name > to pass parameters, and then write a parameter in the view function.The parameter in the view function must be the same as the parameter name in the url, or it cannot be found.
- Take the query string approach: There is no need to match the parts of the query string separately in the url, just use request.GET.get('parameter name') in the view function to get it.The code is as follows:
def author_detail(request): author_id = request.GET['id'] text = 'Author's id Yes:%s' % author_id return HttpResponse(text)
Because the query string uses a GET request, we get the parameters through request.GET, and because GET is a dictionary-like data type, all values are obtained in the same way as a dictionary.
url naming
- Reason for url naming
Because URLs are constantly changing, write-to-death may often modify code.Give the url a name and then use the name of the url to invert it when you use it. -
How to give a url a name
In the path function, you can specify by passing a name parameter.# urls.py from django.urls import path from . import views urlpatterns = [ path('',views.index,name='index'), path('signin/',views.login,name='login') ] # views.py from django.http import HttpResponse from django.shortcuts import redirect,reverse def index(request): username = request.GET.get('username') if username: return HttpResponse('home page') else: return redirect(reverse('login'))
-
Apply Namespace
It is possible to have a url with the same name between multiple apps.To avoid confusion when inverting urls, you can use namespaces to distinguish them.Defining an application namespace is straightforward, as long as you define a variable called app_name in the urls.py of the app to specify the namespace of the application.# urls.py from django.urls import path from . import views app_name = 'front' urlpatterns = [ path('',views.index,name='index'), path('signin/',views.login,name='login') ] # views.py from django.http import HttpResponse from django.shortcuts import redirect,reverse def index(request): username = request.GET.get('username') if username: return HttpResponse('home page') else: return redirect(reverse('front:login'))
- Apply namespace and instance namespace
# An app can create multiple instances and use multiple URLs to map the same app.Confusion arises when using an application namespace when doing the reverse, so to avoid this problem you can use the instance namespace.Pass a namespace variable in the include function. # urls.py from django.urls import path,include urlpattterns = [ path('',include('front.urls')), path('cms1/',include('cms.urls',namespace='cms1')), path('cms2/',include('cms.urls',namespace='cms2')) ]
url hierarchical modularization
After multiple apps, urlpatterns in the urls.py of the main app write too many paths, which can be easily forwarded by creating their own app's corresponding urls.py in the app.
# Primary urls.py, using the include function to include child urls.py from django.urls import path,include urlpattterns = [ path('book/',include('book.urls')) # URLs starting with book go to url s.py under book app ] # urls.py of book app from django.urls import path from . import views urlpattterns = [ path('',views.book), path('detail/<book_id>',views.book_detail), path('list/',views.book_list), ]
reverse function supplement
1. If you need to add a parameter when inverting the url, you can pass the kwargs parameter to the reverse function.
2. If you want to add parameters to the query string, you must manually
Stitch together.
# views.py from django.http import HttpResponse from django.shortcuts import reverse,redirect def index(request): username = request.GET.get('username') if username: return HttpResponse("home page") else: # login_url = reverse('login') + "?next=/" # return redirect(login_url) detail_url = reverse('detail',kwargs={'article_id':1,'page':2}) return redirect(detail_url) def login(request): return HttpResponse("Logon Page") def article_detail(request,article_id): text = 'Your article id yes:%s' % article_id return HttpResponse(text) # urls.py from django.urls import path from front import views urlpatterns = [ path('',views.index,name='index'), path('login/',views.login,name='login'), path('detail/<article_id>/<page>/',views.article_detail,name='detail') ]