django自带后台页面,也提供了自定义页面的方法。
但有时我们并不满足与django自己的页面,也不想全面码一张新页面然后费力的实现一些功能,我们的目的可能只是想要在自带页面上添加一些自定义按钮,表单等等
django的强大的功能自然允许你这样做
1.templates下cexport_refund_to_oss.html
<div>
<form action="/cexport_refund_to_oss_Plugin/" method="post">
<span style="height: 30px;width: 180px;font-size: 15px">
时间区间:<input type="date" name="StrTime"/>
---
<input type="date" name="EndTime"/>
<input type="submit" name="cex_btn" value="导出CSV"/>
<select name="refund" onchange="window.open(this.options[this.selectedIndex].value,target='_blank')" style="color: blue">
{% for upurl in URL %}
<option value="{{ upurl }}">
Download --> <a href="{{ upurl }}" >{{ upurl }}</a>
</option> <br/>
{% endfor %}
</select>
</span>
</form>
</div>
<br/>
2.url.py
url(r'^cexport_refund_to_oss_Plugin/$', cexport_refund_to_oss_Plugin, name='cexport_refund_to_oss_Plugin'),
3.view.py
from brick.wish.cexport_refund_to_oss import *
from brick.db import dbconnect
from django.template import context
import datetime
#from djcelery.tasks import *
def cexport_refund_to_oss_Plugin(request):
if request.method == 'POST':
StrTime = request.POST.get('StrTime','')
EndTime = request.POST.get('EndTime','')
if not StrTime or not EndTime:
messages.error(request, '请正确输入查询时间区间!')
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded')
st = datetime.datetime.strptime(StrTime, "%Y-%m-%d")
ed = datetime.datetime.strptime(EndTime, "%Y-%m-%d")
daycount = (ed - st).days
if daycount < 0 :
messages.error(request, '请正确输入查询时间区间!')
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded')
if daycount > 60:
messages.error(request, '请输入两月内查询时间区间!')
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded')
from app_djcelery.tasks import *
db_conn = dbconnect.run({})['db_conn']
params = {'db_conn':db_conn, 'StrTime': StrTime, 'EndTime': EndTime}
cexport_refund_to_oss_obj = cexport_refund_to_oss()
cexport_refund_to_oss_obj.fexport_refund_to_oss(params)
#加入消息任务队列
#exportToOSSTask.delay(params)
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded')
if result['errorcode'] == 0:
URL = result['osspath']
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded/?exp_url=%s'%URL)
return HttpResponseRedirect('/Project/admin/skuapp/t_order_refunded')
4.xadmin下
xadmin.site.register_plugin(cexport_refund_to_oss_Plugin, ListAdminView)
5.plugin下
from xadmin.views import BaseAdminPlugin
from django.template import loader
from brick.function.find_oss_file import *
import oss2
ossinfo = {'ACCESS_KEY_ID': '', 'ACCESS_KEY_SECRET': '', 'ENDPOINT_OUT': '', '': ''}
class cexport_refund_to_oss_Plugin(BaseAdminPlugin):
#初设False
cexport_oss = False
def init_request(self, *args, **kwargs):
return bool(self.cexport_oss)
def get_media(self, media):
media.add_js([self.static('xadmin/js/xadmin.plugin.button.color.js')])
return media
def block_search_cata_nav(self, context, nodes):
URL = ''
find_oss_file_obj = find_oss_file()
result = find_oss_file_obj.findfile(ossinfo=ossinfo, timerange=86400000*3,urlload='refund')
if result['errorcode'] == 0:
URL = result['fileurl']
nodes.append(loader.render_to_string('cexport_refund_to_oss.html', {'URL':URL}))
6.admin下
cexport_oss = True