首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从django模型中选择特定对象并使用它检索url?(CS50项目2)

如何从django模型中选择特定对象并使用它检索url?(CS50项目2)
EN

Stack Overflow用户
提问于 2022-05-15 22:03:43
回答 1查看 70关注 0票数 0

我正在执行CS50项目2,并有一个名为list的django模型,其中有一个名为an的对象,用户可以使用下拉列表来选择这个对象。所有类别都列在类别页面上,需要链接到显示该类别中所有列表的categories_page页面。您能帮助我使用类别页面的href和变量链接到categories_page页面吗?

类别views.py

代码语言:javascript
运行
复制
def categories(request):
    return render(request, "auctions/categories.html",{
        "Miscellaneous": Listings.objects.all().filter(category=Miscellaneous)
    })

categories.html (我需要将这个页面上的所有类别链接到它们自己的categories_page)。

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

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-17 14:01:29

实际上,没有必要尝试从模型中获取分类对象。这个密码对我有用。

category.views

代码语言:javascript
运行
复制
def categories(request):
    return render(request, "auctions/categories.html")

categories.html

代码语言:javascript
运行
复制
<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的名称放在您想要的参数的名称上。

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

https://stackoverflow.com/questions/72252521

复制
相关文章

相似问题

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