我如何在 django 中用其他模型制作的表单中使用来自其他模型的数据?

How do i use Data from other model inside forms that is made with other model in django?

现在我的模板看起来像这样

我希望将这些卡片用作右侧表格中的选项。我是新手,所以我不知道怎么做。这是否可能,以便我可以以使用我的订单模型构建的形式呈现计划模型中的数据。在订单模型中,我使用计划作为外键

Forms.py



class BuyPlanForm(forms.ModelForm):
    error_css_class = 'error-field'
    required_css_class = 'required-field'
    pack = forms.ModelChoiceField(Plans.objects.all(), widget=forms.RadioSelect())

    class Meta():
        model = Orders
        fields = ['pack']

我的Views.py

class UserBuyPlan(LoginRequiredMixin, View):
    template = 'plans/plan.html'
    success_url = reverse_lazy('home-home')

    def get(self, request):
        plans = Plans.objects.all()
        form = BuyPlanForm()
        
        ctx = {
            'form': form,
            'plans': plans,
        }
        return render(request, self.template, ctx)

    def post(self, request):
        form = BuyPlanForm(request.POST)
        form.instance.user = self.request.user
        if not form.is_valid():
            ctx = {'form': form}
            return render(request, self.template, ctx)

        make = form.save()
        return redirect(self.success_url)

我的models.py


class Plans(models.Model):
    plan_name = models.CharField(max_length=50)
    speed = models.IntegerField()
    price = models.FloatField()

    def __str__(self):
        return self.plan_name

def get_deadline():
    return dt.today() + timedelta(days=30)


class Orders(models.Model):
    user = models.ForeignKey(CustomUser, primary_key=True, on_delete = models.CASCADE)
    pack = models.ForeignKey(Plans, on_delete = models.CASCADE)
    start_date = models.DateField(auto_now_add=True)
    end_date = models.DateField(default=get_deadline())
    is_active = models.BooleanField(default=True)

    def __str__(self):
        name = str(self.user.username)
        return name

    def get_absolute_url(self):
        return reverse('home-home')

我的html模板

{% extends "home/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <h1>Select a Plan That Suits You</h1>
        <hr>

        <div class="cards">
                {% for plan in plans %}
                
                    <div class="card">
                        <div class="upper">
                            <span class="radio">{{ plan.plan_name }}</span> 
                        </div>
                        <div class="lower">
                            <h5>₹{{ plan.price }}</h5>
                            <div class="details">
                                <div class="item">
                                    <div class="item-1">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-wifi" viewbox="0 0 16 16">
                                            <path d="M15.384 6.115a.485.485 0 0 0-.047-.736A12.444 12.444 0 0 0 8 3C5.259 3 2.723 3.882.663 5.379a.485.485 0 0 0-.048.736.518.518 0 0 0 .668.05A11.448 11.448 0 0 1 8 4c2.507 0 4.827.802 6.716 2.164.205.148.49.13.668-.049z"/>
                                            <path d="M13.229 8.271a.482.482 0 0 0-.063-.745A9.455 9.455 0 0 0 8 6c-1.905 0-3.68.56-5.166 1.526a.48.48 0 0 0-.063.745.525.525 0 0 0 .652.065A8.46 8.46 0 0 1 8 7a8.46 8.46 0 0 1 4.576 1.336c.206.132.48.108.653-.065zm-2.183 2.183c.226-.226.185-.605-.1-.75A6.473 6.473 0 0 0 8 9c-1.06 0-2.062.254-2.946.704-.285.145-.326.524-.1.75l.015.015c.16.16.407.19.611.09A5.478 5.478 0 0 1 8 10c.868 0 1.69.201 2.42.56.203.1.45.07.61-.091l.016-.015zM9.06 12.44c.196-.196.198-.52-.04-.66A1.99 1.99 0 0 0 8 11.5a1.99 1.99 0 0 0-1.02.28c-.238.14-.236.464-.04.66l.706.706a.5.5 0 0 0 .707 0l.707-.707z"/>
                                        </svg>
                                        <p>Data</p>
                                        <p>Unlimited</p>
                                    </div>
                                </div>
                                <div class="item">
                                    <div class="item-1">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-speedometer" viewbox="0 0 16 16">
                                            <path d="M8 2a.5.5 0 0 1 .5.5V4a.5.5 0 0 1-1 0V2.5A.5.5 0 0 1 8 2zM3.732 3.732a.5.5 0 0 1 .707 0l.915.914a.5.5 0 1 1-.708.708l-.914-.915a.5.5 0 0 1 0-.707zM2 8a.5.5 0 0 1 .5-.5h1.586a.5.5 0 0 1 0 1H2.5A.5.5 0 0 1 2 8zm9.5 0a.5.5 0 0 1 .5-.5h1.5a.5.5 0 0 1 0 1H12a.5.5 0 0 1-.5-.5zm.754-4.246a.389.389 0 0 0-.527-.02L7.547 7.31A.91.91 0 1 0 8.85 8.569l3.434-4.297a.389.389 0 0 0-.029-.518z"/>
                                            <path fill-rule="evenodd" d="M6.664 15.889A8 8 0 1 1 9.336.11a8 8 0 0 1-2.672 15.78zm-4.665-4.283A11.945 11.945 0 0 1 8 10c2.186 0 4.236.585 6.001 1.606a7 7 0 1 0-12.002 0z"/>
                                        </svg>
                                        <p class="speed">Speed</p>
                                        <p>Upto {{ plan.speed }} Mbps</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                {% endfor %}


            
            
            <div class="content-section">
                <form action="" method="post">
                    {% csrf_token %}
                   
                    {% for choice in form.pack  %}
                        <span class="radio">{{ choice.tag }}</span> 
                        {{ choice.choice_label }}
                        <br>
                    {% endfor %}
                    <input class="btn btn-outline-info" type="submit" value="Sign Up">
                    
                    
                </form>
            </div>
        </div>
    </div>
    
{% endblock content %}

请帮忙。

我只需要使用 if 块来比较选择标签和包中的名称,然后在两者匹配时显示额外的详细信息。 我的plans.html模板

<form action="" method="post">
                    <div class="cards">
                        {% for choice in form.pack  %}
                            <div class="big-card">
                                <span class="radio">{{ choice.tag }}</span> 
                                <div class="card">
                                            <div class="upper">
                                        <h4>{{ choice.choice_label }}</h4>
                                        {% for plan in plans %}
                                        {% if plan.plan_name == choice.choice_label %}
                                            <h5>₹{{ plan.price }}</h5>
                                            </div>
                                    
                                    
                                            <div class="lower">
                                                <div class="details">
                                                    <div class="item">
                                                        <div class="item-1">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-wifi" viewbox="0 0 16 16">
                                                                <path d="M15.384 6.115a.485.485 0 0 0-.047-.736A12.444 12.444 0 0 0 8 3C5.259 3 2.723 3.882.663 5.379a.485.485 0 0 0-.048.736.518.518 0 0 0 .668.05A11.448 11.448 0 0 1 8 4c2.507 0 4.827.802 6.716 2.164.205.148.49.13.668-.049z"/>
                                                                <path d="M13.229 8.271a.482.482 0 0 0-.063-.745A9.455 9.455 0 0 0 8 6c-1.905 0-3.68.56-5.166 1.526a.48.48 0 0 0-.063.745.525.525 0 0 0 .652.065A8.46 8.46 0 0 1 8 7a8.46 8.46 0 0 1 4.576 1.336c.206.132.48.108.653-.065zm-2.183 2.183c.226-.226.185-.605-.1-.75A6.473 6.473 0 0 0 8 9c-1.06 0-2.062.254-2.946.704-.285.145-.326.524-.1.75l.015.015c.16.16.407.19.611.09A5.478 5.478 0 0 1 8 10c.868 0 1.69.201 2.42.56.203.1.45.07.61-.091l.016-.015zM9.06 12.44c.196-.196.198-.52-.04-.66A1.99 1.99 0 0 0 8 11.5a1.99 1.99 0 0 0-1.02.28c-.238.14-.236.464-.04.66l.706.706a.5.5 0 0 0 .707 0l.707-.707z"/>
                                                            </svg>
                                                            <p>Data</p>
                                                            <p>Unlimited</p>
                                                        </div>
                                                    </div>
                                                    <div class="item">
                                                        <div class="item-1">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-speedometer" viewbox="0 0 16 16">
                                                                <path d="M8 2a.5.5 0 0 1 .5.5V4a.5.5 0 0 1-1 0V2.5A.5.5 0 0 1 8 2zM3.732 3.732a.5.5 0 0 1 .707 0l.915.914a.5.5 0 1 1-.708.708l-.914-.915a.5.5 0 0 1 0-.707zM2 8a.5.5 0 0 1 .5-.5h1.586a.5.5 0 0 1 0 1H2.5A.5.5 0 0 1 2 8zm9.5 0a.5.5 0 0 1 .5-.5h1.5a.5.5 0 0 1 0 1H12a.5.5 0 0 1-.5-.5zm.754-4.246a.389.389 0 0 0-.527-.02L7.547 7.31A.91.91 0 1 0 8.85 8.569l3.434-4.297a.389.389 0 0 0-.029-.518z"/>
                                                                <path fill-rule="evenodd" d="M6.664 15.889A8 8 0 1 1 9.336.11a8 8 0 0 1-2.672 15.78zm-4.665-4.283A11.945 11.945 0 0 1 8 10c2.186 0 4.236.585 6.001 1.606a7 7 0 1 0-12.002 0z"/>
                                                            </svg>
                                                            <p class="speed">Speed</p>
                                                            <p>Upto {{ plan.speed }} Mbps</p>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            </div>
                        
                        <br>
                    {% endfor %}
                    </div>
                    {% csrf_token %}
                   
                    
                    <br>
                    <input style="display: block; width: 100px; height: 50px;" class="btn btn-outline-info" type="submit" value="Sign Up">
                </form>

现在我的模板像这样呈现并且可以工作了。