【发布时间】:2021-11-05 13:32:36
【问题描述】:
我正在处理我的项目,主要想法是将一些项目添加到关注列表或“书签”一些项目:
当我呈现页面以查看我通过单击“添加到监视列表按钮”添加它们的监视列表时,Django 给我这个错误: /watchlist/ 中的 NoReverseMatch 未找到带有参数 '('',)' 的 'viewList' 的反向操作。尝试了 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')}"
urls.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'),
]
views.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 文件 (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 %}
【问题讨论】:
标签: python django django-models error-handling