2、Django-视图函数-views.py
1、编写视图
urls.py
__________________________________________________________________________
from django.contrib import admin
from django.urls import path
from user.views import * #先导入应用中的视图函数模块
urlpatterns = [
#路由
#path(访问的路径, 函数名-不带括号)
path('myfirst/', first_view),
]
__________________________________________________________________________
views.py
_________________________________________________________________________
from django.http import HttpResponse #导入http模块用于返回响应
def first_view(request):
pass
#返回相应的response
return HttpResponse('这个视图函数相当于后端程序!这个函数需要在 urls.py 里面去调用 ')
_____________________________________________________________________________________________
2、运行
python.exe .\manage.py runserver
3、在浏览器输入
http://127.0.0.1:8000/myfirst/
#注意以上步骤要一气呵成
使用渲染模式
1、views.py
-------------------------------------------------------------------------------
from django.shortcuts import render
#from django.http import HttpResponse
def app_test(request):
#return HttpResponse("hello world!")
return render(request, 'index.html') #配合html 来做渲染
----------------------------------------------------------------------------------
2、urls.py
-----------------------------------------------------------------------------------
from django.contrib import admin
from django.urls import path
from app_name.views import *
urlpatterns = [
path('index/', app_test),
]
--------------------------------------------------------------------------------------
3、html :需要在应用下创建一个 - templates 目录 、在目录下创建index.html文件
-- index.html
-------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>首页</h2>
<h3>这是使用render模块做的渲染</h3>
</body>
</html>
-------------------------------------------------------------------------------------