Django文件未在网站上显示输入数据

发布于 2025-02-03 15:35:29 字数 2056 浏览 1 评论 0原文

因此,我早些时候遇到了类似的问题,代码无法正确迭代以使我的表输出工作,但是现在我尝试了一个描述列表,并且没有显示我的任何输入数据。任何帮助将不胜感激!

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import products, typeWork


def hello(request):
    context = typeWork.objects.all()
    context2 = {'context':context}
    return render(request, 'products/index.html', context2)

def detail(request, product_id):
    context3 = typeWork.objects.get(id=product_id)
    context4 = {'context3':context3}
    return render(request, 'products/detail.html', context4)

detail.html

{% extends 'base.html' %}

{% block content %}
    <dl>
        <dt>Work</dt>
        <dd>{{context4.work}}</dd>
        <dt>Type of Work</dt>
        <dd>{{context4.genre}}</dd>
    </dl>



{% endblock  %}

工作表的工作代码(就像它在表上显示输入数据一样,只是添加以查看是否可以提供有关详细信息的任何

{% extends 'base.html' %}

{% block content %}
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Work</th>
                <th>Type of Work</th>
                <th>Hours needed</th>
            </tr>
        </thead>
        <tbody>
            {% for context2 in context%}
                <tr>
                    <td>{{context2.work}}</td>
                    <td>{{context2.genre}}</td>
                    <td>{{context2.hoursWorked}}</td>
                </tr>
            {% endfor %}
        
        </tbody>
    
    </table>
{% endblock  %}

见解 问题

工作表图片 ”这是工作表“

So I had a similar issue earlier with the code not iterating correctly to get my table output to work but now i'm trying a description list and it is not showing any of my input data. Any help would be greatly appreciated!

Views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import products, typeWork


def hello(request):
    context = typeWork.objects.all()
    context2 = {'context':context}
    return render(request, 'products/index.html', context2)

def detail(request, product_id):
    context3 = typeWork.objects.get(id=product_id)
    context4 = {'context3':context3}
    return render(request, 'products/detail.html', context4)

Detail.html

{% extends 'base.html' %}

{% block content %}
    <dl>
        <dt>Work</dt>
        <dd>{{context4.work}}</dd>
        <dt>Type of Work</dt>
        <dd>{{context4.genre}}</dd>
    </dl>



{% endblock  %}

Working code for the table(like it shows input data on the table just adding to see if it may provide any insight on why the detail.html isn't showing the input data

index.html

{% extends 'base.html' %}

{% block content %}
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Work</th>
                <th>Type of Work</th>
                <th>Hours needed</th>
            </tr>
        </thead>
        <tbody>
            {% for context2 in context%}
                <tr>
                    <td>{{context2.work}}</td>
                    <td>{{context2.genre}}</td>
                    <td>{{context2.hoursWorked}}</td>
                </tr>
            {% endfor %}
        
        </tbody>
    
    </table>
{% endblock  %}

Picture of my actual issue
Actual issue

Working table pictureThis is the working table

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

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

发布评论

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

评论(2

无声静候 2025-02-10 15:35:30

我认为您通过称呼所有内容1或上下文4来使事情有些困惑4。

上下文是传递给模板的内容,它基本上是变量的字典,可以是QuerySets,字符串,其他词典或其他任何内容。因此,您最初所做的是(要使用更清晰的变量名称翻译):

def detail(request, product_id):
    type_of_work = typeWork.objects.get(id=product_id)
    context = {'type_of_work_context':type_of_work}
    return render(request, 'products/detail.html', context)

然后在您的视图中,您所提到的

<dd>{{context.work}}</dd>

而不是

<dd>{{type_of_work_context.work}}</dd>

在第二个示例中,在答案中起作用的示例,您实际上给出了上下文的上下文,将上下文与相同的名称符合相同的名称其中的记录,因此工作很幸运。

就像风格指南的建议一样,将上下文命令“上下文”称为“上下文”,而其中的元素更具识别名称来反映其内容。

I think you are confusing things a little by calling everything context1 or context4.

The context is what gets passed to the template, and it's basically a dictionary of variables that could be querysets, strings, other dictionaries or whatever. So what you initially did was (to translate with clearer variable names):

def detail(request, product_id):
    type_of_work = typeWork.objects.get(id=product_id)
    context = {'type_of_work_context':type_of_work}
    return render(request, 'products/detail.html', context)

and then in your view you referred to

<dd>{{context.work}}</dd>

instead of

<dd>{{type_of_work_context.work}}</dd>

In the second example, the one that worked in your answer, you've actually given the context dict the same name as the record within it, so it's fairly lucky that that worked.

Just as stylistic guide recommendation, call the context dict 'context', and the elements within it more recognisable names reflecting their contents.

染柒℉ 2025-02-10 15:35:30

这更多的是要求澄清一下,我更改了视图。

from django.shortcuts import render
from django.http import HttpResponse
from .models import products, typeWork


def hello(request):
    context = typeWork.objects.all()
    context2 = {'context':context}
    return render(request, 'products/index.html', context2)

def detail(request, product_id):
    context3 = typeWork.objects.get(id=product_id)
    context4 = {'context4':context3}
    return render(request, 'products/detail.html', context4)

This is more of asking for a clarification i changed the views.py to the following code i would just like clarification for why that worked and honestly i thought i tried that before i guess i didn't since it works now

from django.shortcuts import render
from django.http import HttpResponse
from .models import products, typeWork


def hello(request):
    context = typeWork.objects.all()
    context2 = {'context':context}
    return render(request, 'products/index.html', context2)

def detail(request, product_id):
    context3 = typeWork.objects.get(id=product_id)
    context4 = {'context4':context3}
    return render(request, 'products/detail.html', context4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文