Beispiel #1
0
 def post(self, request):
     form = AuthorForm(request.POST)
     if form.is_valid():
         form.save()
         res = {'code': 0, 'result': '添加作者成功'}
     else:
         res = {'code': 1, 'errmsg': form.errors}
     return JsonResponse(res, safe=True)
Beispiel #2
0
 def post(self, request, *args, **kwargs):
     pk = kwargs.get('pk')
     p = self.model.objects.get(pk=pk)
     form = AuthorForm(request.POST, instance=p)
     if form.is_valid():
         form.save()
         res = {"code": 0, "result": "success", 'next_url': self.next_url}
     else:
         res = {"code": 1, "errmsg": form.errors, 'next_url': self.next_url}
     return render(request, settings.JUMP_PAGE, res)
Beispiel #3
0
 def post(self, request):
     form = AuthorForm(request.POST)
     if form.is_valid():
         form.save()
         res = {'code': 0, 'result': '添加作者成功'}
     else:
         # form.errors会把验证不通过的信息以对象的形式传到前端,前端直接渲染即可
         res = {'code': 1, 'errmsg': form.errors}
         print form.errors
     return JsonResponse(res, safe=True)
Beispiel #4
0
def newauthor(request):
    if request.POST:
        author = Author()
        aauthor = AuthorForm(request.POST, instance = author)
        
        if aauthor.is_valid():
            aauthor.save()
            return render_to_response('newauthor.html',
                    {
                        'aauthor': aauthor,
                        'msg': u'作者添加成功',
                    })
        else:
            aauthor = AuthorForm()
            return render_to_response('newauthor.html', {'aauthor': aauthor,'msg': u'作者添加失败,请检查信息是否正确'})
    else:
        aauthor = AuthorForm()
        return render_to_response('newauthor.html', {'aauthor': aauthor,})
Beispiel #5
0
def add_author(request):
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            new_author = form.save()
            return HttpResponse("{author} is added in our database!".format(author=new_author))
    else:
        form = AuthorForm()
        
    return render (
        request,
        'add-author.html',
        {'form': form}
    )