' WSGIRequest'对象没有属性方法

时间:2017-12-05 22:58:44

标签: python django

我知道这个问题已被多次询问,但我找不到解决问题的任何解决方案。基本上,我在Django的类视图中有一个方法,该方法调用另一个方法,该方法引发AttributeError'WSGIRequest' object has no attribute 'send_email'

这是代码:

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()

    # ...
    # ...

我尝试了以下事项:

  1. 重新排列方法send_email的顺序:位于approve_party上方,位于顶部,位于底部。
  2. 在不复制任何内容的情况下,在另一个编辑器中重写整个文件。
  3. 使用PyCharm将缩进转换为空格选项以确保缩进良好。
  4. 在课堂上写下其他虚拟函数,并尝试从approve_party调用它们 - 它们都不起作用。
  5. 这里是完整的追溯:

    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
    

    approve_party已在urls.py中注册,如下所示:

    urlpatterns = [
        # ...,
        url(r'^parties/approve/([0-9]*$)', views.PartyListView.approve_party, name="Approve Party"),
        # ...
    ]
    

1 个答案:

答案 0 :(得分:2)

您正在错误地使用基于类的视图。基于类的视图应使用as_view方法注册。

url(r'^parties/approve/([0-9]*$)', views.PartyListView.as_view(), name="Approve Party"),

但是,在这种情况下,看起来更简单的是使approve_partysend_email常规函数而不是您的视图方法。

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

然后将您的网址格式更改为:

url(r'^parties/approve/([0-9]+$)', views.approve_party, name="approve-party"),

我建议使用[0-9]+代替[0-9]*来确保网址至少包含一个数字,我会避免在网址模式名称中使用空格。