我有一个仪表板模板,用户可以在其中上传文件并显示它们。我想为此添加删除操作。我有一些想法,但需要一些帮助熨平它们。
我也不确定我应该在哪里使用FileStorage或自定义FileStorage后端。使用这些会比现在的解决方案更容易/更简单地管理文件(删除它们,列出它们等)吗?
首先是我的代码......
观点:
@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。并以这种方式处理它只是确保我返回与我的常规仪表板视图相同的模板和上下文?我可以使用带有DeleteView的CBV,但我不太确定,我仍然有与第一个想法相同的问题,以及它是否可以使用这种方法?有两个视图呈现相同的模板,但删除只会在您按下删除按钮时调用,该按钮将是一个路由到此视图的链接?这里有什么问题吗?
第三,我应该为此使用基于类的视图吗?通过覆盖派遣和邮寄?有没有其他方法我可以覆盖删除?我宁愿不这样做。
基本上哪种解决方案能够发挥最佳作用?有更好的解决方案吗?我基本上让他们登录,但他们不是工作人员,无法访问管理界面。我知道他们应该能够在上传文件时删除这些文件,但是我需要另外的权限吗?有没有办法可以通过覆盖或其他任何方式自定义此删除操作?
任何帮助和示例对我来说都是一个巨大的好处,我已经被困在这一点并可以使用任何帮助。
我真的需要帮助找到最好的方法,这似乎是第二种解决方案,我只是不确定如何完全实现它以及我是否可以自定义我提到的东西,如删除方法(我可以将已删除的文件发送到隐藏的回收站(只是另一个文件夹),以便只有管理员可以看到它们?)。它会以我想要的方式工作吗?
任何信息都会有很大帮助,因为我的测试似乎总是有问题。在我不断测试时会继续发布更新,但任何输入都会有很大的帮助,因为我说我已经被卡住了一段时间。
提前致谢。
编辑:
文件列表模板:
{% 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>
答案 0 :(得分:2)
它实际上可能非常简单,编写另一个视图方法来处理删除事件。为模板中的每个条目指定一个按钮,该按钮应指向链接到删除视图方法的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')
答案 1 :(得分:2)
如果您愿意,可以使用GenericViews:https://docs.djangoproject.com/en/1.8/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView
这很简单。例如:
<强> views.py:强>
class ClientUploadDelete(DeleteView):
model = ClientUpload
success_url = reverse_lazy('dashboard')
template_name = 'client_upload_delete.html'
<强> urls.py:强>
url(r'^delete/(?P<pk>\d+)/$', ClientUploadDelete.as_view(), name="delete_client_upload"),
<强> HTML:强>
<button type="button">
<a href="{% url 'client_upload_delete' %}">Delete</a>
</button>
就是这样。将提示用户使用一个屏幕来验证他们是否要删除该对象。如果他们回答是,则会在上面的示例中将其重定向到dashboard.html
。
修改强>
client_upload_delete.html
<div class='col-sm-6 col-sm-offset-3'>
{% if request.user == object.user %}
<p>Are you sure you want to delete this object?</p>
# Put your object here (image, text item, etc.)
<form action="" method="POST">
{% csrf_token %}
<input type="submit" value="Delete" />
<input type="button" value="Cancel" onclick="window.location='{% url 'dashboard' %}'"/>
</form>
{% else %}
<h4>You're not allowed to be here.</h4>
<a href="{% url 'dashboard' %}">Dashboard</a>
{% endif %}
</div>