Writing Your First Blog App In Django-Part17

We will continue to build our blog app where we left in the post writing-your-first-blog-app-in-django-part-16.

In this post we will see how to protect the pages that will require login. Upto now we have the user signup,user login and user logout.Now the whole idea of authentication is so a user can sign in and then view pages that a person who’s not signed in couldn’t write in.We are authenticating user so they can do additional things so.Say for example a user can add articles to this list now we only want user who are logged in and authenticated to be able to view that page where a user can add an article.So we could say that login is required for that page.Okay i want to add an article and view that form to add a new article

So in this post we will create a template for a user to add a new article then we will protect that page.We will say if you want to view this page
you need to be logged in.

So first change the articles/urls.py file as following to add a url for creating article.

from django.urls import path
from django.conf.urls import include,url
from . import views

app_name = 'articles'

urlpatterns = [
  
  #path('',views.articles_list),
  #path('',views.article_detail),
  url(r'^$',views.articles_list,name="list"),

  #url to create a new article
  url(r'^create/$',views.article_create,name="create"),

  #a named capturing group
  #To create a named capturing group we have to say ?P
  #In angular brackets we have to write the name of the thing we want to capture
  #\w:any type of letter,underscore
  #^:start
  # $:end
  # - means: - can also be included in url
  # + means: that thing before it can be of any length
  url(r'^(?P<slug>[\w-]+)/$',views.articles_details,name="detail"),

]

After that change the articles/views.py file as following to add a article_create view function which will just redirecting to article_create template.

from django.shortcuts import render
from .models import Article
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required

'''
Takes in the request object.
All we are going to do in this function is we will render a template
     to the user which is going to show a list of articles.
First parameter in render is always the request object and the second parameter is
     the template we want to render and third optional is the directory
'''
def articles_list(request):
    #Retrieve all articles from database and sort them according to date
    articles = Article.objects.all().order_by('date')
    return render(request, 'articles/articles_list.html',{'articles':articles})

def articles_details(request, slug):
    #return HttpResponse(slug)
    article = Article.objects.get(slug=slug)
    return render(request,'articles/article_detail.html',{'article':article})

#if the user in not already logged in,then we have to first send the user to the login page
#so we will do that with the help of decorators in django
@login_required(login_url="/accounts/login/")
def article_create(request):
    return render(request, 'articles/article_create.html')

After that make one template named article_create in the template directory inside template directory inside articles app and add this content.

{% extends 'base_layout.html' %} {% block content %}
<div class="create-article">
    <h2>Create an Awesome New Article</h2>

</div>
{% endblock %}

So now run the server,and go to the localhost/articles/create/ page.It will open the page.The logic for creating new article,we will add  in next post.


This is all about how to add an article create url and views in our app.

Leave a Comment

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