gpt4 book ai didi

python - 模板不显示信息 - django

转载 作者:行者123 更新时间:2023-12-04 04:13:11 25 4
gpt4 key购买 nike

我不明白为什么我的数据不显示在我的 user.html 模板中。通过查看代码,一切似乎都已正确链接。 users.html 页面显示在网站上,但没有关于 {{ user.order_set.all }} 的信息。我已经突出显示了我认为与该问题相关的代码。任何指导将不胜感激,谢谢!

user.html - 用于显示数据的 html

{% extends "fuisce/base.html" %}

{% block content %}

**{{ user.order_set.all }}**


{% endblock content %}

views.py - 顺序

from django.shortcuts import render, HttpResponseRedirect
from django.urls import reverse

# Create your views here.

from carts.models import Cart

from .models import Order
from users.models import Profile

from .utils import id_generator

**def orders(request):
context = {}
template = "orders/user.html"
return render(request, template, context)**

def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return HttpResponseRedirect(reverse("cart"))

new_order, created = Order.objects.get_or_create(cart=cart)
if created:
new_order.order_id = id_generator()
new_order.save()
my_p = Profile.objects.get(user=request.user)
new_order.user = my_p
new_order.save()

if new_order.status == "Finished":
# cart.delete()
del request.session['cart_id']
del request.session['items_total']
return HttpResponseRedirect(reverse("cart"))


context = {}
template = "fuisce/about.html"
return render(request, template, context)

models.py - 订单

from django.db import models

# Create your models here.
from carts.models import Cart

from users.models import Profile

STATUS_CHOICES =(
("Started", "Started"),
("Abandoned", "Abandoned"),
("Finished", "Finished"),
)

class Order(models.Model):
**user = models.ForeignKey('users.Profile', null=True, blank=True, on_delete=models.CASCADE)**
order_id = models.CharField(max_length=120, default='ABC', unique=True)
cart = models.ForeignKey('carts.Cart', on_delete=models.CASCADE)
status = models.CharField(max_length=120, choices=STATUS_CHOICES, default="Started")
sub_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
tax_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
final_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)

def __string__(self):
return self.order_id

网址.py

from django.urls import path
from . import views
from carts import views as cart_views
from orders import views as order_views

urlpatterns = [
path('', views.home, name='fuisce-home'),
path('subscription/', views.subscription, name='fuisce-subscription'),
path('oneoff/', views.oneoff, name='fuisce-oneoff'),
path('about/', views.about, name='fuisce-about'),
path('contact/', views.contact, name='fuisce-contact'),
path('cart/', cart_views.view, name='cart'),
path('cart/<int:id>/', cart_views.remove_from_cart, name='remove_from_cart'),
path('cart/<slug>/', cart_views.add_to_cart, name='add_to_cart'),
path('checkout/', order_views.checkout, name='checkout'),
**path('orders/', order_views.orders, name='user_orders'),**

]

最佳答案

根据对问题的评论,您似乎有一个 ForeignKey 字段到 Profile 而不是 User

要么将您的 ForeignKeyProfile 更改为 User,如下所示:

class Order(models.Model):
user = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
null=True,
blank=True)

或者在您的模板中使用不同的查找:

{{ user.profile.order_set.all }}

关于python - 模板不显示信息 - django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61325054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com