Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django variable doesn't get passed to template

Basically, what happens here, the list user_data doesn’t get passed to the template, I can’t figure out why. Does anyone have any ideas please? When I use the search bar I just get the else condition from the template, and it never shows any variable. I also tried to create different variables to pass to the template, but none of them got passed for some reason… Thank you very much!

This is in my views.py

@login_required(login_url='login_user')
def profiles(request):
    context = {}

    if request.method == "POST":
        data = request.POST.get('search_input')
        if len(data) > 0:
            username_result = NewUser.objects.filter(username__icontains=data).distinct()
            email_result = NewUser.objects.filter(email__icontains=data).distinct()
            user_data = []
            for account in username_result:
                user_data.append((account, False))
            context['usernames'] = user_data

            return render(request, "profiles/search_user.html", context)
         else:
            return render(request, "profiles/profiles.html")
    return render(request, "profiles/profiles.html")

Then my template look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% extends 'profiles/base.html' %}

{% block title %}One For All{% endblock %}
{% load static %}
<!-- importing css file -->
{% block style %}<link rel="stylesheet" type="text/css" href="{% static 'css/search_user.css' 
%}">{% endblock %}


{% block content %}
    <!-- navigation bar -->
    <div class="navBar">

        <!-- logo and logo spacers -->
        <div class="logo_spacer"></div>
        <div class="logo"></div>

        <!-- Sign Up Button -->

        <a class="homeBtn" href="{% url 'profiles' %}">Home</a>
        {% if is_self %}
            <a class="settingsBtn" href="{% url 'settings' user=user.username %}">Profile Settings</a>
        {% else %}
            <a class="settingsBtn" href="{% url 'user_profile' username=user.username %}">My Profile</a>
        {% endif %}

        <p>Top Menu</p>
    </div>

    <!-- main body of login page -->
    <div class="main">

        {% if user_data %}
            <p>We find users</p>
            {% for account in user_data %}
                <div>
                    <a class="profile-link" href="{% url 'user_profile' username=user.0.username %}">
                        <!--<img class="img-fluid profile-image" src="{{account.0.avatar.url}}" alt="">--></a>
                </div>
            {% endfor %}
        {% else %}
            <p>This is when user_data doesn't exist or doesn't get passed to template: {{ user_data }} </p>
            {{ user_data }}
        {% endif %}
    </div>

    <div class="bottom">

        <p>Bottom</p>
    </div>
{% endblock %}

{% block js_block %}
{% endblock %}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('logout/', views.logoutUser, name='logout'),
    path('post/', views.post, name='post'),

    path('', views.profiles, name='profiles'),
    path('search_user/', views.profiles, name='profiles'),
    path('UserProfile/<str:username>/', views.user_profile, name='user_profile'),
    path('Settings/<str:user>/', views.settings, name='settings'),
]

>Solution :

In the view you set:

context['usernames'] = user_data

But in the template you reference:

        {% if user_data %}
            <p>We find users</p>
            {% for account in user_data %}

user_data doesn’t exist in the context – you need to reference usernames instead, or change the view to call it user_data

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading