Python Django向模板添加删除操作
我有一个仪表板模板,用户可以上传文件并显示它们。我想在此添加删除操作。我有一些想法,但需要一些帮助来解决 我也不确定应该在哪里使用文件存储或自定义文件存储后端。使用这些工具是否会比我现在的解决方案更容易/更简单地管理文件(删除文件、列出文件等) 首先是我的代码 观点:Python Django向模板添加删除操作,python,django,Python,Django,我有一个仪表板模板,用户可以上传文件并显示它们。我想在此添加删除操作。我有一些想法,但需要一些帮助来解决 我也不确定应该在哪里使用文件存储或自定义文件存储后端。使用这些工具是否会比我现在的解决方案更容易/更简单地管理文件(删除文件、列出文件等) 首先是我的代码 观点: @login_required(login_url='/dashboard-login/') def dashboard(request): current_user = request.user current_
@login_required(login_url='/dashboard-login/')
def dashboard(request):
current_user = request.user
current_client = request.user.client
files = ClientUpload.objects.filter(client=current_client)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
dz_files = request.FILES.getlist('file_upload')
for f in dz_files:
new_file = ClientUpload(client=current_client, file_upload=f)
new_file.save()
logger = logging.getLogger(__name__)
logger.info("File uploaded from " + current_client.company)
else:
logger = logging.getLogger(__name__)
logger.warning("Upload Failed")
return HttpResponseRedirect(reverse('dashboard'))
else:
form = UploadFileForm()
data = {'form': form, 'client': current_client, 'files': files}
return render_to_response('dashboard.html', data, context_instance=RequestContext(request))
这是我的仪表板视图。如果请求是POST,那么我将处理表单
这是我的模型:
@python_2_unicode_compatible
class ClientUpload(models.Model):
client = models.ForeignKey(Client)
created_at = models.DateTimeField(auto_now_add=True)
file_upload = models.FileField(upload_to=generate_filename)
def __str__(self):
return self.client.company
class Meta:
verbose_name_plural = _("Client Uploads")
verbose_name = _("Client Upload")
我的想法是:
首先,我是否可以在不是POST方法时使用else,并尝试在那里处理删除操作?我试图删除的是代表该文件的ClientUpload模型。删除模型操作是否使用POST?我想是的,那么我怎么知道他们是在提交表单还是在删除什么呢
这就引出了我的第二个想法
第二,我能再看一眼吗?称为类似dashboard_delete的东西。并以这种方式进行处理,确保返回与常规仪表板视图相同的模板和上下文?我可以将CBV与DeleteView结合使用,但我不太确定,我仍然有与第一个想法相同的问题,以及它是否能与这种方法一起工作?有两个视图呈现相同的模板,但只有当您按下delete按钮时才会调用delete,这将是路由到此视图的链接?这里有什么问题吗
第三,我应该为此使用基于类的视图吗?通过凌驾于调度和发布之上?是否有其他方法可以替代删除?我宁愿不做这个
基本上,这三种解决方案中哪一种效果最好?有更好的解决办法吗?我基本上让他们登录,但他们不是员工,无法访问管理界面。我知道他们应该能够在上传这些文件时删除这些文件,但我需要其他权限吗?有没有办法通过重写或其他方式自定义此删除操作
任何帮助和例子都会给我带来巨大的好处,我在这方面已经有一段时间了,需要任何帮助
我只是真的需要帮助找到最好的方法,这似乎是第二个解决方案,我只是不知道如何准确地实现它,我是否可以自定义我提到的东西,如删除方法(我可以将删除的文件发送到一个隐藏的回收站(只是另一个文件夹),以便只有管理员可以看到它们?)。它会按照我的意愿工作吗
任何信息都会有很大帮助,因为我的测试似乎总是有问题。我会在不断测试的过程中不断发布更新,但任何输入都会有很大帮助,正如我所说的,我有点被卡住了
提前谢谢
编辑:
文件列表的模板:
{% load i18n %}
{% load staticfiles %}
{% load sasite_filters %}
<table class="table">
<tr>
<th>{% blocktrans %}Filename{% endblocktrans %}</th>
<th>{% blocktrans %}Size (Bytes){% endblocktrans %}</th>
<th>{% blocktrans %}Upload Time{% endblocktrans %}</th>
<th>{% blocktrans %}Actions{% endblocktrans %}</th>
</tr>
{% for file in files %}
{% with uploaded_file=file.file_upload %}
<tr>
<th><a href='{{ uploaded_file.url }}'>{{ uploaded_file.name|pathend}}</a></th>
<th>{{ uploaded_file.size }}</th>
<th>{{ file.created_at }}</th>
<th>Delete, Downloads, etc.</th>
{% endwith %}
{% endfor %}
</tr>
</table>
{%loadi18n%}
{%load staticfiles%}
{%load sasite_filters%}
{%blocktrans%}文件名{%endblocktrans%}
{%blocktrans%}大小(字节){%endblocktrans%}
{%blocktrans%}上载时间{%endblocktrans%}
{%blocktrans%}操作{%endblocktrans%}
{文件%中的文件为%s}
{%with upload_file=file.file_upload%}
{{上传的_file.size}
{{file.created_at}}
删除、下载等。
{%endwith%}
{%endfor%}
实际上可能非常简单,编写另一个视图方法来处理删除事件。为模板中的每个条目指定一个按钮,该按钮应指向链接到删除视图方法的url。在视图中,您可以执行任何删除模型对象的操作,然后重定向到仪表板视图
粗略的例子:
from django.http import HttpResponseRedirect
def delete(request, upload_id):
p = ClientUpload.objects.get(pk=upload_id)
p.delete()
return HttpResponseRedirect('your-dashboard-url')
实际上可能非常简单,编写另一个视图方法来处理delete事件。为模板中的每个条目指定一个按钮,该按钮应指向链接到删除视图方法的url。在视图中,您可以执行任何删除模型对象的操作,然后重定向到仪表板视图 粗略的例子:
from django.http import HttpResponseRedirect
def delete(request, upload_id):
p = ClientUpload.objects.get(pk=upload_id)
p.delete()
return HttpResponseRedirect('your-dashboard-url')
如果需要,可以使用GenericView: 这很简单。例如: 视图。py:
class ClientUploadDelete(DeleteView):
model = ClientUpload
success_url = reverse_lazy('dashboard')
template_name = 'client_upload_delete.html'
url(r'^delete/(?P<pk>\d+)/$', ClientUploadDelete.as_view(), name="delete_client_upload"),
<button type="button">
<a href="{% url 'client_upload_delete' %}">Delete</a>
</button>
url.py:
class ClientUploadDelete(DeleteView):
model = ClientUpload
success_url = reverse_lazy('dashboard')
template_name = 'client_upload_delete.html'
url(r'^delete/(?P<pk>\d+)/$', ClientUploadDelete.as_view(), name="delete_client_upload"),
<button type="button">
<a href="{% url 'client_upload_delete' %}">Delete</a>
</button>
如果需要,可以使用GenericView: 这很简单。例如: 视图。py:
class ClientUploadDelete(DeleteView):
model = ClientUpload
success_url = reverse_lazy('dashboard')
template_name = 'client_upload_delete.html'
url(r'^delete/(?P<pk>\d+)/$', ClientUploadDelete.as_view(), name="delete_client_upload"),
<button type="button">
<a href="{% url 'client_upload_delete' %}">Delete</a>
</button>
url.py:
class ClientUploadDelete(DeleteView):
model = ClientUpload
success_url = reverse_lazy('dashboard')
template_name = 'client_upload_delete.html'
url(r'^delete/(?P<pk>\d+)/$', ClientUploadDelete.as_view(), name="delete_client_upload"),
<button type="button">
<a href="{% url 'client_upload_delete' %}">Delete</a>
</button>
有关示例,请参见我的编辑。删除只是一个单独的视图方法。@gocht不,它不是一个
POST
请求,而是一个GET
。这就是为什么我没有在方法中指定任何内容。请看这里。因为我不知道你用什么作为主键,pk
是一个安全的赌注。从您的评论中,听起来您使用的是默认主键,因此arg1
是对象的id。是的,它是file.id
。您将作为arg1
传递的内容将成为视图方法中的upload\u id
变量名。抱歉,stackoverflow不是一个交互式练习场所。对于这些基本的django知识,您应该参考django文档。但是基本上你可以做url(r'^delete/(?P\d+)$”,views.delete,name='delete')
。有关示例,请参见我的编辑。删除只是一个单独的视图方法。@gocht不,它不是一个POST
请求,而是一个GET
。这就是为什么我没有在方法中指定任何内容。请看这里。因为我不知道你用什么作为主键,pk
是一个安全的赌注。从您的评论中,听起来您使用的是默认主键,因此arg1
是对象的id。是的,它是file.id
。您将作为arg1
传递的内容将成为视图方法中的upload\u id
变量名。抱歉,stackoverflow不是一个交互式练习场所。对于这些基本的django知识,您应该参考django文档。但是基本上你可以做url(r'^delete/(?P\d+)$”,views.delete,name='delete')
。有趣的扭曲:覆盖get
返回HttpResponse(status=405)
(方法不是