首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django错误: NoReverseMatch at :我得到了这个错误

Django错误: NoReverseMatch at :我得到了这个错误
EN

Stack Overflow用户
提问于 2021-02-22 23:51:25
回答 1查看 40关注 0票数 0

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

NoReverseMatch在/watchlist反向拍卖‘拍卖'('',)’找不到。1模式尝试:['(?P0-9+)$']请求方法: GET请求URL:

这是我的urls.py文件:

代码语言:javascript
复制
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文件片段如下:

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

分配“观察者”变量会使应用程序刹车并显示错误消息。你能帮帮我吗?概念上的错误在哪里?

坦卡斯

这是我的Watchlist.html文件:

代码语言:javascript
复制
{% 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文件:

代码语言:javascript
复制
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}"
EN

回答 1

Stack Overflow用户

发布于 2021-02-23 15:10:38

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66325124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档