我想实现一个电子邮件验证,我的工作方式似乎“hacky”,但似乎工作得很好。我在函数的返回部分创建了一个自定义响应,但是我一直收到以下错误:
The response content must be rendered before it can be iterated over.
该过程是有人注册的标准,当我去保存用户模型时,我有一个send_mail()
函数,用于发送带有验证密钥的电子邮件。用户单击该链接并传递密钥,如下所示:
/api/account/verify/849c40665175e56709855cc7aec2b16c05a4d977b3b083790334c6bc01f6e522
在视图中,您可以看到我拉出密钥的位置并处理所有内容。我猜这个错误是因为我在Response()
中使用get_queryset()
,但我不确定。如果没有,我该如何创建自定义响应消息?
版本
Python==2.7.10
Django==1.11.4
djangorestframework==3.6.3
模型
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), null=False, unique=True)
first_name = models.TextField(_('first name'), null=False)
last_name = models.TextField(_('last name'), null=False)
is_active = models.BooleanField(_('active'), default=False)
is_admin = models.BooleanField(_('admin'), default=False)
created_on = models.DateTimeField(_('create on'), auto_now_add=True)
updated_on = models.DateTimeField(_('updated on'), auto_now=True)
is_staff = models.BooleanField(_('staff'), default=False)
activation_key = models.CharField(_('email validation key'), default='', max_length=256)
查看
class ActivateViewSet(generics.ListAPIView):
queryset = User.objects
serializer_class = ActivationSerializer
def get_queryset(self):
activation_key = self.kwargs['activation_key']
if User.objects.all().filter(activation_key=activation_key).exists():
User.objects.all().filter(activation_key=activation_key).update(is_active=True)
return Response({'message': 'key accepted'}, status=status.HTTP_204_NO_CONTENT)
raise NotFound('activation key not found')
串行
class ActivationSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email',)
回溯
内部文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/exception.py” 41. response = get_response(request)
_get_response中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/base.py” 187. response = self.process_exception_by_middleware(e,request)
_get_response中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/base.py” 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
在wrapped_view中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/views/decorators/csrf.py” 58. return view_func(* args,** kwargs)
文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/views/generic/base.py”在视图中 68.返回self.dispatch(request,* args,** kwargs)
发送文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py” 489. response = self.handle_exception(exc) handle_exception中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py” 449. self.raise_uncaught_exception(exc)
发送文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py” 486. response = handler(request,* args,** kwargs)文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/generics.py”获取 201. return self.list(request,* args,** kwargs)
文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/mixins.py”的清单 48.返回Response(serializer.data)
数据中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py” 739. ret = super(ListSerializer,self).data
数据中的文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py” 263. self._data = self.to_representation(self.instance)
文件“/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py”in_representation 657. self.child.to_representation(item)for iterable中的项目
iter 中的文件“/Users/*y/.virtualenvs/video_env/lib/python2.7/site-packages/django/template/response.py” 121.“响应内容必须在可以迭代之前呈现。”
异常类型:ContentNotRenderedError at / api / account / activate / 849c40665175e56709855cc7aec2b16c05a4d977b3b083790334c6bc01f6e522 / 异常值:响应内容必须在迭代之前呈现。
答案 0 :(得分:4)
get_queryset
要求返回类型为queryset
而非Response
对象。您需要将代码更改为
def get_queryset(self):
activation_key = self.kwargs['activation_key']
if User.objects.all().filter(activation_key=activation_key).exists():
users = User.objects.all().filter(activation_key=activation_key)
.update(is_active=True)
return user
现在,由于您要发送自定义回复,因此无法在get_queryset
中完成。您必须覆盖序列化程序的创建方法。以下是如何覆盖此内容的一般示例。
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if not serializer.is_valid(raise_exception=False):
return Response({"Fail": "blablal", status=status.HTTP_400_BAD_REQUEST)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response({"Success": "msb blablabla"}, status=status.HTTP_201_CREATED, headers=headers)