python 3.8
django 3.0.5
pip install pymysql
浏览器 -urls.py - views.py -index 函数
创建一个 应用模块
python manage.py startapp myapp
from myapp import views
urlpatterns = [
# path('admin/', admin.site.urls),
path('index', views.index),
]
from django.shortcuts import render ,HttpResponse
def index(request):
return HttpResponse("<h1>首页</h1")
结合html 使用
path('logs', views.logs),
from django.urls import path
from myapp import views
urlpatterns = [
# path('admin/', admin.site.urls),
path('index', views.index),
path('hello', views.hello),
path('logs', views.logs),
]
打开文件 ,读取文件内容到result 结合html 模板渲染
def logs(request):
import os
# print(os.getcwd())
# 使用路径
pro_path = os.getcwd()
pro_path = os.path.join(pro_path,"myapp","access.log")
print(pro_path)
with open(pro_path,encoding='utf-8') as f:
result = f.read()
print(result)
return render(request,"logs.html",{"result": result})
pre 标签可以保证文本格式不变
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>日志</title>
</head>
<body>
<h1>nginx 访问日志</h1>
<pre style="color: red">{{ result }}</pre>
</body>
</html>