您如何从Django模型表单中获取ID,将其用作URL并从表单中获取该值的所有数据?

发布于 2025-01-22 08:55:35 字数 1608 浏览 2 评论 0原文

我正在进行CS50项目2,需要制作一个网页,该网页通过URL列入ID,并从数据库中检索有关该特定项目的所有信息以在网页上显示。

def listing(request, id):
    id = Listings.objects.get(id=2)
    return render(request, "auctions/listing.html",{
        "listings": Listings.objects.all()
    }) 

这是我当前的代码。我试图通过变量获取URL参数中的ID,我希望列表变量仅获取该项目的对象,但是我不确定如何做到这一点。

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("listings/<int:id>/", views.listing, name = "listing"),
    path("register", views.register, name="register")
]

listing.html

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

{% block body %}

    {{% for listing in listings %}
    <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>
{% endfor %}

{% endblock %}

我知道我必须更改listing.html一旦我弄清楚如何获得ID即可仅显示该ID的对象,但是暂时是我的listing.html。

I am doing CS50 Project 2 and need to make a webpage that takes in the id through the url and retrieves all of the information about that particular item from the database to display on the webpage.

def listing(request, id):
    id = Listings.objects.get(id=2)
    return render(request, "auctions/listing.html",{
        "listings": Listings.objects.all()
    }) 

This is my current code. I am trying to get the id that is in the url parameter through the variable, and I want the listings variable to only get the objects for that item, but I am not exactly sure how to do that.

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("listings/<int:id>/", views.listing, name = "listing"),
    path("register", views.register, name="register")
]

listing.html

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

{% block body %}

    {{% for listing in listings %}
    <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>
{% endfor %}

{% endblock %}

I know that I will have to change the listing.html once I have figured out how to get the id to display only the objects for that id, but for the time being this is my listing.html.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-01-29 08:55:35

我在这里做了很多假设,所以很抱歉,但是我认为该页面没有发现错误,您的最初问题是相关的,我认为这一切都归结为做出两种不同的视图,两个不同的URL路径和两个不同的模板。 全部清单的一个视图/路径/模板 s (复数),另一个用于单个清单(单数)的视图/路径/模板。

首先,您将需要的URL:

path("listings/", views.listings, name = "listings"),
path("listing/<int:id>/", views.listing, name = "listing"),

您似乎已经属于列表 s view/path/template,并进行了一些修改。列表 s 视图将没有ID,因为它将显示 asher 列表(请注意,我将所有这些都更改为列表 s 而不是相反要列表,因此我不会将其与会照顾特定列表项目的单个清单的视图相混淆:

def listings(request):
    return render(request, "auctions/listings.html",{
       "listings": Listings.objects.all()
    })

此视图可以随您的列表 s 模板(请注意,我从中更改了名称列表 s .html,因为listing.html将照顾特定的列表,而listings.html也会列出所有列表)。 {%url'listing'listing.id%}“&gt; 部分是神奇的部分,其中将使用特定列表的ID创建URL(请注意 name 视图跟随URL单词,并且ID确实不是具有引号 - 它将是添加到生成的URL中的参数):

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

{% block body %}

    {{% for listing in listings %}
    <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 href="{% url 'listing' listing.id %}">link to this particular listing's page</a>
    {% endfor %}

{% endblock %}

现在是在特定列表的详细信息页面上,上面生成的链接将在其中进行接下来

def listing(request, id):
    listing = Listings.objects.get(id=id)
    return render(request, "auctions/listing.html",{
        "listing": listing
    }) 

,您必须创建一个称为listing.html的模板,您可以在其中显示特定列表的所有详细信息。

CS50网络编程课程非常棒,而且非常困难。有很大帮助的是阅读 notes ,也停止<< a href =“ https://cs50.harvard.edu/web/2020/weeks/3/” rel =“ nofollow noreferrer”>视频并编写他所展示的代码。所有答案都在那里。只是一个建议;每个人的学习方式都不同。祝你好运!

I'm making a lot of assumptions here, so apologies, but I think the page not found error and your original question are related, and I think it all comes down to making two different views, two different url paths, and two different templates. One view/path/template for all listings (plural), and another view/path/template for the individual listing (singular).

First the urls you will need:

path("listings/", views.listings, name = "listings"),
path("listing/<int:id>/", views.listing, name = "listing"),

What you already have seems like it would belong to the listings view/path/template, with some modifications. The listings view will not have an id, since it will display all the listings (note that I changed all these to listings as opposed to listing so I don't confuse this with the view that will take care of the single listing of a particular listing item:

def listings(request):
    return render(request, "auctions/listings.html",{
       "listings": Listings.objects.all()
    })

This view can go with your listings template (note that I changed the name from listing.html to listings.html, since listing.html will take care of the particular listing, while listings.html will list all listings). Also note the <a href="{% url 'listing' listing.id %}"> part which is the the magical part where the url will be created with the particular listing's id (note that the name of the view goes after the url word, and that the id does not have quotes - it will be the parameter added to the generated url):

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

{% block body %}

    {{% for listing in listings %}
    <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 href="{% url 'listing' listing.id %}">link to this particular listing's page</a>
    {% endfor %}

{% endblock %}

Now to the particular listing's detail page, where the above generated link will take the user to.

def listing(request, id):
    listing = Listings.objects.get(id=id)
    return render(request, "auctions/listing.html",{
        "listing": listing
    }) 

Next you have to create a template called listing.html where you can show all the details of the particular listing.

CS50 Web Programming course is fantastic, and very difficult. What helped me a lot was reading the notes, and also stopping the video and writing the code he demonstrates. All the answers are there. Just a suggestion; everyone learns differently. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文