简体   繁体   English

如何将 model 中的对象添加到 django 中的 CBV?

[英]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 视图中以便在我的模板中访问它们,下面是我的代码,请帮助我,因为我一直在与这几天,谢谢

model.py model.py

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

admin.py管理员.py

class PricingserviceAdmin(admin.StackedInline):
    model = Pricingservice

@admin.register(Pricing)
class PricingAdmin(admin.ModelAdmin):
    inlines = [PricingserviceAdmin]

    class Meta:
       model = Pricing

views.py视图.py

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

template.html模板.html

    <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>

[ [我想在价格下添加定价服务 ] 1 ] 1

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尝试这个

views.py视图.py

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'

urls.py网址.py

path('', HomePageView.as_view(), name='home'),
path('price/', PricePageView.as_view(), name='price'),

template.html模板.html

{% 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.

相关问题 如何使用CBV在所有Django模板中创建侧边栏? - How can I create a sidebar in all my Django templates using CBV? 如何将 if 语句添加到 deleteview? CBV django - How can I add if statement to a deleteview? CBV django 如果字符串中存在字段名,我如何使用 Django model 过滤器从我的数据库中查询对象 - How can I use Django model filter to query objects from my database if a fieldname is present in a string 在Django CreateView中无法从fbv迁移到cbv时出现问题,我的模型未提交我的模型 - Having problem to migrate from fbv to cbv in django CreateView which my model is not submitted in my models 如何在Django中为我的模型添加其他字段 - How can I add additional fields to my Model in Django 使用Django,如何将模型的ForeignKey添加到CreateView? - With Django, how can I add my model's ForeignKey to CreateView? 在django管理面板中无法添加和更改我的模型对象 - In django admin panel can't add and change my model objects 如何防止我的 Django 模型为我的模型列之一创建“延迟属性”? - How can I prevent my Django Model from creating a "deferred attribute" for one of my model columns? Django:如何使用模型类从Django外部与数据库交互? - Django: How can I use my model classes to interact with my database from outside Django? 我可以通过修改后的HTMLCalendar()从Django模型中迭代对象吗? - Can I iterate objects from my Django model over a modified HTMLCalendar()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM