我有一个表单来编辑现有的一组模型。要发布表单,我尝试使用关键字参数将操作定向到网址,作为' id'的值。绑定形式的字段。但是我收到以下错误:
NoReverseMatch at /membership/edit/12
Reverse for 'members_edit' with arguments '()' and keyword arguments '{u'id': None}' not found. 1 pattern(s) tried: ['membership/edit/(?P<id>\\d{1,5})$']
这是我表单的相关html:
<form class="form-horizontal" role="form" action="{% url 'members_edit' id=edit_member_form.member_id.value %}" method="post"> {% csrf_token %}
<div class="row">
<!-- Static ID number -->
<div class="col-md-4 col-md-offset-4 form-control-static text-big">
<p><strong>ID #:</strong>  {{edit_member_form.member_id.value}}</p>
</div>
</div>
</form>
我知道表单中的值不是None
,因为当我在提交POST之前显示绑定表单时,包含{{edit_member_form.member_id.value}}
的行计算为整数(例如12)并正确显示
我是django的新手,但为什么edit_member_form.member_id.value
会在None
标记中评估为url
,但在显示时会评估绑定到表单的数字?
如果有更好的技术可以达到同样的效果,我会非常乐意听到它。我真的只想将表单发回到显示的相同网址,关键字完整无缺。
提前感谢您的任何帮助或建议。
修改:
def members_edit(request, id="1"):
context = { 'active':'members' }
context['filter_list'] = Member.objects.all().order_by('last_name')
id=int(id)
print 'callec'
# if form was submitted
if (request.method == 'POST'):
edit_member_form = EditMemberForm(request.POST)
if edit_member_form.is_valid(): #all validation rules pass
print 'called'
edit_member_and_pass( edit_member_form.cleaned_data )
member_id = edit_member_form.cleaned_data['member_id']
return redirect('members_view', id=member_id)
else:
context['edit_member_form'] = edit_member_form
#return render(request, 'members/members_edit.html', context)
# else display an initial form
else:
member_set = Member.objects.filter(pk=id)
# if we find the member
if ( len(member_set) == 1):
member = member_set[0]
member_dict = { k:v for k,v in member.__dict__.items() if k not in ['_state', 'photo']}
member_dict['member_id'] = id
# get only their passes for this season, that haven't been flagged lost/stolen
pass_set = member.pass_set.filter(season=get_current_season(), lost_stolen=False)
# if the member has one current season pass associated with him
if ( len(pass_set) == 1):
member_pass = pass_set[0]
member_dict['member_type'] = member_pass.member_type
member_dict['active_id'] = member_pass.active_id
edit_member_form = EditMemberForm( member_dict)
context['edit_member_form'] = edit_member_form
#return render(request, 'members/members_edit.html', context)
# make sure they don't have too many for some crazy reason
elif ( len(pass_set) > 1 ):
context['pass_error'] = True
context['pass_error_msg'] = 'Error: Member has too many active passes for this season.\nPlease have a director edit this record directly.'
#return render(request, 'members/members_edit.html', context)
# else the member hasn't been enrolled yet for this season
# so we want to reenroll them
else:
edit_member_form = EditMemberForm( member_dict )
context['edit_member_form'] = edit_member_form
#return render(request, 'members/members_edit.html', context)
# if for some crazy reason there are multiple members with this id
elif ( len(member_set) >1):
context['member_error'] = True
context['member_error_msg'] = 'Error: Multiple members with id ' + str(id) +'\nPlease have a director edit these records directly'
#return render(request, 'members/members_edit.html', context)
# else the member doesn't exist
else:
context['member_error'] = True
context['member_error_msg'] = "Error: Member doesn't exist.\nPlease sign them up as new"
#return render(request, 'members/members_edit.html', context)
return render(request, 'members/members_edit.html', context)
编辑2:问题似乎是edit_member_form.member_id
的值在提交时并未与表单字段保持绑定。我不知道为什么会发生这种情况。
如果我显示字段本身,则表单成功提交。 即,
<p><strong>ID #:</strong>  {{edit_member_form.member_id}}</p>
有效但
<p><strong>ID #:</strong>  {{edit_member_form.member_id.value}}</p>
没有按&#39;吨
如何在不可编辑的情况下显示字段值,但是值是否仍然绑定到表单?
编辑3:
以下是我urls.py
urlpatterns = patterns('members.views',
url(r'^membership/view/(?P<id>\d{1,5})$', 'members_view', name='members_view'), # view members page
url(r'^membership/edit/(?P<id>\d{1,5})$', 'members_edit', name='members_edit'), # edit members page
另外,在我自己尝试解决问题的过程中,我相信如果我可以编写标记以便显示值而且在提交时发布该值,则可以解决。
我认为问题就在于此<p><strong>ID #:</strong>  {{edit_member_form.member_id}}</p>
评估标记中的<input>
字段,该字段在提交表单时发布。由于仅显示该值不会评估为包含此输入标记,因此值不会发布到请求中。那么现在的问题是如何在没有<input>
字段的表单提交上将数据发布到请求中?我是否应该介绍一些像jQuery一样的hacky?
解 最后,解决方案是在包含要提交的值的字段中添加隐藏的输入。在显示字段
之后,它基本上只相当于这一个额外的行<p class="form-control-static" id="id_member_id">{{edit_member_form.member_id.value}}</p>
<input type="hidden" id="id_member_id" name="member_id" value="{{edit_member_form.member_id.value}}">
答案 0 :(得分:0)
我相信这可能就像删除&#34;动作一样简单。来自表单的属性。如果你这样做,表格将回复自己;即,/ membership /编辑/ 12 /上的表格将发布到/ membership /编辑/ 12 /.
这将消除您在页面上以任何形式打印身份证的任何需要。