我的 for 循环没有按预期工作 - 数据没有显示在我的 django 模板中

My for loop does not work as expected - Data does not show up in my django template

我试图在我的 Django 模板中使用 for 循环来显示存储在 table 模型中的数据,但由于某种原因,数据没有显示在模板中。

Views.py

def add_part(request):
    parts = Parts.objects.all()
    context = {
        "parts": parts
    }
    return render(request, 'admintemplate/add_parts_template.html', context)



def add_part_save(request):
    if request.method != "POST":
        messages.error(request, "Method Not Allowed!")
        return redirect('add_part')
    else:
        part_name = request.POST.get('part_name')
        part_type = request.POST.get('part_type')
        supplier_id = request.POST.get('suppliers')
        suppliers = Suppliers.objects.get(id=supplier_id)
        

        try:
            part = Parts(part_name=part_name, part_type=part_type, supplier_id=supplier)
            part.save()
            messages.success(request, "Part Added Successfully!")
            return redirect('add_part')
        except:
            messages.error(request, "Failed to Add Part!")
            return redirect('add_part')

models.py

零件和服务模型完全相同,只是列名不同,所以我认为两者的功能应该是相同的。

Suppliers models

class Suppliers(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)

Parts model

class Parts(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    part_type = models.CharField(max_length=20)
    supplier_id = models.ForeignKey(Suppliers, on_delete=models.CASCADE)

Services model

class Services(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    service_type = models.CharField(max_length=20)
    supplier_id = models.ForeignKey(Suppliers, on_delete=models.CASCADE)

零件模板

{% extends 'admintemplate/base_template.html' %}

{% block page_title %}
    Add Parts
{% endblock page_title %}

{% block main_content %}

{% load static %}

<section class="content">
        <div class="container-fluid">

            <div class="row">
                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="card card-primary">
                    <div class="card-header">
                        <h3 class="card-title">Add Parts</h3>
                    </div>
                    <!-- /.card-header -->
                    <!-- form start -->
                    <form role="form" method="POST" action="{% url 'add_part_save' %}">
                        {% csrf_token %}

                        
                                {% comment %} Display Messages {% endcomment %}
                                {% if messages %}
                                <div class="form-group">
                                <div class="col-12">
                                    {% for message in messages %}
                                    {% if message.tags == "error" %}
                                        <div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin-top: 10px;">
                                        {{ message }}
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                        </div>
                                    {% elif message.tags == "success" %}
                                        <div class="alert alert-success alert-dismissible fade show" role="alert" style="margin-top: 10px;">
                                        {{ message }}
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                        </div>
                                    {% endif %}
                                    {% endfor %}
                                </div>
                                </div>
                                {% endif %}
                            

                        <div class="card-body">
                            <div class="form-group">
                                <label>Part Name </label>
                                <input type="text" class="form-control" name="part_name" placeholder="Part Name">
                            </div>
                            
                            <div class="form-group">
                                <label>Part Type </label>
                                <input type="text" class="form-control" name="part_type" placeholder="Part Type">
                            </div>

                            <div class="form-group">
                                <label>Supplier Name</label>
                                <select class="form-control" name="suppliers">
                                    {% for supplier in suppliers %}
                                        <option value="{{ supplier.id }}">{{ supplier.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>

                        </div>
                        <!-- /.card-body -->

                        <div class="card-footer">
                        <button type="submit" class="btn btn-primary">Add Part</button>
                        </div>
                    </form>
                    </div>
                    <!-- /.card -->

                </div>
            </div>

        </div><!-- /.container-fluid -->
      </section>

  {% endblock main_content %}

现在零件模板中的服务根本不显示。表格上没有选择。但是,对于添加服务模板,它确实会填充。我不知道为什么会这样,因为我对两个模板使用了完全相同的代码。

将视图更改为此解决了问题

def add_part(request):
    parts = Parts.objects.all()
    context = {
        "suppliers": suppliers
    }
    return render(request, 'admintemplate/add_parts_template.html', context)