如何创建django下拉列表

n8ghc7c1  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(156)

我需要使用django在datatable字段中创建一个下拉列表,以显示所有国家,并允许用户选择一个并获得响应。我试过这个代码,但它不工作。

models.py

class Datatable(models.Model):
    COUNTRY_CHOICE = (
    ('MA', 'MA'),
    ('FR', 'FR'),
    ('US', 'US'),
)

    Title = models.CharField('Title',max_length=500),
    Price = models.DecimalField('Price',decimal_places = 3, max_digits = 10000),
    Currency = models.CharField('Currency',max_length=500),
    Stars = models.DecimalField('Stars',decimal_places = 3 , max_digits = 10000),
    Orders = models.PositiveIntegerField('Orders',max_length=500),
    Shipcost = models.CharField('Shipcost',max_length=500),
    Supplier = models.CharField('Supplier',max_length=500),
    Productlinks = models.CharField('Productlinks',max_length=700),
    Feedback = models.IntegerField('Feedback',max_length=700),
    Images = models.IntegerField('Images',max_length=700),
    Country = models.CharField(blank=True, choices=COUNTRY_CHOICE, max_length=100)

datatable.html

<th class="Country">
                    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Customer Country
                    </button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                      <a class="dropdown-item" href="#MA">MA</a>
                      <a class="dropdown-item" href="#FR">FR</a>
                      <a class="dropdown-item" href="#US">US</a>
                    </div>
                  </th>

views.py

from multiprocessing import context
from django.shortcuts import render   
import pymongo
from .models import *
from .serializers import *
from .forms import *
from .codes.scraping import scrap

def home_view(request):
    context = {}
    context ['form'] = Scraping()
    return render(request,'home.html', context)
def datatable_view(request):
    if request.method =='POST':
        form = Scraping(request.POST)
        if form.is_valid():
            subject=form.cleaned_data['subject']
            scrap(subject)
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    db= client["db3"]
    col = db["aliex3"]
    products = col.find()
    context = {'products' : products}
    return render(request,'datatable.html', context)

任何帮助将是伟大的。谢谢你!

rqenqsqc

rqenqsqc1#

你必须在你的国家之间循环:
首先将国家传递到您的上下文:

# your previous code...
products = col.find()
countries = Country.objects.all()

context = {}
context['products'] = products
context['countries'] = countries
return render(request,'datatable.html', context)

在你的html文件中:

<th class="Country">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Customer Country
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    {% for country in countries %}
      <a class="dropdown-item" href="#{{country}}">{{country}}</a>
    {% endfor %}
    </div>
</th>

相关问题