D18 文件修改页 Modify doc

文件创建後可能要修改标记或是更改上传的档案
只能修改自己发的文件
先看使用者是否登入以及要修改的文件是不是属於此使用者
如果是get进入就从资料库里面将文件的资料调出来并设为预设内容
如果文件本身有附件但只是改标题或是remark 则附件延用
如果要上传新的附件则覆盖或是创建
doc_info/views.py

@login_required
def doc_modify(request,doc_id):
    user = request.user
    Doc_warehouse = doc_warehouse.objects.filter(user_id=user.id)
    try:
        doc = Doc_warehouse.get(id=doc_id)
        if request.method == "POST":
            user_id = request.user.id
            doc_create_date = doc_warehouse.objects.get(id=doc_id).create_date
            doc_title = request.POST.get('title','')
            doc_remark = request.POST.get('remark','')
            upload_file = request.FILES.get('upload_file',doc.upload_file)
            doc = doc_warehouse(
                id = doc_id,
                create_date = doc_create_date,
                user_id = user_id,
                title = doc_title,
                remark = doc_remark,
                upload_file = upload_file,
            )
            doc.save()
            return redirect('/doc/user/list')
        else:
            form = ModifyForm(initial={"title":f"{doc.title}", "remark":f"{doc.remark}"})
            context = {
                'form': form,
                'doc': doc,
            }
            return render(request, 'doc/modify.html', context)
    except Exception as e:
        print(e)
        return redirect('/doc/user/list')

与create最大不同是需要doc_id
doc_info/urls.py

urlpatterns = [
    path('doc/modify/<int:doc_id>', views.doc_modify, name='modify'),
]

自动带入文件资讯
templates/test.html

{% block content %}

<a href="{% url 'doc_info:main' %}">Main page</a> |
<a href="{% url 'auth_info:profile' %}">My Profile</a> | 
<a href="{% url 'account_logout' %}">Logout</a>

<form method="post" action="" enctype="multipart/form-data">
    {% csrf_token %}
    <h3>Modify Document</h3>
    <table>
        <tr>
            <th>Author: </th>
            <td>{{ user.first_name }} {{ user.last_name }}</td>
        </tr>
        <tr>
            <th>Contact Email: </th>
            <td>{{ user.email }}</td>
        </tr>        
        <tr>
            <th>Contact Phone: </th>
            <td>{{ user.userprofile.phone_number }}</td>
        </tr>
        <!-- need to have some gap here -->
        <tr>
            <th>Title: </th>
            <td>{{ form.title }}</td>
        </tr>
        <tr>
            <th>Remark: </th>
            <td>{{ form.remark }}</td>
        </tr>
        <tr>
            <th>Attachment: </th>
            <td>{{ doc.upload_file }}<input type="file" name="upload_file"></td>
        </tr>
    </table>
    <div class="button-wrapper submit">
        <input type="submit" value="Save" />
    </div>
</form>

{% endblock %}

modify page画面
Imgur


<<:  [13th][Day16] docker push

>>:  D23-(9/23)-宅配通(2642)-航运、空运、不能错过陆运

【Day 14】Function 函数

Functions 紧接着,我们就要来介绍函数了!写函数可以让我们的程序码更简洁明了也更有效率,因此...

Day 28 ~ AI从入门到放弃 - 猫狗辨识之三

今天延续之前的主题,我们将使用EfficientNetB0的架构,但不使用预训练权重,参考了Kera...

D7 - 如何用 Google Apps Script 将 Google 表单的回应即时同步在多个行事历上?

来到了第七天。老样子,先讲推荐的速解,如果你很急着用,这些 Add-On 可以帮上忙,第一是 For...

Day 12 - 物品借用纪录系统 (3) 系统完成

今天我们就把整个服务完成吧! 不知道大家顺利地收到通知了没? 我已经顺利收到罗~ 咦咦咦?发生什麽事...

Day_28:让 Vite 来开启你的Vue之 跌入深坑_生命周期 hooks 与 async/await

Hi Dai Gei Ho~ 我是Winnie ~ 在今天的文章中,我们要简单来说说 在 Lifec...