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> -------------------------------------------------------------------------------------
本文作者:little小新
本文链接:https://www.cnblogs.com/littlecc/p/18111582
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
标签:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步