Writing Your First Blog App In Django-Part21

We will continue to build our blog app where we left in the post “Writing Your First Blog App in Django-part 20

So we have most of functionalities implemented like signup,login,post articles,but there is one thing bothering me and that is i always see the logout button at the top.The logout button appears irrespective of whether we are logged in or not,but now we should show some dynamic information depending on whether the user is logged in or not.So if we are logged in we will be able to see logout button and will also want to see a button which will create new article,and if we are logged out we should not see any of those buttons instead we should see the login and signup buttons.

So to do that we need a way in template in django to detect whether the user is logged in or not,show that we can show them different content depending on the state.

So our goal is if the user is logged in then we will show him two buttons,one for logout and one for creating logout,and if he is not logged in then we will show him two buttons,one for login and one for signup.

So to do changes according to above change the base_layout.html file as following:

{% load static from staticfiles %}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Djangonauts</title>
    <link rel="stylesheet" href="{% static 'style.css' %}">
</head>

<body>
    <header class="wrapper">
        <h1>
            <a href="{% url 'articles:list' %}"><img src="{% static 'logo.png' %}" alt="djangonautic" /></a>
        </h1>
        <nav>
            <ul>
                {% if user.is_authenticated %}
                <li>
                    <form class="logout-link" action="{% url 'accounts:logout' %}" method="post">
                        {% csrf_token %}
                        <button type="submit">Logout</button>
                    </form>
                </li>
                <li><a href="{% url 'articles:create' %}" class="highlight">New Article</a></li>
                {% else %}
                <li><a href="{% url 'accounts:login' %}">Login</a></li>
                <li><a href="{% url 'accounts:signup' %}">Signup</a></li>
                {% endif %}
            </ul>
        </nav>
    </header>
    <div class="wrapper">
        {% block content %} {% endblock %}
    </div>
</body>

</html>

Now you can check the changes by running the server.After this change we will to show articles list on the http://127.0.0.1:8000/ url only.
Till now we are showing articles list page on http://127.0.0.1:8000/articles/ url.

So to do this we have to change our root urls. So first go to blog/urls.py file and change it according to this:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include,url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
from articles import views as article_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('articles/', include('articles.urls')),
    path('accounts/', include('accounts.urls')),
    url(r'^$',article_views.articles_list,name="home"),
]

urlpatterns +=staticfiles_urlpatterns()
urlpatterns +=static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

You can see here we are directly importing the articles_list view function form articles app.So first we have to import views file from articles app.You can also change the logo in the base_layout.html file such that whenevar user clicks on logo it will open that home page.So change it like this:

{% load static from staticfiles %}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Djangonauts</title>
    <link rel="stylesheet" href="{% static 'style.css' %}">
</head>

<body>
    <header class="wrapper">
        <h1>
            <a href="{% url 'home' %}"><img src="{% static 'logo.png' %}" alt="djangonautic" /></a>
        </h1>
        <nav>
            <ul>
                {% if user.is_authenticated %}
                <li>
                    <form class="logout-link" action="{% url 'accounts:logout' %}" method="post">
                        {% csrf_token %}
                        <button type="submit">Logout</button>
                    </form>
                </li>
                <li><a href="{% url 'articles:create' %}" class="highlight">New Article</a></li>
                {% else %}
                <li><a href="{% url 'accounts:login' %}">Login</a></li>
                <li><a href="{% url 'accounts:signup' %}">Signup</a></li>
                {% endif %}
            </ul>
        </nav>
    </header>
    <div class="wrapper">
        {% block content %} {% endblock %}
    </div>
</body>

</html>

So now run the server,you will be able to see the articles list page on localhost only.


So this is all about how to check the login status and redirecting the home page.

Leave a Comment

Your email address will not be published. Required fields are marked *