簡體   English   中英

django 錯誤:NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)'

[英]django error : NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)'

我正在處理我的項目,主要想法是將一些項目添加到監視列表或“書簽”一些項目

當我渲染頁面以查看我通過單擊“添加到監視列表按鈕”添加它們的監視列表時,Django 給我這個錯誤:NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)' not found . 嘗試了 1 個模式:['Post/(?P[0-9]+)$']

我的代碼:

模型.py文件

class Post(models.Model):
     #data fields
     title = models.CharField(max_length=64)
     textarea = models.TextField()
     #bid
     price = models.FloatField(default=0)
     currentBid = models.FloatField(blank=True, null=True)
     imageurl = models.CharField(max_length=255, null=True, blank=True)
     category = models.ForeignKey(Category, on_delete=models.CASCADE, default="No Category Yet!", null=True,  blank=True)
     creator = models.ForeignKey(User, on_delete=models.PROTECT)
     date = models.DateTimeField(auto_now_add=True)
     # for activated the Category
     activate = models.BooleanField(default=True)
     buyer = models.ForeignKey(User, null=True, on_delete=models.CASCADE, related_name="auctions_Post_creator")
      ############################### this is the watchers
      watchers = models.ManyToManyField(User, blank=True, related_name='favorite')
      def __str__(self):
              return f"{self.title} | {self.textarea} |  {self.date.strftime('%B %d %Y')}"

網址.py

urlpatterns =[
        # Watchlist
         path('Post/<int:id>/watchlist_post/change/<str:reverse_method>',
         views.watchlist_post, name='watchlist_post'),
         path('watchlist/', views.watchlist_list, name='watchlist_list')
         path('Post/<int:id>', views.viewList, name='viewList'),

]

視圖.py

#start watchlist
 def viewList(request, id):
     # check for the watchlist
     listing = Post.objects.get(id=id)
     if listing.watchers.filter(id=request.user.id).exists():
             is_watched = True
     else:
             is_watched = False
     context = {
             'listing': listing,
             'comment_form': CommentForm(),
             'comments': listing.get_comments.all(),
             'Bidform': BidForm(),
              # IS_WATCHED method
             'is_watched': is_watched
     }
    return render(request, 'auctions/item.html', context)


 @login_required
 def watchlist_post(requset, id, reverse_method):
          listing = get_object_or_404(Post, id=id)
          if listing.watchers.filter(id=requset.user.id).exists():
                  listing.watchers.remove(requset.user)
          else:
                  listing.watchers.add(requset.user)
          if reverse_method == "viewList":
                  return viewList(requset, id)
        return HttpResponseRedirect(reverse(reverse_method))


 @login_required
 def watchlist_list(request):
         user = requset.user
         watchers_items = user.favorite.all()
         context = {
                 'watchers_items': watchers_items
         }
         return render(requset, 'auctions/watchlist.html', context)

HTML 文件:(item.html)此文件用於顯示例如帖子:(標題/描述/日期等。)

   <!-- check to add to watchlist -->
    <div class="col">
    {% if is_watched %}
    <a href="{% url 'watchlist_post' listing.id 'viewList' %}" class="btn btn-dark btn-sm m-1 btn-block">Remove To Watchlist</a>
    <!-- remove it -->
    {% else %}
        <a href="{% url 'watchlist_post' listing.id 'viewList' %}" class="btn btn-dark btn-sm m-1 btn-block">Add To Watchlist</a>
    {% endif %}
    </div>

HTML FILE (layout.html) 用於添加指向頁面導航欄的鏈接

            <li class="nav-item">
                <a class="nav-link" href="{% url 'watchlist_list' %}">Watchlist</a>
            </li>

HTML 文件(whitchlsit 頁面)

        {% for post in watchers_items %}
        <div class="col-sm-4">
            <div class="card my-2">
                <img src="{{post.imageurl}}" class="img-fluid">
                <div class="card-body">
                    <div class="text-center py-2">
                        <h5 class="card-title text-info">{{post.title}}</h5>
                        <p class="alert alert-info">{{post.textarea}}</p>
                        <ul class="list-group">
                            <li class="list-group-item list-group-item-info">category: {{post.category.name}}</li>
                            <li class="list-group-item list-group-item-info">Price: {{post.price}}$</li>
                            <li class="list-group-item list-group-item-info">Created by: {{post.creator}}</li>
                        </ul>
                    </div>

                    <!-- Buttons -->
                    <div class="row">
                        <div class="col">
                            <a href="{% url 'viewList' listing.id %}" class="btn btn-dark btn-sm m-1 btn-block">View</a>
                        </div>

                    </div>
                </div>
                <div class="card-footer">
                    <small class="text-muted">Created since:{{post.date}}</small>
                </div>
            </div>
            {% endfor %}

此問題的罪魁禍首是您正在調用"{% url 'viewList' listing.id %}"即使您的urls.py中不存在viewList

所以,你可以創建一個這樣的:

path("some/path", views.some_func, name="viewList")

編輯

正如錯誤所說, listing.id為空( {% url 'viewList' listing.id %} )。 因此,您可能想要使用post.id代替,因為您正在使用post的名稱進行循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM