django 将监视列表添加到产品页面

uinbv5nw  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(111)

通过点击添加按钮,用户应该能够将产品添加到监视列表.在此之后,该按钮应更改为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 %}


add.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 %}


watchlist.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 %}

cbwuti44

cbwuti441#

如果单击按钮时没有出现任何错误,您是否忘记了保存表单?

form.save()

字符串

相关问题