简体   繁体   中英

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 . 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. 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.

Are you using Materialize CSS? If yes, then Django forms renders dropdowns differently from how Materialize expects them. So you will want to override the form widget. You can do something like so:

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. You can also pass any name tags in the attrs too.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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