您好,我的django标签未显示结果。我不知道我在哪里犯错,因为我是django python的新手。如果您想查看更多代码,请告诉我。我使用 product_details = Product.objects.get(id = product_id)来查询同一模型的对象,并希望使用html显示它用django标签显示它,但是它不起作用。
模板:
{% extends 'default/base.html' %}
{% block content %}
<h1>Products Details </h1>
<p>These are the details of your product, {{ user.username }}</p>
{% for s in product__details %}
{{ s.name }}
{% endfor %}
{% endblock %}
我的观点
from django.shortcuts import render, redirect, get_object_or_404
from .forms import NewPro, Product
from django.contrib.auth.decorators import login_required
@login_required()
def pro(request):
form = NewPro()
if request.method == 'POST':
form = NewPro(request.POST, request.FILES)
if form.is_valid():
entry = form.save(commit=False)
entry.user = request.user
entry.save()
return redirect('add_products')
else:
form = NewPro()
return render(request, "default/add_product.html", {'form': form})
@login_required()
def product(request, product_id):
print(product_id)
details = get_object_or_404(Product, id=product_id)
product_details = Product.objects.get(id=product_id)
print(product_details)
return render(request, "default/product_details.html", {'details': details, 'product_details': product_details})
答案 0 :(得分:1)
您的上下文是product_details,但对于循环product__details。你应该改变。
{% for s in product_details %}
{{ s.name }}
{% endfor %}