如何从 Django 模型 select 特定对象并使用它来检索 url? (CS50 项目 2)

How do you select a particular object from a django model and use it to retrieve a url? (CS50 Project 2)

我正在做 CS50 项目 2,有一个名为 Listing 的 django 模型,它有一个名为 Category 的对象,用户可以使用下拉列表选择该对象。所有类别都列在类别页面上,需要 link 到显示该类别中所有列表的 categories_page 页面。你能帮我把类别页面的 href 和变量 link 转到 categories_page 页面吗?

类别views.py

def categories(request):
    return render(request, "auctions/categories.html",{
        "Miscellaneous": Listings.objects.all().filter(category=Miscellaneous)
    })

categories.html(我需要link本页的所有分类categories_page。)

{% block body %}
    <h1>Categories</h1>

    <a href="{% url 'categories' Miscellaneous.category %}" style = "color: rgb(58, 58, 58);"><h5>Miscellaneous</h5></a>
    <h5>Movies and Television</h5>
    <h5>Sports</h5>
    <h5>Arts and Crafts</h5>
    <h5>Clothing</h5>
    <h5>Books</h5>
{% endblock %}

categories_page views.py

def categories_page(request, category):
    listings = Listings.objects.all().filter(category=category)
    return render(request, "auctions/categories_page.html",{
        "listings": listings,
        "heading": category
    })

categories_page.html

{% block body %}
    <h1>{{ heading }}</h1>
    {% for listing in listings %}
        <a href="{% url 'listing' listing.id %}" style = "color: rgb(58, 58, 58);">
            <img src ="{{ listing.image }}" style = "height: 10%; width: 10%;">
            <h4 class = "text">{{ listing.title }}</h4>
            <h6>Description: {{ listing.description }}</h6>
            <h6>Category: {{ listing.category }}</h6> 
            <h6>Price: ${{ listing.bid }}</h6>
        </a>
    {% endfor %}

{% endblock %

categories_page 视图和 html 都有效。我在使用类别视图和 html.

时遇到问题

如果您需要更多代码,请告诉我。非常感谢!

urls.py

from django.urls import path
from .models import User, Listings
from .forms import ListingsForm
from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("create", views.create, name="create"),
    path("watchlist", views.watchlist, name="watchlist"),
    path("categories", views.categories, name = "categories"),
    path("categories/<str:category>/", views.categories_page, name = "categories_page"),
    path("listing/<int:id>/", views.listing, name = "listing"),
    path("register", views.register, name="register")
]

models.py

class Listings(models.Model):
    CATEGORY = [
    ("Miscellaneous", "Miscellaneous"),
    ("Movies and Television", "Movies and Television"),
    ("Sports", "Sports"),
    ("Arts and Crafts", "Arts and Crafts"),
    ("Clothing", "Clothing"),
    ("Books", "Books"),
]
    title = models.CharField(max_length=64)
    description = models.CharField(max_length=500)
    bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
    image = models.URLField(null=True, blank=True)
    category = models.CharField(max_length=64, choices=CATEGORY, default=None)

实际上没有必要尝试从模型中获取类别对象。此代码对我有用。

category.views

def categories(request):
    return render(request, "auctions/categories.html")

categories.html

<a href="{% url 'categories_page' 'Miscellaneous' %}" style = "color: rgb(58, 58, 58);"><h5>Miscellaneous</h5></a>
    <a href="{% url 'categories_page' 'Movies and Television' %}" style = "color: rgb(58, 58, 58);"><h5>Movies and Television</h5></a>
    <a href="{% url 'categories_page' 'Sports' %}" style = "color: rgb(58, 58, 58);"><h5>Sports</h5></a>
    <a href="{% url 'categories_page' 'Arts and Crafts' %}" style = "color: rgb(58, 58, 58);"><h5>Arts and Crafts</h5></a>
    <a href="{% url 'categories_page' 'Clothing' %}" style = "color: rgb(58, 58, 58);"><h5>Clothing</h5></a>
    <a href="{% url 'categories_page' 'Books' %}" style = "color: rgb(58, 58, 58);"><h5>Books</h5></a>

您所要做的就是输入 url 的名称,然后输入您想要的参数的名称。