0

Ok, I'm stuck and I don't know what I'm doing wrong. I have database and i need to fetch data from it and render it in HTML. I've watched few tutorials and it doesn't seem hard but it's not working for me. this is my view:

def get_all_subjects(request):
    all_subjects = Predmeti.objects.all()
    return render(request, 'home.html', {"Predmeti": all_subjects})

and this is home.html:


{% block content %}
    {% if user.is_authenticated %}
        Hi {{ user.email }}!
        <p><a href="{% url 'logout' %}">logout</a></p>
        <table>
        {% for predmet in Predmeti %}

            <tr>
            <td>{{ predmet.ime }}</td>
            <td>{{ predmet.kod }}</td>
            <td>{{ predmet.bodovi }}</td>
            <td>{{ predmet.izborni }} </td>

            <td>
            <form method="POST" action="{% url 'upisni list' id=predmet.id %}">
                {% csrf_token %}
                <button type="submit"> Dodaj</button>
            </form>
            </td>
            </tr>

        {% endfor %}
        </table>
{% else %}
        <p>You are not logged in</p>
        <a href="{% url 'login' %}">login</a> |
        <a href="{% url 'signup' %}">signup</a>
    {% endif %}
{% endblock %}

It just says hi to username. Any help is welcome

4
  • is your data saved to a default database? Commented Feb 7, 2020 at 10:24
  • does you data actually contain any records? Commented Feb 7, 2020 at 10:27
  • Is the logout button getting displayed? Commented Feb 7, 2020 at 10:33
  • I can supposed the variable's name Predmeti you pass is not well enough. Maybe there's a conflict with the name of your model or something like this. Have you tried a different name for the variable passed to the template? Commented Feb 7, 2020 at 11:46

2 Answers 2

1

as return do this:

return render(request, 'home.html', locals())

then in html:

{% for predmet in all_subjects %}
    {{ predmet.ime }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

5 Comments

still does not display data
@ŠimeGalac then check if there is some data in database related to this model or if you are authenticated.
there is data and I am authenticated, hate it when I stuck on trivial stuff
@ŠimeGalac debug if you can add some html text inside loop, and check if you see it. If yes then print all_subjects in you view.
obviously, data is not in the all_subjects list
0

found the problem, it was beacuse in settings.py I used home.html as django default TemplateView

path('', TemplateView.as_view(template_name='home.html'),  name='home'),

did this:

path('upisnilist/', views.get_all_subjects, name='predmeti' )

and works fine

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.