将关注列表添加到产品页面

Add watchlist to product page

提问人:zahra shokrizadeh 提问时间:11/5/2023 更新时间:11/6/2023 访问量:35

问:

通过单击“添加”按钮,用户应该能够将产品添加到监视列表中。 在此之后,该按钮应更改为 Remo,按下它,该产品将从列表中删除。 页面顶部应该有一个链接标题,然后单击要显示的监视列表中的那些产品。 但是,这些代码不执行任何操作。 为什么??

表格和 models.py:

#forms.py
class AddWatchlist(forms.Form):
    product_id = forms.IntegerField(widget=forms.HiddenInput())


#models.py(Related parts)
class Product(models.Model):
    title = models.CharField(max_length=64)

views.py:

def product_detail(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    comments = Comment.objects.filter(product=product)
    offers = PriceOffer.objects.filter(product=product).order_by('-offer_price')
    off_list = [float(product.first_bid)]
    maximum = float(product.first_bid)
    if request.method == 'POST':
        # comment
        # offer
        
    off = maximum
    context = {
        'product': product,
        'comments': comments, 
        'offers': offers,
        'off' : off
        }

    return render(request, 'auctions/product_detail.html', context)

def watchlist(request):
    products = request.user.watchlist.all()
    return render(request, 'auctions/watchlist.html', {'products': products})

def add(request):
    if request.method == 'POST':
        form = AddWatchlist(request.POST)
        if form.is_valid():
            product_id = form.cleaned_data['product_id']
            product = Product.objects.get(id=product_id)
            request.user.watchlist.add(product)
            return redirect('product_detail')
   else:
       form = AddWatchlist()

   return render(request, 'auctions/add.html', {'form': form})

def remove(request, product_id):
    product = Product.objects.get(id=product_id)
    request.user.watchlist.remove(product)
    return redirect('watchlist')

urls.py:

path("product_detail/<int:product_id>/", views.product_detail, name="product_detail"),
path('add', views.add, name='add'),
path('remove/<int:product_id>/', views.remove, name='remove'),
path('watchlist', views.watchlist, name='watchlist'),

product_detail.html(相关部分):

{% for product in products %}
    {{ product.name }}
    {% if product in request.user.watchlist.all %}
        <form action="{% url 'remove' product.id %}" method="post">
            {% csrf_token %}
            <button type="submit">Remove</button>
        </form>
    {% else %}
        <form action="{% url 'add' %}" method="post">
            {% csrf_token %}
            <input type="hidden" name="product_id" value="{{ product.id }}">
            <button type="submit">Add</button>
        </form>
    {% endif %}

{% endfor %}

添加.html:

{% extends "auctions/layout.html" %}
{% block body %}

    <form action="{% url 'add' %}" method="post">
        {% csrf_token %}
        {{ form }}
        <button type="submit">Add to Watchlist</button>
    </form>

{% endblock %}

监视列表.html:

{% extends "auctions/layout.html" %}
{% block body %}

    {% if products %}
        {% for product in products %}
            {{ product.name }}
            <form action="{% url 'remove' product.id %}" method="post">
                {% csrf_token %}
                <button type="submit">Remove</button>
            </form>
        {% endfor %}
    {% else %}
        <p>No products in watchlist</p>
    {% endif %}

{% endblock %}
python html django 手表

评论


答:

0赞 Roberto Rubertelli 11/6/2023 #1

如果单击按钮时没有任何问题,您是否忘记保存表单?

form.save()

评论

0赞 zahra 11/11/2023
谢谢你的回答。这些代码没有输出,即不显示任何按钮。