Adrian Grzywaczewski
Adrian Grzywaczewski

Reputation: 888

Correct structure for a Django application and handling the views and sub views

I struggle a bit in order to understand how a Django Application should be properly structured. In particular, I struggle to get my head around the views.

So I have this file

base.html

And I know that this file will have common elements of the whole application such as footer, navbar, head etc.

So far so good. Inside my base.html file I will have the tag:

{% block content %}
{% endblock %}

Which handles the dynamic content of the application. Now I create a static page called home.html and inside that file I put

{% extends 'base.html' %}
... Whatever I want
{% block content %}

And now comes the part that I really struggle to understand how it works. I create a new app called dashboard. I create a dashboard.html file and similarly to home.html I extend the base, and but elements inside the content block. In the views.py of the generated application I define:

@login_required(login_url="login/")
def dashboard(request):
    return render(request, "dashboard.html")

and the dashboard.html

{% extends 'base.html' %}
{% block content %}
    <div class="dashboard-page">
        <div class="row equal-sides">
            <div class="col-sm-2 trim-right">
                {% include 'dashboard_navigation.html' %}
            </div>
            <div class="col-sm-10 trim-left">
                <div class="dashboard-main card">
                   <h3>Welcome to your dashboard</h3>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

enter image description here

Happy days. Now I want to reload only the main container on the right-hand-side, depending on the selection from the navigation bar on the left-hand-side. How do I achieve that?

I thought that if I will add a new url inside my urls file and a corresponding view inside views.py file it will be all good.

def single_search(request):
    tweets = get_tweets()
    context = {'tweets': tweets}
    return render(request, "search.html", context)

urls.py

url(r'^dashboard', views.dashboard, name='dashboard'),
url(r'^dashboard/search$', views.single_search, name='search'),

However, what I get is the same page as the dashboard.html enter image description here

I managed to go around the problem and display "proper" content with the following code:

{% extends 'base.html' %} {% block content %}
    <div class="dashboard-page">
        <div class="row equal-sides">
            <div class="col-sm-2 trim-right">
                {% include 'dashboard_navigation.html' %}
            </div>
            <div class="col-sm-10 trim-left">
                <div class="dashboard-main card">

                    {% if 'search' in request.path %}
                        {% include 'search.html' %}
                    {% else %}
                        <h3>Welcome to your dashboard</h3>
                    {% endif %}
                </div>
            </div>
        </div>
    </div> {% endblock %}

But I know that it is not the proper way of doing it. I also know that the context that I am passing is not being returned due to the fact that it is inside the include 'search.html' tag. If I pass the same context inside the dashboard it works both in the search.html and dashboard.html unfortunately I only want to see that content in if the user is on the search.html subpage.

Additionally, I append the tree of my application:

enter image description here

Upvotes: 0

Views: 153

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

The problem is simply that you haven't terminated your dashboard URL pattern, so it matches both URLs. It should be:

url(r'^dashboard$', views.dashboard, name='dashboard'),

If you find regexes confusing, the new path syntax in Django 2.0 might be easier.

Upvotes: 2

Related Questions