[英]Django - Pass a <select> form POST data from one view to another
I am trying to pass data that I collected from a form, onto another page. 我试图将我从表单收集的数据传递到另一页上。 I am a beginner at Django and still don't understand POST and GET. 我是Django的初学者,但仍然不了解POST和GET。
I've been trying to solve this issue for the past few days, but I keep running in circles. 过去几天,我一直在努力解决此问题,但我一直在圈子里奔波。 I am assuming this is a rather simple issue, but I just don't know what's going on. 我假设这是一个相当简单的问题,但我只是不知道发生了什么。
I want to have it that on my select interest view, once I click on submit will send the selected interest to another view called select discipline. 我希望在选择兴趣视图中单击“提交”后,会将选择的兴趣发送到另一个名为“选择学科”的视图。 I want to be able to use that selected interest in my select_discipline view. 我希望能够在我的select_discipline视图中使用所选兴趣。
views.py views.py
@login_required
def select_interest(request):
context = RequestContext(request)
interests = Interest.objects.order_by('name')
return render_to_response('selectinterest.html', {'interests': interests, 'disciplines': disciplines }, context )
@login_required
def select_discipline(request):
interest = request.POST.get('interest-select')
context = RequestContext(request)
interests = Interest.objects.order_by('name')
disciplines = Discipline.objects.filter(parentInterest = interest)
return render('selectdiscipline.html', {'interest': interest, 'interests': interests, 'disciplines': disciplines }, context )
selectinterest.html selectinterest.html
<div class = "container">
<h1>Change Primary Interest</h1>
<form action="{% url 'myapp:select_discipline' %}" method="POST" id="interest-select">
{% csrf_token %}
<select title="interest-select" id="usrInterest">
<option disabled selected> -- select an option -- </option>
{% for i in interests %}
<option name = "interest" value={{i.id}}>{{i.name}}</option>
{% endfor %}
</select> -->
<input type="submit" value="Load Disciplines"/>
</form>
</div>
Note: I think the problem is that you did not define 'name' attribute in the select tag. 注意:我认为问题在于您没有在select标签中定义'name'属性。 You should use attribute 'name' in the tag select, because, It is the name used the dictionary POST Also, I recommend using django-crispy-forms 您应该在标签select中使用属性“ name”,因为,这是字典POST所使用的名称。此外,我建议使用django-crispy-forms
selectinterest.html selectinterest.html
<div class="container">
<h1>Change Primary Interest</h1>
<form action="{% url 'myapp:select_discipline' %}" method="POST" id="interest-select">
{% csrf_token %}
<select id="id_interest" name="interest">
<option disabled selected> -- select an option -- </option>
{% for i in interests %}
<option value={{i.id}}>{{i.name}}</option>
{% endfor %}
</select>
<input type="submit" value="Load Disciplines"/>
</form>
</div>
views.py views.py
@login_required
def select_discipline(request):
if request.method == 'POST':
interest = request.POST.get('interest')
context = RequestContext(request)
interests = Interest.objects.order_by('name')
disciplines = Discipline.objects.filter(parentInterest=interest)
return render(
'selectdiscipline.html',
{
'interest': interest,
'interests': interests,
'disciplines': disciplines
},
context
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.