In Django, the view is a web request to respond
is a view of a python function defined in views.py file.
is defined views:
Example: writing myApp \ views.py in
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Sunck is a good man")
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myApp.urls')),
]
urls.py file created in the application catalog myApp:from django.urls import path, include
from . import views
urlpatterns = [
path('',views.index),
]
from django.contrib import admin
from django.conf.urls import url,include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myApp.urls')),
]
urls.py file created in the application catalog myApp:from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
]
Templates are HTML pages can be filled according to the data transfer from the view
create a template:
create templates directory, create a template corresponding to the directory of the project (project / templates / myApp) in the directory
template path configuration:
Modify 'DIRS' under TEMPLATES under settings.py file 'DIRS': [os.path.join (BASE_DIR, 'templates')],
is defined grades.html and students.html template:
create grades.html with students.html template files in \ myApp \ templates directory
Template Syntax:
{{output value can be a variable, or may be an object, attributes}}
{%}% executed code segment
http://127.0.0.1:8000/grades
write grades.html template:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Class Information</title>
</head>
<body>
<h1>Class List</h1>
<ul>
<!--[python04, python05, python06]-->
{%for grade in grades%}
<li>
<a href="#">{{grade.gname}}</a>
</li>
{%endfor%}
</ul>
</body>
</html>
Define a view: myApp \views.py
from .models import Grades
def grades(request):
# Template to fetch data
gradesList = Grades.objects.all()
# To pass data to the template, and then render the page, the rendered page returned to the browser
return render(request, 'myApp/grades.html', {"grades": gradesList})
Configuring url: myApp \urls.py
urlpatterns = [
url(r'^$', views.index),
url(r'^(\d+)/(\d+)$', views.detail),
url(r'^grades/', views.grades)
]
Page orientation to http://127.0.0.1:8000/students
template write students.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Students page</title>
</head>
<body>
<h1>Student Information List</h1>
<ul>
{%for student in students%}
<li>
{{student.sname}}--{{student.scontend}}
</li>
{%endfor%}
</ul>
</body>
</html>
Define a view: myApp \views.py
from .models import Students
def students(request):
studentsList = Students.objects.all()
return render(request, 'myApp/students.html', {"students": studentsList})
Configuring url: myApp \urls.py
urlpatterns = [
url(r'^$', views.index),
url(r'^(\d+)/(\d+)$', views.detail),
url(r'^grades/', views.grades),
url(r'^students/', views.students),
]
A view function (class), or view for short, is a simple Python function (class) that accepts web requests and returns web responses. The response can be the HTML content of a web page, a redirect, a 4...
Book website link This article mainly describes three aspects: 1. The basic label of the template; 2. The basic usage of the template; 3. The call of the period variable. A simple example template: &l...
template The template is an html page, which can be filled with values based on the data passed in the view The directory for creating templates is as shown below: Modify the settings.py file...
4.1 Using Templates Configuration template catalog Create a directory called Templates in the Django project configuration directory (/ Tango_with_django_project /). Note that this directory is to be ...
1. Install Django-Redis extension package $ pip install django-redis 2. Configure Redis database Configure the Redis database in the setting file...
Study notes, for reference only Reference from: Django builds the official website of large enterprises; This series of Blogs is mainly application-oriented, and the theoretical foundation part is inB...
Create a view turn on demo Applications inviews.py File, add the code The first view function must be defined, for receiving a request for data djangoHttpRequest Object, usually namedrequest Function ...
Article Directory 1. The function of the view 2. Use of view functions 3. Error view Custom 404 pages 4. Capture url parameters Position parameter Keyword parameters 5. The request parameter of the vi...
Django(mtV) Basic 4: View function Views Request object Return object HttpResponse render forward redirect Reverse analysis Keyword parameter method HttpResponse other subclasses Conversational techno...
Django learning - view and routing Ideas: Application layer----configuration parameters: method, routing, attribute parameters Control layer - use Django's function method to get the value passed by t...