每当API中的POST请求发送邮件

时间:2018-09-12 11:17:04

标签: django django-rest-framework vuejs2

我正在使用django和django-rest-framework来提供一些API。 Vue.js用于将这些API作为前端框架进行交互。我能够在“ Axios”的帮助下发送POST请求数据,并且数据正在django中保存。

我想在发生POST请求时发送邮件。最初,当我使用django-forms时,发送邮件很容易,因为我会在form.save()之前发送send_mail。但是现在我有点困惑

views.py

class StudentQueryViewSet(viewsets.ModelViewSet):
  queryset = StudentQuery.objects.all()
  serializer_class = StudentQuerySerializer

  def create(self, request, *args, **kwargs):
    query=self.get_object()
    serializer = StudentQuerySerializer(data=request.DATA)
    if serializer.is_valid():
      serializer.save()
      msg = "A new query has been added in Category" + str(query.category)
      msg += "\n Name: " + query.name
      msg += "\n Contact: " + str(query.contact)
      msg += "\n Subject: " + str(query.subjects)
      msg += "\n Standard: " + str(query.standard)
      msg += "\n Address: " + str(query.address)
      send_mail(emailSubject, message, emailFrom, emailList, fail_silently=False)
      return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

使用此创建方法时,我被断言错误击中。这是回溯

Internal Server Error: /api/student-query/
Traceback (most recent call last):
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/f1uk3r/Coding/edhusk-project/edhusk2/clientforms/views.py", line 21, in create
    query=self.get_object()
  File "/home/f1uk3r/.virtualenvs/edhusk-project/lib/python3.5/site-packages/rest_framework/generics.py", line 94, in get_object
    (self.__class__.__name__, lookup_url_kwarg)
AssertionError: Expected view StudentQueryViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

routers.py

from clientforms.views import  StudentQueryViewSet
router.register(r'student-query', StudentQueryViewSet)

urls.py

from .routers import router
urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include(router.urls)),
]

2 个答案:

答案 0 :(得分:1)

您应该删除对get_object的调用,并将序列化程序保存的结果分配给query

def create(self, request, *args, **kwargs):
    serializer = StudentQuerySerializer(data=request.DATA)
    if serializer.is_valid():
      query = serializer.save()
      msg = "A new query has been added in Category" + str(query.category)
      ...

(请注意,query是在此处使用的令人困惑的名称;为了与您的其余代码保持一致,我将其保留下来,但实际上您应该使用类似obj的名称。)

答案 1 :(得分:0)

另一种选择是保持视图集和序列化器不变,而不用覆盖create方法来发送电子邮件。

并使用信号代替。

您可以为pre_savepost_save信号注册一个侦听器。然后从那里发送电子邮件。

因此,每次创建新的StudentQuery并更新另一个对象时-它将运行您的监听器来发送电子邮件。