Job Search Engine
915 浏览 5 years, 8 months
2.17 列表视图提升 (1) 下载
版权声明: 转载请注明出处 http://www.codingsoho.com/列表视图提升 (1) 下载
添加下载模板代码,这儿使用post方法处理
<div class="pull-right">
<form id="export" method="POST" action="" style="float: left;padding-right: 10px;">{% csrf_token %}
<a class="fa fa-download fa-1x" href="javascript:;" onclick="document.getElementById('export').submit();"> {% trans "download" %}</a>
</form>
{% if request.user.is_staff %}
<a href=''><i class="fa fa-plus-square-o" aria-hidden="true"> {% trans "Create" %}</i></a>
{% endif %}
</div>
给创建留了接口,暂时未实现,如果需要创建CreateView,可以用它提供快捷入口
添加font awesome支持图表
<link rel="stylesheet" type="text/css" href="[https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css](https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css)">
后台处理
class JobEntryListView(PageMixin, ListView):
def post(self, *args, **kwargs):
qs = self.get_queryset()
qs = self.filter_class(self.request.GET, queryset=qs).qs
qs = self.further_filter(qs)
fields_display = []
fields_fk = [ ]
fields_datetime = ["created","updated", ]
from django.db import models
excludes = [field.name for field in self.model._meta.get_fields() if isinstance(field, models.ManyToOneRel)]
excludes.append('trigger_scrap_records')
fields_multiple = ["",]
from plugin.utils import gen_csv
return gen_csv(self.model, qs, "job_entry.csv", fields_display, fields_fk, fields_datetime, excludes, fields_multiple)
插件代码
from django.http import HttpResponse, HttpResponseRedirect
import csv
import codecs
from django.utils.encoding import force_str, force_text
from django.db import models
def gen_csv(model, qs, filename, fields_display, fields_fk, fields_datetime, excludes, fields_multiple=None, fields_property=None):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
response.write(codecs.BOM_UTF8) # add bom header
writer = csv.writer(response)
row = []
for field in model._meta.get_fields():
if field.name in excludes:
continue
row.append(field.verbose_name)
if fields_property:
for field in fields_property:
row.append(_(field[1]))
writer.writerow(row)
for obj in qs:
row = []
for field in model._meta.get_fields():
if field.name in excludes:
continue
#row.append(field.value_to_string(obj).encode('utf8'))
value = getattr(obj, field.name)
if value:
if field.name in fields_datetime:
if isinstance(field, models.DateTimeField):
value = value.strftime('%Y-%m-%d %H:%M:%S')
else:
value = value.strftime('%Y-%m-%d')
elif field.name in fields_display:
value = obj._get_FIELD_display(field)
elif field.name in fields_fk:
value = force_text(value, strings_only=True)
elif fields_multiple and field.name in fields_multiple:
str = "{0}{1}()".format("obj.get_",field.name)
value = eval(str)
value = "%s" % (value)
if fields_multiple and field.name in fields_multiple:
row.append(value)
else:
row.append(value.encode('utf8'))
else:
row.append("")
if fields_property:
for field in fields_property:
value = getattr(obj, field[0])
row.append(value)
writer.writerow(row)
return response