Django模板未显示我的所有参数

时间:2019-12-02 18:04:53

标签: python django django-models django-templates django-views

我有问题。我想在模型Django中创建关系。我希望每个用户都有不同的产品价格。必须将产品分配给用户。它必须仅在登录后才显示。不幸的是,该模板并未显示所有参数。

offers / models.py

class Product(models.Model):
  title = models.CharField(max_length=100)
  category = models.CharField(max_length=50)
  weight = models.FloatField()
  description = models.TextField(blank=True)
  photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
  is_published  = models.BooleanField(default=True)
  list_date = models.DateField(default=datetime.now, blank=True)

  def __str__(self):
    return self.title

class UserProduct(models.Model):
  user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  title = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
  price = models.FloatField()
  is_published  = models.BooleanField(default=True)
  list_date = models.DateField(default=datetime.now, blank=True)

  def __str__(self):
    return str(self.user.username) if self.user.username else ''

要为登录用户显示产品。

offers / views.py

def index(request):
  context = {
      'products': Product.objects.order_by('category').filter(is_published=True)
  }
  return render(request, 'offers/products.html', context)


def userproduct(request):
   context = {
      'userproduct': UserProduct.objects.filter(user_id=request.user.id),
      'userproduct1': Product.objects.all()
  }
   return render(request, 'offers/userproducts.html', context)

我的模板仅显示标题和价格。

offers / userproducts.html

<!-- Offers -->
        <section class="text-center mb-4 py-4">
          <div class="container">
            <div class="row">
                {% if user.is_authenticated %}
                {% if userproduct %}
                {% for product in userproduct %}

            <!--Grid column-->
            <div class="col-lg-3 col-md-6 mb-4">
              <div class="card">
                <div class="view overlay">
                  <img src="{{ product.photo.url }}" class="card-img-top" alt="">
                  <a href="{% url 'product' product.id %}">
                    <div class="mask rgba-white-slight"></div>
                  </a>
                </div>
                <div class="card-body text-center">
                    <h6 class="grey-text">{{ product.category }}</h6>
                  <h5>
                    <strong>
                      <a href="{% url 'product' product.id %}" class="dark-grey-text">{{ product.title }}</a>
                    </strong>
                  </h5>
                  <h4 class="font-weight-bold colores">
                    <strong>{{ product.price }}</strong>
                  </h4>
                </div>
              </div>
            </div>
            <!--Grid column-->

            {% endfor %}
            {% else %}
              <div class="col-sm-12 sm-12">
                <p>No Products Available XD</p>
              </div>
            {% endif %}
            {% endif %}

          </div>
        </div>
        </section>

1 个答案:

答案 0 :(得分:2)

您要遍历的public function handle($request, Closure $next) { $post = Post::findBySlugOrFail($request->slug); if ($post->status == "draft" && auth()->check() && auth()->user()->hasAnyRole(['Administrator'])) { return $next($request); } else { abort(404); } } 查询集属于userproduct模型,该模型没有您要查找的字段。

这应该是这样的:

UserProduct

如果您需要<!-- Offers --> <section class="text-center mb-4 py-4"> <div class="container"> <div class="row"> {% if user.is_authenticated %} {% if userproduct %} {% for product in userproduct %} <!--Grid column--> <div class="col-lg-3 col-md-6 mb-4"> <div class="card"> <div class="view overlay"> <img src="{{ product.title.photo.url }}" class="card-img-top" alt=""> <a href="{% url 'product' product.title.id %}"> <div class="mask rgba-white-slight"></div> </a> </div> <div class="card-body text-center"> <h6 class="grey-text">{{ product.title.category }}</h6> <h5> <strong> <a href="{% url 'product' product.title.id %}" class="dark-grey-text">{{ product.title.title }}</a> </strong> </h5> <h4 class="font-weight-bold colores"> <strong>{{ product.price }}</strong> </h4> </div> </div> </div> <!--Grid column--> {% endfor %} {% else %} <div class="col-sm-12 sm-12"> <p>No Products Available XD</p> </div> {% endif %} {% endif %} </div> </div> </section> 模型中的字段,则应获取Product实例Product。所以:

  • 价格:title

  • 标题:product.priceproduct.title.title也可以使用product.title功能。)

  • 类别:__str__

  • 照片:product.title.category

  • 以此类推...