Description of the relationship between Django URL and View
- 2021-09-24 23:08:02
- OfStack
Relationship between URL and View
1. Every time a user sends an HttpRequest request, Django will match the request with URL_pattern, and when the first URL_pattern is matched, the request will be transferred to the corresponding view
2. view is used to respond to request and return response. response can contain web page files, pictures and so on. So most of the code in the project is written here.
view can introduce common templates to reduce the amount of code, see the documentation for details
Use generic views
3. The method of introducing HTML file into view:
Create an templates folder under an app, and put the html file into the folder
In fact, it is not necessary to put the HTML file in this templates, which can be defined at will
However, you need to find templates in setting. py.
Modify DIRS: [os. path. join (BASE_DIR, 'Folder where you define html')]
Under app view. py file, write view function in it
def function name (request):
return render(request,'index.html')
render (rendering) requires 3 variables, the first variable is request request, the second is the template directory, the third is a dictionary (optional), dictionary for the corresponding template design variables, you can use locals () function design variables automatically converted into dictionaries
4. Create an urls. py under app and write the following
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
5. Find urls. py in the project folder and add the following
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
For < a > href in the label can be replaced by {% url 'url name'%}, and url name refers to path in urls. py ('login. html/', views.my_login, name= 'url name').
get_object_or_404 () is commonly used in Django to catch 404 errors instead of writing try... except
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
Supplement: Configuration method of url and view in django
Configuration method of url and view in django (1)
url.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.showAssets, name='show_assets'),
]
views.py
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
# Create your views here.
def showAssets(request):
return render_to_response('assets_index.html', {})
Configuration method of url and view in django (2)
url.py
# -*- coding: utf-8 -*-
from django.conf.urls import url
from app001.views import IndexView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', IndexView.as_view(), name="index"), ]
views.py
# -*- coding: utf-8 -*-
from django.views.generic.base import View
from django.shortcuts import render
# Create your views here.
class IndexView(View):
def get(self, request):
return render(request, 'index.html', {})