【问题标题】:Django how to retrieve values from a database for an existing post so that we can editDjango 如何从数据库中检索现有帖子的值以便我们可以编辑
【发布时间】:2021-04-29 06:21:01
【问题描述】:

我正在显示一个已保存到数据库的发布表单。我让用户可以选择查看数据库中的值,并让他可以选择编辑它们。但是,它不适用于下拉菜单。我试过写一些东西,但它没有显示正确的值。

请帮忙:(((

这里是sn-p的代码:

        <div class="form-group col-md-6">
                <label for="industry_type">Industry Type</label>
                <select class="form-control" id="industry_type" name="industry_type" selected=" 
                {{ internship.industry_type }}">
                    {% for ind_type in industry_types %}
                         <option value="{{ ind_type }}" {% if '{{ ind_type }}' == '{{ 
                         internship.industry_type }}' %}selected="selected" {% endif %}> 
                         {{ind_type}}</option>
                    {% endfor %}
                </select>            
       </div>

views.py

def edit_internship(request, pid):
  internship = Internship.objects.get(id=pid)
  skills = InternshipSkill.objects.filter(internship=internship)
  emp_steps = InternshipEmpStep.objects.filter(internship=internship)
 .order_by('emp_step_no')

  industry_types = IndustryType.objects.all()
  context = {
        'industry_types': industry_types, 
        'internship': internship,
        'skills': skills,
        'emp_steps': emp_steps,
    } 
  return render(request, 'edit_internship.html', context)

【问题讨论】:

  • 嗨 :) 您可以编辑您的问题以提供您的视图的代码(在 views.py 中)吗?很难说出您将哪些信息传递给模板。哪个位没有显示?
  • @TomHamiltonStubber 你好 :) 已发布。实习的行业类型可以是信息技术、银行等。我想在下拉列表中显示从所有类型中选择的行业类型

标签: python html django django-models django-views


【解决方案1】:

好吧,这只是因为如果你已经有花括号,不需要在 django 的模板中重新添加花括号:

<div class="form-group col-md-6">
  <label for="industry_type">Industry Type</label>
  <select class="form-control" id="industry_type" name="industry_type" selected="{{ internship.industry_type.pk }}">
    {% for ind_type in industry_types %}
       <option value="{{ ind_type.pk }}" {% if ind_type.pk == internship.industry_type.pk %}selected="selected" {% endif %}> 
         {{ ind_type }}
       </option>
    {% endfor %}
  </select>
</div>

另外,在声明值时不要忘记 ind_type.pk 参数。


为了将来参考,我真的建议你在创建这些表单时使用 Django 的表单,它会让你的喜欢变得更容易:

views.py


class EditInternship(UpdateView):
    model = Internship
    template_name = 'edit_internship.html'
    form_class = EditInternshipForm

    def get_context_data(**kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx.update(
            skills=InternshipSkill.objects.filter(internship=internship),
            emp_steps=InternshipEmpStep.objects.filter(internship=internship).order_by('emp_step_no')
        )
        # You'll also have `form` already in the context
        return ctx


edit_internship = EditInternship.as_view()

forms.py


class EditInternshipForm(forms.ModelForm):
    class Meta:
        model = Internship
        fields = 'industry_type', 'other_fields_on_the_model_you_want_to_edit'

edit_internship.html

<form method="post" enctype="multipart/form-data">
  {% csrf_token %}
  {{ form.industry_type }}
  {{ form.other_fields_you_want_to_edit }}
   <input type="submit" name="submit_button" value="Save" class="btn btn-primary btn-sm" id="submit_button">
</form>

【讨论】:

  • 选项是否有任何值呈现?还是没有呈现选项?
  • @TonyHamiltonStubber,是的,但它为所有实习显示相同的选项,即使他们有不同的行业类型
  • 既然如此,下拉列表中的选项对于每个实习都是一样的?或者selected 选项对于每个实习都是一样的?我刚刚看到我应该输入{% if ind_type.pk == internship.industry_type.pk %}selected="{{ internship.industry_type.pk }}",我会改变我的答案
  • 选择的选项对所有人都是一样的。顺便说一句,我还没有建立从实习.industry_type 到行业类型的外键关系:/ 这个解决方案也不起作用
  • 哦,如果不是 ForeignKey 关系,那是什么字段实习.industry_type?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多