比方说,用户无法在计算机上找到该文件夹,因此可以更轻松地管理网页上的文件夹文件。我设法在网页/ Here is that webpage /上显示文件夹文件文档,然后尝试下载或删除所需的文档,但是出现一些错误。
我尝试使用os.path.exists()和os.remove()删除文档,但是据我了解,我没有正确创建url模式,也没有将正确的参数传递给html模板。
这是我的代码:
....
path('edited_agreements/', views.delete_new_file, name='delete_new_file'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
def new_files_list(request):
path = MEDIA_ROOT
new_files_list = []
for file in os.listdir(path):
if file.endswith('.doc') or file.endswith('.docx'):
new_files_list.append(file)
return render(request, 'uploads/new_files_list.html', {
'new_files': new_files_list
})
def delete_new_file(request, path):
if request.method == 'POST':
file_path = os.path.join(MEDIA_ROOT, path)
if os.path.exists(file_path):
os.remove(file_path)
return redirect('new_files_list')
....
<tbody>
{% for new_file in new_files %}
<tr>
<td>{{ new_file }}</td>
<td>
<form method="post" action="{{ new_file.path }}" target="_blank">
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm">Download</button>
</form>
</td>
<td>
<form method="post" action="{{ new_file }}">
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
....
当我单击删除时,我希望从我的网页和文件夹中删除该文件,而当我要下载时,则要下载该文件。现在我收到以下错误:
The current path, edited_agreements/222222.docx, didn't match any of these.