创建监视列表功能

Create a watch list feature

提问人:zahra shokrizadeh 提问时间:11/16/2023 最后编辑:zahra shokrizadeh 更新时间:11/17/2023 访问量:47

问:

用户可以通过单击添加按钮将该产品添加到观察列表中,然后添加按钮将更改为 Rimo。这一次,通过单击此按钮,该产品将从列表中删除。 主页上应该有一个链接,点击它,就会显示观察列表中的所有产品,点击详情按钮,可以看到产品的详细信息,点击删除按钮,可以将它们从列表中删除。

models.py

class User(AbstractUser):
    pass


class List(models.Model):
    choice = (
        ('d', 'Dark'),
        ('s', 'Sweet'),
    )
    user = models.CharField(max_length=64)
    title = models.CharField(max_length=64)
    description = models.TextField()  
    category = models.CharField(max_length=64)      
    first_bid = models.IntegerField()  
    image = models.ImageField(upload_to="img/", null=True)      
    image_url = models.CharField(max_length=228, default = None, blank = True, null = 
             True)
    status = models.CharField(max_length=1, choices= choice)
    active_bool = models.BooleanField(default = True)

class Watchlist(models.Model):
    user = models.CharField(max_length=64)
    watch_list = models.ForeignKey(List, on_deleted= 
                models.CASCADE)

views.py:

def product_detail(request, product_id):
    product = get_object_or_404(List, pk=product_id)
    comments = Comment.objects.filter(product=product)

    if request.method == 'POST':
    
        # comment
        if 'comment' in request.POST:
            user = request.user
            if user.is_authenticated:
                content = request.POST.get('content')
                comment = Comment(product=product, user=user, content=content)
                comment.save()
    

    context = {
        'product': product,
        'comments': comments, 
       }

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



 @login_required(login_url="login")
 def add(request):
    product_id = request.POST.get('productid', False)
    watch = Watchlist.objects.filter(user = 
            request.user.username)

    for items in watch:
    if int(items.watch_list.id) == int(product_id):
        return watchlist(request, request.user.username)


    watch_list = get_object_or_404(List, pk = product_id)
    user = request.user.username
    context = {
        'watch_list': watch_list,
        'user': user
    }
    new_watch = get_object_or_404(Watchlist, context)
    new_watch.save()


    messages.success(request, "Item added to watchlist")
    return product_detail(request, product_id)




@login_required(login_url="login")
def remove(request):
    remove_id = request.GET["productid"]
    list_ = Watchlist.objects.get(pk = remove_id)
    messages.success(request, f"{list_.watch_list.title} is 
            deleted from your watchlist.")
    list_.delete()
    return redirect("index")


@login_required(login_url="login")
def watchlist(request, username):
    products = Watchlist.objects.filter(user = username)
    return render(request, 'auctions/watchlist.html', 
             {'products': products})

product_deyail.html(仅与问题相关的部分):

<form method= "get" action = "{% url "add" %}">
    <button type = "submit" value = {{ product.id }} name = 
                         "productid" >Add to Watchlist</button>
</form>

监视列表.html:

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

    {% if products %}
        {% for product in products %}
            <img src= {{ item.image.url }} alt = "{{item.title}}"><br>
            <a>{{ item.title }}</a><br>
            <a>{{ item.category }}</a><br>
            <a><a>Frist Bid: </a> {{ item.first_bid }} $ </a><br>

            <a href="{% url 'product_detail' item.id %}">View Product</a>

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

layout.html(仅与问题相关的部分 & 显示链接名称):

<ul class="nav">
        <li class="nav-item">
            <a class="nav-link" href="{% url 'index' %}">Active Listings</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="{% url 'watchlist' 
                  user.username %}">My WatchList</a>
        </li>


        {% if user.is_authenticated %}
            <li class="nav-item">
                <a class="nav-link" href="{% url 'logout' %}">Log Out</a>
            </li>
        {% else %}
            <li class="nav-item">
                <a class="nav-link" href="{% url 'login' %}">Log In</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="{% url 'register' %}">Register</a>
            </li>
        {% endif %}

单击监视列表时,将显示文本为“监视列表中没有产品”的页面,但是单击“添加监视列表”按钮时,会返回错误: (页面未找到 (404)

没有与给定查询匹配的列表。

请求方式:GET

请求 URL:http://127.0.0.1:8000/add/?productid=1

募集者:auctions.views.add)

原因是什么?

python html django 列表 观察

评论


答: 暂无答案