Writing Your First Blog App In Django-Part6

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

In this post we will see how to set up static files in our project.Static files are things like images,css files or javascript files that we can server up to the client the browser.In django it’s not as simple as say suppose we want to create an image so we can add an image tag to html file and path to that image provided,but that is not going to work in our project.We have to explicitly set static images up inside our project and in our case we are going to tell django to serve up our images now.

So for doing that first go into blog/urls.py and tell it to serve static files as following:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('articles/', include('articles.urls')),
    
]

urlpatterns +=staticfiles_urlpatterns()

Now after this change in the settings.py file at the end add the following lines of code:

STATICFILES_DIRS = (
    #use this to join two paths
    #first path is BASE_DIR.it will grab the base directory of our project 
    #second path is folder inside this base directory where we will store our static files
    os.path.join(BASE_DIR, 'assets')
)

So we will create assets folder inside base directory of our project which will contain all css,js files of our django project.

for example create style.css file inside css file and add this code inside that file:

body {
    background-color: blue;
}

Now we have our static css file,how do we connect it to our html template articles_list.html file?

As a simple way we can add below line under the head section in articles_list.html file:

<link rel=”stylesheet” href=”/static/style.css”>
This will work for us,but this is not the right way to do in django. The better way to use static files inside in django project is to use something that comes baked with django and we can load that into the html template by using template tags as following:
<!-- So right here we are saying that loading this static thing which we can use now
  inside this template to form our static url.So that if we do ever change that url 
 in the settings then this thing right here will work it our for us.We don't need  
  to go around every template and start to rechange all of the href attributes.All
  the source attributes.It is all taking care for us.


-->
{% load static from staticfiles %}

<!DOCTYPE html>
<html>

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

<body>
    <h1>Articles List</h1>
    <div class=" articles ">
        {% for article in articles %}
        <div class="article ">
            <h2><a href="# ">{{ article.title }}</a></h2>
            <p>{{ article.snippet }}</p>
            <p>{{ article.date }}</p>
        </div>
        {% endfor %}
    </div>


</body>

</html>

change the style.css file as following to make page more interactive:

body{
    background-color: #0f121f;
    background-image: url(/static/stars.png);
    background-repeat-y: no-repeat;
    color: #fff;
}

body *{
    font-family: Ubuntu;
}

h1, h2, h3, h4, h5{
    font-weight: normal;
    margin: 0;
}

a, a:hover, a:visited{
    color: #fff;
    text-decoration: none;
}

Note:You can download the image from here: https://raw.githubusercontent.com/iamshaunjp/django-playlist/lesson-12/djangonautic/assets/stars.png  and add it to assets folder.

After that run the server,you will be able to see the following screen which is looking better than earlier.

Writing Your First Blog App In Django-Part6


This is all about how to add static file(css files,js files,images) inside your django project and to load them into your html template.

Leave a Comment

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