django DJ-Stripe元数据未连接到产品

d6kp6zgx  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(111)

我遵循这个指南:
https://www.saaspegasus.com/guides/django-stripe-integrate/#contents
我进入“将元数据添加到Stripe对象”,并被告知创建一个metadata.py文件。它没有告诉我从哪里导入@dataclass和List,所以我不得不猜测。问题是,从指南中的这一点向前移动,元数据不会打印到我的模板中,但我可以检索产品信息。我认为这本指南是失踪或假设我知道的东西元数据是需要使它的工作。
我的metadata.py:

from dataclasses import dataclass

from pip import List
from project.subscriptions import features

@dataclass
class ProductMetadata(object):
    """
    Metadata for a Stripe product.
    """
    stripe_id: str
    name: str
    features: List[str]
    description: str = ''
    is_default: bool = False

PREMIUM = ProductMetadata(
    stripe_id='<prod id>',
    name='Premium',
    description='yerp',
    is_default=False,
    features=[
        features.UNLIMITED_WIDGETS,
        features.LUDICROUS_MODE,
        features.PRIORITY_SUPPORT,
    ],
)

features.py:

UNLIMITED_WIDGETS = 'Unlimited Widgets'
LUDICROUS_MODE = 'Ludicrous Mode'
PRIORITY_SUPPORT = 'Priority Support'

观点:

from django.shortcuts import render
from djstripe.models import Product

def pricing_page(request):
  context = {
    'products': Product.objects.all()
  }
  return render(request,'subscriptions/pricing_page.html', context=context)

网址:

path('pricing_page/', view=pricing_page, name='pricing_page'),

模板:

{% extends "base.html" %}{% load static socialaccount %}

{% block content %}

        <h1>Plans</h1>
       

        <section>
          <p class="title">Pricing Plans</p>
          <div class="columns">
            {% for product in products %}
              <div class="column">
                <p class="subtitle">{{ product.name }}</p>
                {% for plan in product.plan_set.all %}
                  <div>
                    <p class="heading">{{ plan.nickname }}</p>
                    <p>{{ plan.human_readable_price }}</p>
                  </div>
                {% endfor %}
              </div>
            {% endfor %}
          </div>
        </section>

        
        <div class="plan-interval-selector">
          {% for plan in plans %}
            <button class="button">{{ plan.name }}</button>
          {% endfor %}
        </div>
        <div class="columns plan-selector">
          {% for product in products %}
            {{ product.metadata|pprint }}

            <div class="column">
              <div {% if product.metadata.is_default %}class="is-selected"{% endif %}>
                <span class="icon">
                  <i class="fa {% if product.metadata.is_default %}fa-check{% else %}fa-circle{% endif %}">
                  </i>
                </span>
                <p class="plan-name">{{ product.metadata.name }}</p>
                <p class="plan-description">{{ product.metadata.description }}</p>
                <div class="plan-price">
                  <span class="price">{{ product.metadata.monthly_price }}</span> / month
                </div>
                <ul class="features">
                  {% for feature in product.metadata.features %}
                  <li>
                    <span class="icon"><i class="fa fa-check"></i></span>
                    <span class="feature">{{ feature }}</span>
                  </li>
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endfor %}
        </div>
{% endblock content %}

第一个forloop直接从产品中获取属性,它可以工作。第二个方法尝试从元数据中获取属性。它为每个产品循环,但不输出元数据。有什么建议吗

j2cgzkjk

j2cgzkjk1#

这对我很有效

def pricing_page(request):
        products = Product.objects.all()
        for product in products:
            if product.id == metadata.Starter.stripe_id:
                product.metadata = metadata.Starter
            elif product.id == metadata.Growth.stripe_id:
                product.metadata = metadata.Growth
            elif product.id == metadata.Advanced.stripe_id:
                product.metadata = metadata.Advanced
        
        return render(request, "home/pricing_page.html", ({'products': products }))

相关问题