excel 使用 Django xlwt 导出时出错
Error while excel exporting using Django xlwt
我正在尝试使用 xlwt 库将信息从我的 Web 应用程序导出到 Excel 文件,但 urls.py
中出现以下错误
TypeError: export_excel_av_items() missing 1 required positional
argument: 'request'
models.py:
class Stock(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30, unique=True)
quantity = models.IntegerField(default=1)
is_deleted = models.BooleanField(default=False)
def __str__(self):
return self.name
views.py:
def export_excel_av_items(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="available-items.xls"'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('Available Items')
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns= ['Name', 'Qty']
for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style)
font_style = xlwt.XFStyle()
rows = Stock.objects.all().values_list(
'name', 'quantity')
for row in rows:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, str(row[col_num]), font_style)
wb.save(response)
return response
urls.py:
path('', views.StockListView.as_view(), name='inventory'),
path('new', views.StockCreateView.as_view(), name='new-stock'),
path('stock/<pk>/edit', views.StockUpdateView.as_view(), name='edit-stock'),
path('stock/<pk>/delete', views.StockDeleteView.as_view(), name='delete-stock'),
path('inventory/export-excel', views.export_excel_av_items(), name='export-excel'),
在 URL 中,你指向函数,你不调用它,所以下面一行
path('inventory/export-excel', views.export_excel_av_items(), name='export-excel'),
应
path('inventory/export-excel', views.export_excel_av_items, name='export-excel'),
我正在尝试使用 xlwt 库将信息从我的 Web 应用程序导出到 Excel 文件,但 urls.py
中出现以下错误TypeError: export_excel_av_items() missing 1 required positional argument: 'request'
models.py:
class Stock(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30, unique=True)
quantity = models.IntegerField(default=1)
is_deleted = models.BooleanField(default=False)
def __str__(self):
return self.name
views.py:
def export_excel_av_items(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="available-items.xls"'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('Available Items')
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns= ['Name', 'Qty']
for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style)
font_style = xlwt.XFStyle()
rows = Stock.objects.all().values_list(
'name', 'quantity')
for row in rows:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, str(row[col_num]), font_style)
wb.save(response)
return response
urls.py:
path('', views.StockListView.as_view(), name='inventory'),
path('new', views.StockCreateView.as_view(), name='new-stock'),
path('stock/<pk>/edit', views.StockUpdateView.as_view(), name='edit-stock'),
path('stock/<pk>/delete', views.StockDeleteView.as_view(), name='delete-stock'),
path('inventory/export-excel', views.export_excel_av_items(), name='export-excel'),
在 URL 中,你指向函数,你不调用它,所以下面一行
path('inventory/export-excel', views.export_excel_av_items(), name='export-excel'),
应
path('inventory/export-excel', views.export_excel_av_items, name='export-excel'),