Django将POST数据从一个视图传递到另一个视图

2024-09-30 06:25:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将从表单中收集的数据传递到另一个页面上。我是Django的初学者,仍然不理解POST-and-GET。在

过去几天我一直在努力解决这个问题,但我一直在绕圈子。我想这是一个相当简单的问题,但我不知道发生了什么。在

我希望在我的selectinterest视图中,一旦单击submit,就会将所选兴趣发送到另一个名为selectdiscipline的视图。我希望能够在我的select_discipline视图中使用所选兴趣。在

在视图.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 )

在选择Interest.html在

^{pr2}$

Tags: 视图objectsrequesthtmlcontextpostselect兴趣
1条回答
网友
1楼 · 发布于 2024-09-30 06:25:26

Note: I think the problem is that you did not define 'name' attribute in the select tag. 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

在选择Interest.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> 

在视图.py在

^{pr2}$

相关问题 更多 >

    热门问题