我在显示我的页面的输入框时遇到问题。在同一页面上,我希望有两个视图,一个访问和检索数据,另一个是表单。由于它是一个事件注册页面,第一个视图显示特定事件的详细信息,第二个视图是从事件本身中删除特定用户的视图。
我的观点.py
def ShowSpecificEvent(request, eventslug):
if request.method == 'POST':
form = RemovalForm(request.POST)
if form.is_valid():
event = Event.objects.get(slug=eventslug)
context = {'event': event,}
updated_event = event.signed_up.remove(for_removal = form.cleaned_data['for_removal'],)
updated_event.save()
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
event = Event.objects.get(slug=eventslug)
context = {'event': event,}
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
def Remove(request, eventslug):
if request.method == 'POST':
form = RemovalForm(request.POST)
if form.is_valid():
event = Event.objects.get(slug=eventslug)
context = {'event': event,}
updated_event = event.signed_up.remove(for_removal = form.cleaned_data['for_removal'],)
updated_event.save()
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
return HttpResponseRedirect('base_remove_user.html')
我的模板
{% block content %}
event details...
{% if user.is_authenticated %}
{% if event.sign_up_is_live %}
<p><a href= "{% url eventattend event.slug %}"> Sign me up!</a></p>
<form action='' method='post'>
{% csrf_token %}
{% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
<div class='register_div'>
{% if form.for_removal.errors %} <p class='error'>{{ form.for_removal.errors }}</p>{% endif %}
<p><label for='for_removal'{% if form.for_removal.errors %}class='error'{% endif %}>User to be removed:</label></p>
<p>{{ form.for_removal }}</p>
</div>
<p><input type='submit'></p>
</form>
{% endif %}{% endif %}
{% endblock %}
我读过我认为最相关的这篇文章,但规定的方法并没有真正帮助。
我也考虑过拆分这两个页面,但我将无法在删除用户页面中检索 eventslug。
提前致谢 :)
更新:
gutrt 的回答很有帮助,我设法通过将 ShowSpecificEvent 更改为以下内容来显示输入框:
def ShowSpecificEvent(request, eventslug):
event = Event.objects.get(slug=eventslug)
form = RemovalForm(request.POST or None)
context = {'event': event, 'form': form,}
if request.method == 'POST':
if form.is_valid():
updated_event = event.signed_up.remove(request.POST.get('for_removal'))
updated_event.save()
return HttpResponseRedirect('base_user_removed.html')
else:
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
但是,在提交表单后,我得到一个 ValueError(invalid literal for int() with base 10: 'a_technicolor_skye') 其中 a_technicolor_skye 是我要删除的用户。有没有人有任何想法?顺便说一句,event.signed_up 是多对多字段,我已将其设置为 null=True 和 blank=True。