Template switching issue

Hi, Folks. I hope the week is treating you all well.
I’m a bit confused on why something isn’t working and was hoping for some feedback.

The setup is thus:
I’ve got a base.html file and a blocks.html file. blocks.html obviously extends base.html.
I also have a results.html file.
As of right now, while I experiment around, both results.html and blocks.html have the exact same content. The both define all blocks for base.html exactly the same EXCEPT for one block - {% block front %}.

in blocks.html it looks like this →

{% block front %}
<div class="row align-items-start py-5">
{% for i in range %}
    <div class="col-4">
        <div class="card">
            <video controls>
                <source src="" type="video/mp4">
            </video>
            <div class="card-body text-center">
                <h3 class="card-title h5"><a href="article.html">Livestreaming</a></h3>
                <div class="card-text">
                    Someone Livestreaming something from somewhere at sometime with someone involved
                </div>
            </div>
        </div>
    </div>
{% endfor %}
</div>
{% endblock front %}

in results.html it looks like this →

{% block front %}
    <div class="row align-items-start py-5">
        <div class="col">
            <h4>Mainstream Results: </h4>
        </div>
        <div class="col">
            <h4>Fringe Results: </h4>
        </div>
    </div>

    <!-- loop block -->
    <div class="row align-items-start py-2">
        <div class="col">
            <h5>Searched version: {{ search_string }}</h5>
        </div>
        <div class="col">
            <h5>Opposing version: {{ fringe_string }}</h5>
        </div>
    </div>
{% endblock front %}

my views.py is this →

def index(request): 
    template = 'searchnet/blocks.html'
    context = {
        "location": location,
        "weather": weather,
        "icon": icon,
        "menu": [
            .........
            ],
        "range": range(3),
        }
    return render(request, template, context)


def results(request, search_string):
    template = 'searchnet/results.html'
    context = {
        "search_string": search_string,
        "fringe_string": search_string[::-1]
    }
    return render(request, template, context)

And my urls.py is this →

urlpatterns = [
    path('', views.index, name='index'),
    path('search/<str:search_string>/', views.results, name='results'),
]

The search bar is a block that looks like this →

{% block search %}
    <form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
        <input type="search" name="search" method="GET" class="form-control" placeholder="Search.. Anything, Anytime, Anyone, Anywhere, Anyhow" aria-label="Search">
    </form>
{% endblock search %}

My confusion is that if I type something into the search bar the url changes to display the search term but results.html isn’t rendered. It stays on blocks.html; but like I said, the url bar reflects the change just the templates don’t change.

Can someone breakdown what’s happening here? I feel inside I’m missing a small link in the chain but I can’t put my finger on it. Thanks to all replies

Your search bar is going to post to whatever view rendered it.

If you rendered views.index containing that search bar, then the post from the search bar is going to post back to views.index.

But you’re not showing enough of the context around this issue to be sure. (You haven’t shown the complete templates nor explained how that search block fits into anything.)

You’re also not handling the difference between a POST and GET request.

What I suggest at this point is that you take a step back and work your way through either the Official Django Tutorial or the Django Girls Tutorial to gain an understanding of how forms, views, urls, and templates all work together to build the pages to be rendered and presented to the browser.

The way I picture it happening is a term is put into the search bar (rendered by views.index), press enter, and views.results is loaded, rendering an identical template as views.index except that the {% block front%} is different

The template is as follows →

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

<!-- weather -->
{% block weather %}
<div class="text-color-third">{{ weather }} in {{ location }}</div>
{% endblock weather %}

<!-- register -->
{% block register %}
<a class="btn btn-sm btn-outline-secondary" href="#">Register</a>
{% endblock register %}

<!-- menu -->
{% block menu %}
<div class="nav-scroller py-1 mb-2">
    <nav class="nav d-flex justify-content-between">
        {% for category in menu %}
        <a class="p-2 text-color-primary" href="#">{{category}}</a>
        {% endfor %}
    </nav>
</div>
{% endblock menu %}

<!-- search -->
{% block search %}
    <form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
        <input type="search" name="search" method="GET" class="form-control" placeholder="Search.. Anything, Anytime, Anyone, Anywhere, Anyhow" aria-label="Search">
    </form>
{% endblock search %}


{% block front %}
<div class="row align-items-start py-5">
{% for i in range %}
    <div class="col-4">
        <div class="card">
            <video controls>
                <source src="" type="video/mp4">
            </video>
            <div class="card-body text-center">
                <h3 class="card-title h5"><a href="article.html">Livestreaming</a></h3>
                <div class="card-text">
                    Someone Livestreaming something from somewhere at sometime with someone involved
                </div>
            </div>
        </div>
    </div>
{% endfor %}
</div>
{% endblock front %}

the one rendered by views.results is exactly the same except the front block. And I know this isn’t the ideal way to do things, I’m playing around trying to get use to how data moves through django projects.

I took this suggestion when you made it to me before. I’ve gone through the Django Girls one. I was actually just fiddle farting around, playing with some things I learned in that one when I encountered this issue. I’ll probably start the other one this weekend

Sorry, that’s not how it works.

A view is a function that receives a request and returns a response.

Which view is called is determined by the url of the request.

Then you’ve already seen on the Django Forms section how a post is submitted to a view, and how you handle a form submission. That’s what you’re trying to do here.