I know this question has been asked many times, but I haven't found any solution that solved my problem. Basically, I have a method inside a class view in Django, and that method calls another method, which raises an AttributeError
: 'WSGIRequest' object has no attribute 'send_email'
.
This is the code:
class PartyListView(ListView):
# ...
# ...
def approve_party(self, party_id):
returnObj = {}
# get the party by id
party = party_repo.get_party_by_id(party_id)
if party is None:
returnObj['message'] = "The requested party doesn't exist."
jsonObj = json.dumps(returnObj)
return HttpResponse(jsonObj, status=404, content_type="application/json")
# change the status to Approved
party.party_status = Core.PartyStatus.Approved
party.save()
# email the party team on the approval
self.send_email(True, party)
# return a json success response
returnObj['message'] = "The party has been approved."
jsonObj = json.dumps(returnObj)
return HttpResponse(jsonObj, status=200, content_type="application/json")
def send_email(self, isApprovalEmail, party):
username = self.request.user.username
from_email = username + "@example.com"
recipient_list = [party.host.student_email]
cc = ['admin@example.com']
if isApprovalEmail:
subject = "APPROVED Party Registration for {}".format(party.party_time)
body = "approval email"
email = EmailMessage(subject, body, from_email, recipient_list, cc=cc)
email.send()
else:
subject = "REJECTED Party Registration for {}".format(party.party_time)
body = "rejection email"
email = EmailMessage(subject, body, from_email, recipient_list, cc=cc)
email.send()
# ...
# ...
I have tried the following things:
send_email
: Above approve_party
, below, at the top, at the bottom. approve_party
- none of them works. Here's the full traceback:
Internal Server Error: /admin/parties/approve/151
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:/Programming/Party-Reg\admin_dashboard\views.py", line 93, in approve_party
self.send_email(True, party)
AttributeError: 'WSGIRequest' object has no attribute 'send_email'
[05/Dec/2017 18:07:19] "GET /admin/parties/approve/151 HTTP/1.1" 500 17786
And approve_party
is registered in urls.py
as follows:
urlpatterns = [
# ...,
url(r'^parties/approve/([0-9]*$)', views.PartyListView.approve_party, name="Approve Party"),
# ...
]
You are using class based views incorrectly. Class based views should be registered with the as_view
method.
url(r'^parties/approve/([0-9]*$)', views.PartyListView.as_view(), name="Approve Party"),
However, in this case, it looks as it would be simpler to make approve_party
and send_email
regular functions instead of methods of your view.
def approve_party(request, party_id):
returnObj = {}
...
send_email(request, True, party)
return HttpResponse(jsonObj, status=200, content_type="application/json")
def send_email(request, isApprovalEmail, party):
username = request.user.username
Then change your URL pattern to:
url(r'^parties/approve/([0-9]+$)', views.approve_party, name="approve-party"),
I suggest using [0-9]+
instead of [0-9]*
to make sure that the URL includes at least one digit, and I would avoid using spaces in URL pattern names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.