新增一個測試頁
主要是進行對資料庫的動作
還有前端的展示
可以在瀏覽器上按F5就快速新增測試資料到資料庫內
doc_info/views.py 新增功能在此
from .models import doc_warehouse
def doc_test(request):
for i in range(3,50):
try:
Doc_warehouse = doc_warehouse(
user_id = i,
title = f"tittle {i}",
remark = f"remark {i}",
link = f"link {i}",
)
Doc_warehouse.save()
print(f"test doc {i} be created")
except Exception as e:
print(e)
# update doc
# Doc_warehouse = doc_warehouse
## Doc_warehouse.objects.filter(user_id=2).update(user_id=1, title=f"doc 1", )
return render(request, 'doc/test.html')
doc_info/urls.py 增加轉址規則
from django.urls import path
from . import views
app_name = "doc_info"
urlpatterns = [
path('doc/test/', views.doc_test, name='test'),
]
templates/doc/test.html 增加個頁面顯示
{% block content %}
<table border="1">
<tr><th>Attributes</th><th>Result</th></tr>
<tr><td>request</td><td>{{request}}</td></tr>
<tr><td>request.get_host</td><td>{{request.get_host}}</td></tr>
<tr><td>request.encoding</td><td>{{request.encoding}}</td></tr>
<tr><td>request.method</td><td>{{request.method}}</td></tr>
<tr><td>request.scheme</td><td>{{request.scheme}}</td></tr>
<tr><td>request.data</td><td>{{request.data}}</td></tr>
<tr><td>request.path</td><td>{{request.path}}</td></tr>
<tr><td>request.path_info</td><td>{{request.path_info}}</td></tr>
<tr><td>request.content_type</td><td>{{request.content_type}}</td></tr>
<tr><td>request.content_params</td><td>{{request.content_params}}</td></tr>
<tr><td>request.GET</td><td>{{request.GET}}</td></tr>
<tr><td>request.POST</td><td>{{request.POST}}</td></tr>
<!-- <tr><td>request.COOKIES</td><td>{{request.COOKIES}}</td></tr> -->
<tr><td>request.FILES</td><td>{{request.FILES}}</td></tr>
<!-- <tr><td>request.META</td><td>{{request.META}}</td></tr> -->
<tr><td>request.headers</td><td>{{request.headers}}</td></tr>
<tr><td>request.headers.user_agent</td><td>{{request.headers.user_agent}}</td></tr>
<tr><td>request.read</td><td>{{request.read}}</td></tr>
<tr><td>request</td><td>{{request}}</td></tr>
<tr><td>request</td><td>{{request}}</td></tr>
<tr><td>request</td><td>{{request}}</td></tr>
<tr><td>request</td><td>{{request}}</td></tr>
<tr><td>request</td><td>{{request}}</td></tr>
</table>
<h2>Add ok</h2>
{% endblock %}
柳覽器進入http://127.0.0.1:8000/doc/test/
console 會印出資料庫變動的訊息