Django⾃学之前端HTML上传⽂件读取⽂件内容并⼊库,将⼊
库数据展⽰在前台页⾯列表
1.创建⼯程temp,同时创建app应⽤song01app
2.在创建的app应⽤song01app下创建⽂件⽬录"templates"——》"song01app"——》"upload_file.html"
3.在创建的app应⽤song01app下创建urls.py⽂件
4.在⼯程temp下的settings.py⽂件下添加应⽤名song01app
5.编写temp下的urls.py⽂件
ib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('temp/', include('song01app.urls'))
]
6.编写应⽤song01app下的urls.py⽂件
from django.urls import path
from song01app import views
urlpatterns = [
path('upload/', views.upload_file)
]
7.在应⽤song01app下创建monkey_log_file⽬录,⽤于存放上传的⽇志⽂件
8.编写upload_file.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MonkeyTest</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
<h1>Monkey 测试</h1>
请输⼊测试执⾏⼈:<input type="text" name="peoples">
请输⼊测试版本:<input type="text" name="versions"><br>
请上传monkey⽇志⽂件<input type="file" name="files"><br>
<input type="submit" value="上传">
<br>
<b><small>测试执⾏结果:</small></b><hr>
</form>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>测试执⾏⼈</td>
<td>测试版本号</td>
<td>ANR异常数</td>
<td>Crash异常数</td>
<td>Exception异常数</td>
<td>是否执⾏完</td>
<td>统计合计</td>
<td>备注</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>django怎么学
8.编辑song01app下的views.py⽂件
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def upload_file(request):
hod == "POST":
File = ("files", None)
if File is None:
return HttpResponse("请选择需要上传的monkey⽇志⽂件")
else:
with open("./song01app/monkey_log_file/%s" % File.name, 'wb+') as  f:                for chunk in File.chunks():
f.write(chunk)
return render(request, "song01app/upload_file.html")
else:
return render(request, "song01app/upload_file.html")
9.编辑song01app应⽤下的models.py⽂件
from django.db import models
# Create your models here.
class result(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=150, blank=False, help_text='测试执⾏⼈')
version_num = models.PositiveIntegerField(blank=False,  help_text='版本号')
anr_num = models.IntegerField(blank=True,  help_text='ANR异常数')
crash_num = models.IntegerField(blank=True,  help_text='crash异常数')
exception_num = models.IntegerField(blank=True,  help_text='exception异常数')
monkey_is_finish = models.BooleanField(default=False, help_text='是否正常结束')
bug_num = models.IntegerField(blank=True, null=False, help_text='bug数量')
remark = models.CharField(max_length=30, blank=False, null=False, help_text="备注")
10.在应⽤song01app下创建⼀个num_count.py⽂件,⽤于处理⽂件中的数据
def _out_log(request):
contains = json.loads(_open_andreadfile(request))
people = ("peoples", None)
version = ("versions", None)
if contains:
crash_num=contains['crash'], exception_num=contains['exception'], monkey_is_finish=contains['is_finish'],                            name=people, version_num=version, remark="写死到备注")
list = sult.objects.all()
return list
else:
crash_num=contains['crash'], exception_num=contains['exception'],monkey_is_finish=contains['is_finish'],                            name=people, version_num=version, remark="写死到备注")
def _open_andreadfile(request):
File = ("files", None)
log_out_str = "./song01app/monkey_log_file/" + File.name
log_content = open(log_out_str, "r")
s = ad()
anr = s.count('ANR')
crash = s.count('CRASH')
exception = s.count('Exception')
monkey_is_finish = s.count('Monkey finished')
total = anr+crash+exception
is_finish = False
if (monkey_is_finish > 0):
is_finish = True
log_content.close()
_result_list = json.dumps({'anr': anr, 'crash': crash, 'exception': exception, 'is_finish': is_finish, 'total': total})
return  _result_list
11.修改views.py和upload_file.html⽂件标红的地⽅,图1是views.py图2是upload_file.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。