django error : NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)'
django error : NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)'
我正在做我的项目,主要想法是将一些项目添加到关注列表或“添加书签”一些项目:
当我呈现页面以查看我通过单击“添加到监视列表按钮”添加它们的监视列表时,Django 给我这个错误:
/watchlist/ 上的 NoReverseMatch
对 'viewList' 进行反转,未找到参数“(”,)'。尝试了 1 种模式:['Post/(?P[0-9]+)$']
我的代码:
Model.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) 此文件用于显示 post 例如:(标题/描述/日期等。)
<!-- 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) 用于将 link 添加到页面的导航栏
<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 %}"
,即使 viewList
在您的 urls.py
中不存在。
因此,您可以这样创建一个:
path("some/path", views.some_func, name="viewList")
编辑
如错误所述,listing.id
为空 ({% url 'viewList' listing.id %}
)。因此,您可能想要执行 post.id
,因为您正在循环使用 post
.
的名称
我正在做我的项目,主要想法是将一些项目添加到关注列表或“添加书签”一些项目:
当我呈现页面以查看我通过单击“添加到监视列表按钮”添加它们的监视列表时,Django 给我这个错误: /watchlist/ 上的 NoReverseMatch 对 'viewList' 进行反转,未找到参数“(”,)'。尝试了 1 种模式:['Post/(?P[0-9]+)$']
我的代码:
Model.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) 此文件用于显示 post 例如:(标题/描述/日期等。)
<!-- 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) 用于将 link 添加到页面的导航栏
<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 %}"
,即使 viewList
在您的 urls.py
中不存在。
因此,您可以这样创建一个:
path("some/path", views.some_func, name="viewList")
编辑
如错误所述,listing.id
为空 ({% url 'viewList' listing.id %}
)。因此,您可能想要执行 post.id
,因为您正在循环使用 post
.