【问题标题】:Django : Maintaining option selected in HTML templateDjango:维护在 HTML 模板中选择的选项
【发布时间】:2018-07-12 12:45:50
【问题描述】:

当我在 html 模板中使用 django if 函数来设置对所选选项标签的更改时,但它一直选择 --SELECT-- 选项

 <form  method="post">

<select name="name">
<option >--SELECT--</option>
  {% for job in all_pekerjaan%}
  <option value="{{job.id}}"
         {% if job.id == current_name %}selected="selected"{% endif %}>
         {{job.name}}
     </option>
    {% endfor %}
</select>
{% csrf_token %}

  <input type="submit" class="btn btn-success" value="submit" >
</form>

这是我的 views.py

from django.http import HttpResponse
from .models import daftartabel, Pekerjaan
from .forms import form1, form2
from django.template import loader
from django.shortcuts import render

def index(request):

    if request.method == 'POST':
        #print(request.POST['name'])
        current_name=request.POST['name']
        all_datas = daftartabel.objects.filter(pekerjaan_id=request.POST['name'])
    else:
        all_datas = daftartabel.objects.all()
    all_pekerjaan = Pekerjaan.objects.all()
    template = loader.get_template('tabel/index.html')
    #print(all_datas)
    #print(all_pekerjaan)
    context = {
        'all_datas' : all_datas,
        'all_pekerjaan' : all_pekerjaan,
        'current_name' : request.POST['name'],
    }
    #print (context)
    print(type(context['current_name']))

    return HttpResponse(template.render(context, request))

我的 forms.py 类

class form2(forms.Form):
    name = forms.CharField(max_length=100)

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: python html django django-templates html-select


    【解决方案1】:

    问题已解决,将 'current_name' 更改为整数会有所帮助。

    if request.method == 'POST':
            context = {
                'all_datas' : all_datas,
                'all_pekerjaan' : all_pekerjaan,
                'current_name' : int(request.POST['name']),
                }
        else:
                    context = {
                        'all_datas' : all_datas,
                        'all_pekerjaan' : all_pekerjaan,
                        }
    

    【讨论】:

      【解决方案2】:

      在条件之前声明变量。所以像这样的

      def index(request):
          current_name = 0 /*0 or None based on what kind of value it carries*/ 
          all_datas = None
          if request.method == 'POST':
              #print(request.POST['name'])
              current_name=request.POST['name']
              all_datas = daftartabel.objects.filter(pekerjaan_id=request.POST['name'])
          else:
              all_datas = daftartabel.objects.all()
      

      另外,去掉上下文对象赋值中的最后一个逗号

      context = {
              'all_datas' : all_datas,
              'all_pekerjaan' : all_pekerjaan,
              'current_name' : request.POST['name']
          }
      

      【讨论】:

      • 我使用的是 job.id 而不是 job.name,因为它更容易一些
      • 但是 current_name 包含什么值?
      • 我的帖子值,这意味着我的 job.id ,,,,但是它不起作用
      • 什么都没有改变...但感谢您为我寻找解决方案
      • 您是否也尝试删除为上下文对象赋值的多余逗号?
      猜你喜欢
      • 2012-01-13
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2014-05-07
      • 2012-04-17
      相关资源
      最近更新 更多