Oct-30-2024, 02:36 PM
Hello team,
I am facing with the following issue when trying to download info to excel file, after clicking on hyperlink to download file the browser shows the issue: module 'openpyxl.workbook' has no attribute 'active'
but as per my understanding I have activated the workbook
this is the view/class on views.py
this is the error shown on the pyton terminal
Thanks in advance for help provided
I am facing with the following issue when trying to download info to excel file, after clicking on hyperlink to download file the browser shows the issue: module 'openpyxl.workbook' has no attribute 'active'
but as per my understanding I have activated the workbook
this is the view/class on views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Member
#from .resources import Memberexcel
from openpyxl import workbook
from django.views.generic import TemplateView
from .serializers import Member_Model_Serializer
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.authentication import SessionAuthentication
class MemberExcel(TemplateView):
def get(self, request, *args, **kwars):
memberset = Member.objects.all()
wb = workbook
ws = wb.active
ws['B1'] = 'Members Report'
ws.merge.cells('B1:E1')
ws['B3'] = 'FirstName'
ws['C3'] = 'LastName'
ws['D3'] = 'Phone'
ws['D3'] = 'joined date'
count = 4
for member in memberset:
ws.cell(row = count, column = 2).value = member.firstname
ws.cell(row = count, column = 3).value = member.lastname
ws.cell(row = count, column = 4).value = member.phone
ws.cell(row = count, column = 5).value = member.joined_date
count += 1
file_name = 'Members Report.xlsx'
response = HttpResponse(content_type='application/ms-excel')
content = "attachment; filename = {0}".format(file_name)
response['Content-Disposition'] = content
wb.save(response)
return response
###here the URL.py
from django.contrib import admin
from django.urls import path, include
# from rest_framework.routers import DefaultRouter
from . import views
from .views import MemberExcel
urlpatterns = [
path('', views.main, name='main'),
path('members/', views.members, name='members'),
path('members/details/<slug:slug>', views.details, name='details'),
path('testing/', views.testing, name='testing'),
path('admin/', admin.site.urls),
path('memberst/', views.memberst, name='memberst'),
path('memberst/MemberExcel/', MemberExcel.as_view(), name='MemberExcel'),
]for reference this is the line in my html template that calls the download: Quote: <p><a href="MemberExcel/">Export to excel</a></p>
this is the error shown on the pyton terminal
Quote:Traceback (most recent call last):
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\my_tennis_club\members\views.py", line 47, in get
ws = wb.active
AttributeError: module 'openpyxl.workbook' has no attribute 'active'
Thanks in advance for help provided
