Home > database >  Django variable doesn't get passed to template
Django variable doesn't get passed to template

Time:08-04

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.

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 looks like this:

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

        <!-- logo and logo spacers -->
        <div ></div>
        <div ></div>

        <!-- Sign Up Button -->

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

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

    <!-- main body of login page -->
    <div >

        {% if user_data %}
            <p>We find users</p>
            {% for account in user_data %}
                <div>
                    <a  href="{% url 'user_profile' username=user.0.username %}">
                        <!--<img  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 >

        <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'),
]

CodePudding user response:

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

  • Related