Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

Django - Function should show all entries added to a list, but only one entry is appearing. HTML issue?

I am creating a way for a user to add a post to a list on a website. The page should show all the different items on their list, when they go to their watchlist page. Currently the page is only showing one item, not all of the items added. I know the other items are successfully added because when I view the page as admin http://127.0.0.1:8000/admin/ all the items are there. So I think my error is in the html file of the watchlist. Any help is appreciated as I am new to Django.

html file

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

{% block body %}
<div class="container">
<h2>Items on your watchlist:</h2>

</div>
<div class="container" id="listings">

{% for item in items %}
{% for i in item %}

<div class="card mb-3" style="max-width: 540px;" id="card-listings">
    <div class="row no-gutters">
      <div class="col-md-4">

          <img src="{{ i.image.url }}" alt="..." class="card-img" style="max- 
   height:350px">

      </div>
      <div class="col-md-8">
        <div class="card-body">
          <h5 class="card-title">{{i.title}}</h5>
          <p class="card-text">{{i.description}}</p>
          <p class="card-text">${{i.price}}</p>
          <a href="{% url 'listingpage' i.id %}"><button class="btn btn-outline- 
   success">Bid Now!</button></a>
              <p class="card-text"><small class="text-muted">{{i.time}}</small></p>
        </div>
      </div>
    </div>
  </div>
  {% endfor %}
  {% empty %}
  <h3>No listings found in your watchlist...</h3>
  {% endfor %}
 </div>
{% endblock %}

This function is for viewing the page - should display all entries that were added to the page.

def mywatchlist(request, username):
    if request.user.username:
        w = Watchlist.objects.filter(user=username)
        items = []
        for i in w:
            items.append(Listings.objects.filter(id=i.listingid))
            w = Watchlist.objects.filter(user=request.user.username)
            wcount = len(w)
            return render(request, "auctions/watchlist.html", {
                "items": items,
                "wcount": wcount,
            })

        w = Watchlist.objects.filter(user=request.user.username)
        wcount = len(w)
        if wcount < 1:
            return render(request, "auctions/watchlist.html", {
                "items": None,
                "wcount": wcount,
            })

This will add an entry to the list:

def addwatchlist(request, listingid):
    if request.user.username:
        entry = Watchlist()
        entry.user = request.user.username
        entry.listingid = listingid
        entry.save()
        return redirect('listingpage', id=listingid)
    else:
        return redirect('index')
question from:https://stackoverflow.com/questions/65545453/django-function-should-show-all-entries-added-to-a-list-but-only-one-entry-is

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...