按钮不工作有问题

There was a problem with the button not working

我是一名想精通Django的学生。按钮不起作用。如果您按下 detail.html 中的按钮,我想将产品保存在数据库中,就像我购买了它一样。我的目标是获取 views.py 上所写的买家、日期和产品代码。但是,即使您现在按下按钮,也无法将其保存在 DB 中。有什么问题吗?

model.py

class Join(models.Model):
    join_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
    part_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.join_code)

    class Meta:
        ordering = ['join_code']

Join/views

从日期时间导入时区

从 django.shortcuts 导入渲染

来自 zeronine.models 导入 *

def join_detail(request):
    product = Product.objects.all()

    if request.method == "POST":
        join = Join()
        join.product_code = product
        join.username = request.user
        join.part_date = timezone.now()
        join.save()

    return render(request, 'zeronine/detail.html', {'product': product})

detail.html

{% extends 'base.html' %}
{% block title %} 상품 상세보기 {% endblock %}
{% block content %}

<div class="container">
    <div class="row">
        <div class="col-4">
        <img src="{{product.image.url}}" width="190%" style="margin-top: 35px;">
    </div>
        <div class="text-center col" style="margin-top:150px; margin-left:200px;">
            <b><h4 class="content" style="margin-bottom: -5px;"><b>{{product.name}}</b></h4></b>
            <br>

                    <div>
<!--                        <span>주최자 : <b>{{ product.username }}</b></span><br>-->
                        <span style="color: #111111">모집기간 : <b>{{ product.start_date }} ~ {{ product.due_date }}</b></span>
                    </div>
                    <hr style="margin-top: 30px; margin-bottom: 30px;">

            <p><span class="badge badge-dark">가격</span>
            {% load humanize %}
                 {% for designated in designated_object %}
                        {% if designated.product_code.product_code == product.product_code %}
                            {{designated.price | floatformat:'0' | intcomma }}원
                        {% endif %}
                    {% endfor %}</p>


            <span class="badge badge-dark">목표금액</span> {{ product.target_price | floatformat:'0' | intcomma }}원 <br><br>

            <p class="badge badge-dark">공동구매 취지
                {{product.benefit|linebreaks}}</p>
            <p class="badge badge-dark">상세설명
                {{product.detail|linebreaks}}</p>

            <br>

            <form action="" method="post">
                {% csrf_token %}
                <a onclick="alert('{{ product.name }} 공동구매 참여가 완료되었습니다.');" style="cursor:pointer;">

                    <form method="POST" action ="{% url 'zeronine:join_detail' %}">
                  {% csrf_token %}
                     <div class="form-group">
                    <button type="submit" action="{% url 'zeronine:join_detail' %}" class="btn btn-primary" style="float: right; background: #637B46; border: white">업로드</button>
                     </div>
                 </form>

                </a>
            </form>
        </div>
    </div>
</div>


{% endblock %}

我不确定,但您的模板中有一个表单。也许这是导致问题的原因。

还有 在 POST 部分。最好使用

join = Join.objects.create(product_code=product, ....)```