django中view.py中的语法无效。?

时间:2015-08-13 08:42:23

标签: django django-models django-forms django-views

我在Django中遇到错误:语法无效(views.py)。

def deals(request):
    form = deals()
    if request.method == "POST":
        form = deals(request.POST, request.FILES)
    if form.is_valid():
        form.save()
    else:
        return render_to_response("deals.html", {'form':form}, context_instance=RequestContext(request))
    else:
    form = deals()
    return render_to_response("deals.html", {'form':form}, context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:3)

问题实际上是缩进问题,表现为语法错误。从angular.module('angular-country-select', []) .directive('countrySelect', ['$timeout', function($timeout) { return { restrict: 'A', require:'ngModel', link: function(scope, elem, attrs, ngModelCtrl) { console.log(attrs, attrs.placeholder); var data = [{"id":"AF","text":"Afghanistan"},{"id":"AX","text":"Åland Islands"},...]; var select2Inst = elem.select2({ data: data }); $timeout(function() { if (attrs.value != '') { ngModelCtrl.$setViewValue(attrs.value); scope.$apply(select2Inst.select2('val', attrs.value)); } }, 2000); } }; }]); 开始的四行应缩进一级。

然而,这仍然不是推荐的模式。您根本不需要内部if form.is_valid(),并且必须在成功发布后重定向。

else

此外,我还使用了def deals(request): form = DealsForm() if request.method == "POST": form = DealsForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('/') # or wherever else: form = DealsForm() return render(request "deals.html", {'form':form}) 快捷方式而不是render,因为它会自动使用RequestContext。

请注意,所有这些都在文档中明确给出;没有理由做任何其他事情。