我似乎无法让我的视图返回一个HttpResponse对象。我根据属性choice
的值调用了def。
这是我的views.py代码:
def assign_attribute_value(request, id_item_type):
"""
"""
def integer_attr(item_type):
form = forms.AssignIntegerTypeForm()
if request.method == "POST":
form = forms.AssignIntegerTypeForm(request.POST)
if form.is_valid():
attr_int = form.cleaned_data['attr_int']
item_type.attr_int = attr_int
item_type.save()
return HttpResponseRedirect('/des/list_item_types/')
else:
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def string_attr(item_type):
form = forms.AssignStringTypeForm()
if request.method == "POST":
form = forms.AssignStringTypeForm(request.POST)
if form.is_valid():
attr_str = form.cleaned_data['attr_str']
item_type.attr_str = attr_str
item_type.save()
return HttpResponseRedirect('/des/list_item_types/')
else:
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def boolean_attr(item_type):
form = forms.AssignBooleanTypeForm()
if request.method == "POST":
form = forms.AssignBooleanTypeForm(request.POST)
if form.is_valid():
attr_bool = form.cleaned_data['attr_bool']
item_type.attr_bool = attr_bool
item_type.save()
return HttpResponseRedirect('/des/list_item_types/')
else:
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def date_attr(item_type):
form = forms.AssignDateTypeForm()
if request.method == "POST":
form = forms.AssignDateTypeForm(request.POST)
if form.is_valid():
attr_date = form.cleaned_data['attr_date']
item_type.attr_date = attr_date
item_type.save()
return HttpResponseRedirect('/des/list_item_types/')
else:
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
ctx = {'form':form}
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
type_func = {0: integer_attr,
1: string_attr,
2: boolean_attr,
3: date_attr,
}
item_type = ItemType.objects.get(id=id_item_type)
type_func[item_type.choice](item_type=item_type)
答案 0 :(得分:0)
您的代码:
def assign_attribute_value(request, id_item_type):
def integer_attr(item_type):
...
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def string_attr(item_type):
...
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def boolean_attr(item_type):
...
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
def date_attr(item_type):
...
return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))
type_func = {0: integer_attr,
1: string_attr,
2: boolean_attr,
3: date_attr,
}
item_type = ItemType.objects.get(id=id_item_type)
type_func[item_type.choice](item_type=item_type)
您定义了几个回调,并且您正在调用它们,但是您没有从视图返回由任何回调返回的呈现响应。这样做:
def assign_attribute_value(request, id_item_type):
...
return type_func[item_type.choice](item_type=item_type)
顺便说一句,我建议你把这些回调放在模块级别,以避免在每次视图调用时编译它们。或者在单独的方法中使用基于类的视图和那些回调。