【问题标题】:Django: checkbox values not printing to new templateDjango:复选框值未打印到新模板
【发布时间】:2020-12-14 11:18:26
【问题描述】:

我正在开发一个用作杂货店的 Django 项目。我正在尝试对其进行设置,以便当人们单击复选框并按下确认购买按钮时,复选框中的值将打印到新的 HTML 模板。我遇到的问题是,当我转到新模板时,它不会打印复选框中的值。

Views.py

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html'  # <app>/<model>_<viewtype>.html
    context_object_name = 'posts'

def inventory(request):
    products = request.POST.getlist('products')
    for product in products:
        a = Post.objects.get(title=product)
        a.quantity = a.quantity -1
        a.save()
    print(products)
    return render(request, 'blog/confirm.html')

主页.html

{% extends "blog/base.html" %}
{% block content %}
    <form action="{% url 'inventory' %}" method="POST" id="menuForm">
      {% for post in posts %}
        {% if post.quantity > 0 %}
            <article class="media content-section">
              <div class="media-body">
                <div class="article-metadata">
                  <a class="mr-2">{{ post.category }}</a>
                </div>
                <h2><a class="article-title" >{{ post.title }}</a></h2>
                <p class="article-content"> Price: ${{ post.Price }}</p>
                <p class="article-content"> Sale: ${{ post.Sale }}</p>
                <input type="checkbox" id="product_{{ post.id }}" value="{{ post.title }}" form="menuForm" name="products" > Inventory count: {{ post.quantity }}
              </input>
              </div>
            </article>
        {% else %}
        {% endif %}
      {% endfor %}
      <button type="submit" form="menuForm">Confirm Purchase</button>
    </form>
{% endblock content %}

确认.html

{% extends "blog/base.html" %}
{% block content %}
{% for post in posts %}
        <article class="media content-section">
          <div class="media-body">
            <div class="article-metadata">
              <a class="mr-2">{{ post.category }}</a>
            </div>
            <h2><a class="article-title" >{{ post.title }}</a></h2>
            <p class="article-content"> Price: ${{ post.Price }}</p>
            <p class="article-content"> Sale: ${{ post.Sale }}</p>
            <input type="checkbox" id="product_{{ post.id }}" value="{{ post.title }}" form="menuForm" name="products" > Inventory count: {{ post.quantity }}
          </input>
          </div>
        </article>
{% endfor %}
{% endblock %}

urls.py

path('list/', PostListView.as_view(), name='blog-home'),
path('confirm', views.inventory, name='inventory'),

【问题讨论】:

  • confirm.html 页面数据即将到来?
  • @Ankit:是的,confirm.html 显示导航菜单,但不显示任何其他数据。我会添加一张图片以供参考。

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


【解决方案1】:

这是因为你没有在confirm.html页面上传递帖子

def inventory(request):
  posts = Post.objects.get(title=product)
  products = request.POST.getlist('products')
  for product in products:
      a = Post.objects.get(title=product)
      a.quantity = a.quantity -1
      a.save()
  print(products)
  return render(request, 'blog/confirm.html',{'posts':posts})

【讨论】:

  • @Yahya 如果库存是您的 PostListView 实例,而不是将 self 传入其中并进入您的班级
猜你喜欢
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2021-08-13
  • 1970-01-01
  • 2011-05-15
相关资源
最近更新 更多