[英]how can i add objects from my model to my CBV in django?
i am trying to add objects from my model into my CBV, i have a model created so i am trying to pass them to my views, but its not working,tho i have 2 classes created which are Pricing and Pricingservice, i want an inline for a field in Pricingservice which i did, but i dont know how to add both to my view in CBV in the view so as to access them in my template, here is my code below, kindly help me out because i have been battling with this for days, thanks我正在尝试将 model 中的对象添加到我的 CBV 中,我创建了一个 model,所以我试图将它们传递给我的视图,但它不起作用,尽管我创建了 2 个类,它们是定价和定价服务,我想要一个内联对于我所做的 Pricingservice 中的一个字段,但我不知道如何将两者都添加到视图中的 CBV 视图中以便在我的模板中访问它们,下面是我的代码,请帮助我,因为我一直在与这几天,谢谢
class Pricing(models.Model):
plans = models.CharField(max_length=250)
amount = models.CharField(max_length=250)
services = models.CharField(max_length=250)
def __str__(self):
return self.plans
class Pricingservice(models.Model):
pricing = models.ForeignKey(Pricing, default=None, on_delete=models.CASCADE)
services = models.CharField(max_length=250)
def __str__(self):
return self.pricing.plans
class PricingserviceAdmin(admin.StackedInline):
model = Pricingservice
@admin.register(Pricing)
class PricingAdmin(admin.ModelAdmin):
inlines = [PricingserviceAdmin]
class Meta:
model = Pricing
class HomePageView(TemplateView):
template_name = 'pages/home.html'
def get_context_data(self, **kwargs):
context = super(HomePageView, self).get_context_data(**kwargs)
context['Pricing'] = Pricing.objects.all()
return context
<div class="row">
{% for p in Pricing %}
<div class="col-md-3 ftco-animate">
<div class="pricing-entry pb-5 text-center">
<div>
<h3 class="mb-4">{{ p.plans }}</h3>
<p><span class="price">#{{ p.amount }}</span> <span class="per">/ Month</span></p>
</div>
<ul>
<li>{{ p.services }}</li>
</ul>
<p class="button text-center"><a href="#" class="btn btn-primary px-4 py-3">Get Offer</a></p>
</div>
</div>
{% endfor %}
</div>
It would be better to use a ListView
.最好使用ListView
。 You would need to tell Django what the relevant model for your view is, ie:您需要告诉 Django 与您的观点相关的 model 是什么,即:
class HomePageView(ListView):
model = Pricingservice
template_name = 'pages/home.html'
And then in your template, you can use:然后在您的模板中,您可以使用:
{% for pricingservice in pricingservice_list %}
{{ pricingservice.pricing }}
{% endfor %}
I am not sure exactly how your two models differ, but maybe it would be better to merge them?我不确定你的两个模型到底有什么不同,但也许合并它们会更好?
Try this solution:试试这个解决方案:
class HomePageView(ListView):
model = Pricing
template_name = 'pages/home.html'
And then try this template:然后试试这个模板:
{% for pricing in object_list %}
{{pricing.plans}}<br>
{% for service in pricing.pricingservice_set.all %}
{{ service.services }}<br>
{% endfor %}
{% endfor %}
Please add some styling yourself.请自己添加一些样式。 :) :)
try this尝试这个
class HomePageView(TemplateView):
template_name = 'pages/home.html'
def get_context_data(self, **kwargs):
context = super(HomePageView, self).get_context_data(**kwargs)
context['Pricing'] = Pricing.objects.all()
return context
class PricePageView(ListView):
model = Pricingservice
template_name = 'pages/home.html'
path('', HomePageView.as_view(), name='home'),
path('price/', PricePageView.as_view(), name='price'),
{% for pricingservice in Pricingservice_list %}
<ul>
<li>{{ pricingservice }}</li>
</ul>
{% endfor %}
am not sure this will work but try this if that answer does not work for you我不确定这是否可行,但如果该答案对您不起作用,请尝试此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.