Skip to content

Instantly share code, notes, and snippets.

@daimon99
Created September 4, 2019 10:39
Show Gist options
  • Save daimon99/f77d5f750b91bb9c6f06c8e1ec882466 to your computer and use it in GitHub Desktop.
Save daimon99/f77d5f750b91bb9c6f06c8e1ec882466 to your computer and use it in GitHub Desktop.
Django Admin 页面中间页操作
{% extends 'admin/base_site.html' %}
{% load i18n admin_urls static admin_list %}
{% block content %}
<form action="" method="POST">
{% csrf_token %}
<div>
<fieldset class="module aligned">
<div class="form-row">
<div>
<label class="required" for="id_local_file_full_path">
文件路径:
</label>
<input type="text" name="local_file_full_path" style="width: 400px;" required
id="id_local_file_full_path">
</div>
</div>
</fieldset>
<div class="submit-row">
<input type="hidden" name="action" value="assign_task"/>
<input type="submit" name="apply" value="提交"/>
&nbsp
<button type="button" name="cancel" onclick="history.back(-1);" class="module">取消</button>
</div>
</div>
</form>
{% endblock %}
@admin.register(m.ShareStorage)
class ShareStorageAdmin(admin.ModelAdmin):
...
def get_urls(self):
from django.urls import path
def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
wrapper.model_admin = self
return update_wrapper(wrapper, view)
info = self.model._meta.app_label, self.model._meta.model_name
urlpatterns = [
path('', wrap(self.changelist_view), name='%s_%s_changelist' % info),
path('add/', wrap(self.add_view), name='%s_%s_add' % info),
path('add_local/', wrap(self.add_local_file_view), name='%s_%s_add_local' % info),
path('autocomplete/', wrap(self.autocomplete_view), name='%s_%s_autocomplete' % info),
path('<path:object_id>/history/', wrap(self.history_view), name='%s_%s_history' % info),
path('<path:object_id>/delete/', wrap(self.delete_view), name='%s_%s_delete' % info),
path('<path:object_id>/change/', wrap(self.change_view), name='%s_%s_change' % info),
# For backwards compatibility (was the change url before 1.9)
path('<path:object_id>/', wrap(RedirectView.as_view(
pattern_name='%s:%s_%s_change' % ((self.admin_site.name,) + info)
))),
]
return urlpatterns
def add_local_file_view(self, request):
context = {
'title': '增加本地文件'
}
if 'apply' in request.POST:
local_file_full_path = request.POST.get('local_file_full_path')
if not os.path.exists(local_file_full_path):
self.message_user(request, f'文件不存在: {local_file_full_path}')
return HttpResponseRedirect(request.get_full_path())
else:
normal_upload_user_file_2(request.user, '/', os.path.basename(local_file_full_path),
local_file_full_path)
self.message_user(request,
"上传成功")
return HttpResponseRedirect(reverse('admin:dapi_sharestorage_changelist'))
return render(request, 'admin/sharestorage/add_local_file.html', context=context)
{% extends 'admin/change_list.html' %}
{% load i18n admin_urls static admin_list %}
{% block object-tools-items %}
{{ block.super }}
<li>
<a href="/admin/dapi/sharestorage/add_local/" class="addlink">
增加本地文件
</a>
</li>
{% endblock %}
@daimon99
Copy link
Author

daimon99 commented Sep 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment