我无法将对象从 Djanog 页面的索引页面传输到 display_item.html 页面

问题描述 投票:0回答:1

我已经尝试了很长时间来传输 item,这是通过 Django 模板语言中的 for 循环获得的。项目是一个对象。我尝试了很多不同的安排,但到目前为止没有任何效果。

这是索引文件。

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

{% block body %}
    <h2>Active Listings</h2>
    {% for item in listings %}
   
   
    <a href="{% url 'display_item' entry=item  %}">
   
        <img src="/media/{{ item.image_url }}" width="300" height="400" alt="Picture of the item.">
   
        <h3>{{ item.title }}</h3>
   
    </a>
    <p>{{ item.description }}</p>
    <h5>Starting bid: {{ item.starting_bid }}</h5>
    {% empty %}
    <h4>No listings to display!</h4>
   {% endfor %}
{% endblock %}

它是应该显示对象的文件。

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

{% block body %}
<img src="/media/{{ item.image_url }}" width="400" alt="Picture of item here.">
<p>{{ item.category }}</p>
<h3>{{ item.title }}</h3>
<p>{{ item.description }}</p>
<br>
<br>
<h5>Created by {{ item.created_by }}</h5>
<p>{{ item.created_at }}</p>
{% endblock %}

这是 URLS.py 文件。

from django.conf import settings 
from django.conf.urls.static import static

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("new_listing", views.new_listing, name="new_listing"),
    path("display_item/<str:entry>/", views.display_item , name="display_item")
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的views.py 文件。

from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import Listing, Bid, Comment,User


def index(request):
    
    
    # return HttpResponse(f"The listings are: "+str(listings))
   
    return render(request, "auctions/index.html",{
                  "listings": Listing.objects.all() }
                  )


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "auctions/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request, "auctions/register.html")


def new_listing(request):
    if request.method == "POST":
        title = request.POST["title"]
        description = request.POST["description"]
        starting_bid = request.POST["starting_bid"]
        image_url = request.POST["image_url"]
        category = request.POST["category"]
        created_by = request.user

        # Attempt to create new listing
        try:
            listing = Listing.objects.create(
                title=title,
                description=description,
                starting_bid=starting_bid,
                image_url=image_url,
                category=category,
                created_by=created_by
            )
            listing.save()
        except IntegrityError:
            return render(request, "auctions/new_listing.html", {   
                "message": "Error creating new listing."
            })
        return HttpResponseRedirect(reverse("index"))
    else:
       return render (request, "auctions/new_listing.html")
    
def display_item(request, entry):
    if entry is None:
        ty = type(entry)
        return HttpResponse("It's null brao.")
    else:
        return HttpResponse("Type is "+ty)
    # return render(request, "auctions/item.html", {
    #     "item":entry
    # })

我尝试了 chatGPT,我尝试检查其数据类型并渲染它,但它不可见。 检查我的代码很多次。

我期待它能发挥作用。

python html django web
1个回答
0
投票

因此,在 Django 中,我们无法直接通过 URL 传递对象。 我们要做的只是通过 URL 某种方式(通常是 ID)传递到将渲染后续页面的函数,然后直接在该视图函数上调用该对象。

© www.soinside.com 2019 - 2024. All rights reserved.