Home > Mobile >  Django file not showing input data on website
Django file not showing input data on website

Time:06-02

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 >
        <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

CodePudding user response:

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)

CodePudding user response:

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.

  • Related