iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
Modern Web

Python x Django 網站實作&學習記錄系列 第 14

D14 重新設定create date & 上傳功能測試

將create date的auto_now_add刪除 並加入upload相關欄位
DateTimeField()設定auto_now_add的話 會在創建文件的時候自動加入時間戳記
後續如果使用update(只調用sql)也沒問題
但如果用save(走過model)
你不傳值給他,他會把create date設為null
你傳值給他,他會說這個欄位是不可更改的
一般來說都會用update更新
但是上傳附件要用save,會經過model層,所以一定會遇到這個問題
然後新增加upload file需要的兩個欄位
指定upload_to會將檔案儲存到upload資料夾內
doc_info/models.py

from django.db import models
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress

class doc_warehouse(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='doc_warehouse')
    create_date = models.DateTimeField()
    last_mod_date = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=128, blank=True)
    remark = models.CharField(max_length=128, blank=True)
    status = models.IntegerField(default=1, null=True, blank=True)
    link = models.CharField(max_length=128, blank=True)
    upload_file_name = models.CharField(max_length=128, blank=True)
    upload_file = models.FileField(upload_to='upload',null=True, blank=True)

下列方法可以讓保持create date然後又更新其他資訊
doc_info/views.py

def doc_test(request):
    i=5
    doc_create_date = doc_warehouse.objects.get(id=153).create_date
    doc = doc_warehouse(
        id = 153,
        create_date = doc_create_date,
        user_id = i,
        title = f"tittle {i}",
        remark = f"remark {i}",
        link = f"link {i}",
    )
    doc.save()
    return render(request, 'doc/test.html')

下列方法可以將用POST方式傳到test的資訊跟檔案上傳到資料庫內
doc_info/views.py

def doc_test(request):
    if request.method == "POST":
        upload_file_name = request.POST["fileTitle"]
        upload_file = request.FILES["uploadedFile"]
         document = doc_warehouse(
            user_id = 4,
            upload_file_name = upload_file_name,
            upload_file = upload_file,
        )
        print(document)
        document.save()
    return render(request, 'doc/test.html')

加入table展示request傳送過來的訊息
建立一個簡單的上傳功能form
templates/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>


</table>
<h2>Add ok</h2>

<body>
    <form action="{% url 'doc_info:test' %}" method="POST" enctype="multipart/form-data">
        <input type="text" name="fileTitle" placeholder="Enter a title">
        <input type="file" name="uploadedFile">
        {% csrf_token %}
        <input type="submit" value="Upload">
    </form>   
</body>
{% endblock %}

Imgur


上一篇
D13 刪除特定的使用者文件
下一篇
D15 下載功能測試
系列文
Python x Django 網站實作&學習記錄30

尚未有邦友留言

立即登入留言