【问题标题】:Django error: NoReverseMatch at : I get this errorDjango 错误:NoReverseMatch 在:我收到此错误
【发布时间】:2021-02-22 23:51:25
【问题描述】:

我有以下我不理解的概念错误: 请看一下抛出的错误:

NoReverseMatch 在 /watchlist 未找到带有参数 '('',)' 的 'auction' 的反向操作。尝试了 1 种模式:['(?P[0-9]+)$'] 请求方法:GET 请求网址:http://127.0.0.1:8000/watchlist

这是我的 urls.py 文件:

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("<int:auction_id>", views.auction, name="auction"),
    path("new", views.newItem, name="newItem"),
    path("watchlist", views.watchlist, name="watchlist"),
    path("addwatchList/<int:auction_id>", views.add_watchlist, name="add_watchlist"),
    path("remwatchList/<int:auction_id>", views.rem_watchlist, name="rem_watchlist"),

]

而出现错误的views.py文件片段是这样的:

@login_required
def watchlist(request):
    u = User.objects.get(username=request.user)
    return render(request, "auctions/watchlist.html", {
        "watcher": u.watchingAuction.all()
        })

分配“watcher”变量会使应用程序停止并显示错误消息。 你能帮帮我吗?概念错误在哪里?

谢谢

这是我的 Watchlist.html 文件:

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

{% block body %}
    <h2 style="text-align: center;">My Watchlist!</h2>
    <hr>
    {% if watcher%}
    <div class="row">

{% for item in watcher %}
<div class="card" style="width: 18rem;">
  <img src="{{item.watchingAuction.url}}" class="card-img-top" alt="..." style="max-height: 200px; min-height: 200px; max-width: 180px; min-width: 180px;">
  <div class="card-body">
    <h5 class="card-title">{{item.watchingAuction.name}}</h5>
    <h6 class="card-title">Price: ${{item.watchingAuction.startBid}}</h6>
    <h6 class="card-title">{{item.watchingAuction.owner}}</h6>
    <p class="card-text">{{item.watchingAuction.description}}</p>
    <a href="{% url 'auction' item.watchingAuction.id %}" class="btn btn-primary">To Auction</a>
  </div>
</div>
  {% endfor %}
  {% else %}
  <h3 style="text-align: center; color:darkslateblue;">No auctions watched so far</h3>
  {% endif %}
</div>
{% endblock %}

这是我的 models.py 文件:

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    pass


class Auction(models.Model):
    NOCAT = 'NO'
    SURF = 'SU'
    KITESURF = 'KI'
    WINDSURF = 'WI'
    SKI = 'SK'
    BASE = 'BA'
    CATEGORY_CHOICES = [
        (NOCAT, 'Nocat'),
        (SURF, 'Surf'),
        (KITESURF, 'Kitesurf'),
        (WINDSURF, 'Windsurf'),
        (SKI, 'Ski'),
        (BASE, 'Base'),
    ]
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="auctionOwner")
    name = models.CharField(max_length=64)
    description = models.TextField(max_length=200)
    startBid = models.FloatField()
    currentBid = models.FloatField(blank=True, null=True)
    url = models.URLField(blank=True)
    watching = models.ManyToManyField(User, blank=True, related_name="watchingAuction")
    category = models.CharField(
    blank=True,
    max_length=2,
    choices=CATEGORY_CHOICES,
    default=SURF )

    def __str__(self):
        return f"{self.name} {self.startBid} {self.category}"

    def snippet(self):
        return self.description[:25] + "..."

class Bids(models.Model):
    item = models.ForeignKey(Auction, on_delete=models.CASCADE, related_name="itemBid")
    bidder = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bidMan")
    bid = models.FloatField()

    def __str__(self):
        return f"{self.bidder} {self.bid}"


class Comments(models.Model):
    comment = models.TextField(max_length=300)
    commenter = models.ForeignKey(User, on_delete=models.CASCADE, related_name="userComment")
    item = models.ManyToManyField(Auction,blank=True, related_name="speech")

    def __str__(self):
        return f"{self.commenter} says: {self.comment}"

【问题讨论】:

  • 显示你的watchlist.html
  • 即使 watchlist.html 文件被完全注释掉,我也会得到同样的错误。请查看我的 watchlist.html
  • 我刚刚在原始消息的底部添加了监视列表文件。谢谢!
  • 还添加了我的 models.py 文件,以防万一。谢谢

标签: python django


【解决方案1】:

我发现了错误,我在 for 循环中访问了每个元素的错误属性。现在解决了

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 2015-10-24
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2021-05-24
    相关资源
    最近更新 更多