第一次发布海报。我正在尝试使用Django Rest Framework创建一个简单的联系选项。联系页面允许用户和非用户通过表单向站点管理员发送电子邮件。已经有好几个星期......我在下面添加了我的问题和代码。
1)视图集是否需要一些额外的工作才能连接到表单数据?
2)DRF API查看器是否允许对此进行测试?它应该显示电子邮件字段吗?
# serializers.py
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
message = serializers.CharField()
name = serializers.CharField()
# views.py
class CommentViewSet(viewsets.ViewSet):
def list(self, request): #, format=None
comment = CommentSerializer(data=request.data)
if comment.is_valid():
form_email = comment.data['email']
form_message = comment.data['message'] + "email: " + form_email
form_name = comment.data['name']
send_mail("New contact form submission",
form_message,
form_email,
['myemailaddress@gmail.com'],
fail_silently=False
)
return Response(comment.data)
# Not sure how the html connects here:
# return render('comment.html', {
# 'form': form_class,
# })
return Response(
{
"success": False,
'error-code':'invalid-data'
},
)
# urls.py
router = DefaultRouter()
router.register(r'profiles', views.ProfileViewSet)
router.register(r'users', views.UserViewSet)
router.register(r'comment', views.CommentViewSet, 'Comment')
urlpatterns = [
url(r'^', include(router.urls)),
]