简体   繁体   English

无法使动态 Django 依赖下拉列表

[英]Unable to make Dynamic Django dependent dropdown

I am new to Django and I am struggling to make a dynamic Django dependent select dropdown for 'Categories', and I have been making a CRUD with Products having categories ,sub categories ,colors ,size below is the code for my Products model:我是 Django 的新手,我正在努力为“类别”制作一个动态的 Django 相关选择下拉列表,我一直在使用具有类别、子类别、颜色、大小的产品制作 CRUD,下面是我的产品模型的代码:

from tkinter import CASCADE
from django.db import models
from rest_framework import serializers
# Create your models here.
CATEGORY_CHOICES = [('ninesixwear','9-6WEAR'),('desiswag','DESI SWAG'),('fusionwear','FUSION WEAR'),
                    ('bridalwear','BRIDAL WEAR')]
class Products(models.Model):
    Categories = serializers.ChoiceField(choices = CATEGORY_CHOICES)
    sub_categories = models.CharField(max_length=15)
    Colors = models.CharField(max_length=15)
    Size = models.CharField(max_length=15)
    image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
    title = models.CharField(max_length=50)
    price = models.CharField(max_length=10)
    sku_number = models.CharField(max_length=10)
    prod_details = models.CharField(max_length=300)
    quantity = models.IntegerField(default=0)
    isactive = models.BooleanField(default=True)

model file create Category field模型文件创建类别字段

class Category (models.Model):
     name = models.CharField(max_length=200)
     slug = models.SlugField(max_length=255, unique=True)
   
     def __str__(self):
         return self.name
     class Meta:
         verbose_name_plural= 'Categories' 

viwe file query all category from database viwe 文件从数据库中查询所有类别

def category(request):
    categories = Category.objects.all()

html file code html文件代码

       <div class="col-md-12">
            <label>Category</label>
            <select name="category" class="form-control">
                <option value="">select category</option>
                {% for category in categories %}
                <option value="{{category.id}}">{{category.name}}</option>
                {% endfor %}
            </select>
        </div>

If you still don't understand then check this repo Github如果你仍然不明白,请查看这个 repo Github

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

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