简体   繁体   English

在Django中从模型创建下拉列表

[英]Creating Dropdown from Model in Django

I am looking to create a dropdown in a template where the values of the dropdown come from a field (reference) within my Orders model in models.py . 我希望在模板中创建一个下拉列表,其中下拉列表的值来自models.py Orders模型中的字段(引用)。 I understand creating a dropdown where the values are set statically, but since I am looking to populate with values stored in the DB, I'm unsure of where to start. 我理解创建一个静态设置值的下拉列表,但由于我希望填充存储在数据库中的值,我不确定从哪里开始。

I've created the model and attempted playing around with views.py , forms.py and templates. 我创建了模型并尝试使用views.pyforms.py和templates。 I am able to get each of the order numbers to display but not in a dropdown and I am struggling with how to write my template. 我能够显示每个订单号但不在下拉列表中,我正在努力解决如何编写模板的问题。

models.py

from django.db import models

class Orders(models.Model): 

    reference = models.CharField(max_length=50, blank=False)
    ultimate_consignee = models.CharField(max_length=500)
    ship_to = models.CharField(max_length=500)

def _str_(self):
    return self.reference

forms.py

from django import forms
from .models import *

def references():
    list_of_references = []
    querySet = Orders.objects.all()
    for orders in querySet:
        list_of_references.append(orders.reference)
    return list_of_references

    class DropDownMenuReferences(forms.Form):

        reference = forms.ChoiceField(choices=[(x) for x in references()])

views.py

def reference_view(request):
    if request.method == "POST":
        form = references(request.POST)

        if form.is_valid():
            form.save()
            return redirect('index')

    else:
        form = references()
        return render(request, 'proforma_select.html', {'form': form})

proforma_select.html

{% extends 'base.html' %}

{% block body %}

  <div class="container">
    <form method="POST">

      <br>

      {% for field in form %}
      <div class="form-group row">
        <label for="id_{{ field.name }}" class="col-2 col-form-label"> {{ field.label }}</label>
        <div class="col-10">
          {{ field }}

        </div>
      </div>
      {% endfor %}

      <button type="submit" class="btn btn-primary" name="button">Add Order</button>
    </form>
  </div>

{% endblock %}

All I get when I render the template is each of the reference #s listed out but NOT within a dropdown. 我在渲染模板时获得的是每个引用的引用#,但不在下拉列表中。 This leads me to believe my problem is mainly with the template, but I'm unsure as I am new to using Django. 这让我相信我的问题主要是模板,但我不确定我是否是使用Django的新手。

Are you using Materialize CSS? 你在使用Materialise CSS吗? If yes, then Django forms renders dropdowns differently from how Materialize expects them. 如果是,则Django表单呈现的下拉菜单与Materialize预期的不同。 So you will want to override the form widget. 因此,您需要覆盖表单小部件。 You can do something like so: 你可以这样做:

forms.py: forms.py:

class DropDownMenuReferences(forms.Form):
     reference = forms.ChoiceField(choices=[(x) for x in references()],
     widget=forms.Select(choices=[(x) for x in references()], attrs={'class': 
     'browser-default'}))

This overrides the parameters passed into html. 这会覆盖传递给html的参数。 You can also pass any name tags in the attrs too. 您也可以在attrs中传递任何名称标签。

The issue: https://github.com/Dogfalo/materialize/issues/4904 问题: https//github.com/Dogfalo/materialize/issues/4904

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM