DEV Community

Cover image for Learn Django from scratch with a small project _2
Yahaya Kehinde
Yahaya Kehinde

Posted on • Updated on

Learn Django from scratch with a small project _2

In the previous post, we created our first "Hello World". However, In this post we are going to be creating a new django template within which we will render our HTML page. We would need to refactor our previous code a bit to achieve this.

First we are going to create a new folder called 'templates' within our root directory which is our date_project. In order to remember this, just know that templates should be created in the same directory as our "" file. Within this, we will create our new "index.html" file.

Alt Text

Now we need to inform django that we are going to be using templates and let django know about the location of our templates.

Go to "" and under "TEMPLATES", add this within the DIRS,

  'DIRS': [os.path.join(BASE_DIR, 'templates')],
Enter fullscreen mode Exit fullscreen mode

Next, we will insert the content of our index.html, which I'll be copying and pasting from my original site.

It contains a lot of CDN links used for animations and as such, we need to change some of the code.

But first, we need to edit our views to indicate that we have a new HTML file we want to render. Delete these lines:

from django.http import HttpResponse

return HttpResponse('Hello world')

Enter fullscreen mode Exit fullscreen mode

replace the with:

from django.shortcuts import render

# Create your views here.
def index (request):
    return render(request, 'index.html') 
Enter fullscreen mode Exit fullscreen mode

Your HTML page should display nicely now. This is what i have now. However, my CSS file and images are not displaying. This is because we also need to render the static files.

Alt Text

First we create a new folder called 'static' within our date_app. Create another folder named "css" within which, we put our css files and another folder named "images" containing our image. For emphasis, always remember that the “templates” folder is created within the same directory as our “” file while the “static” folder is created within our date_app.

Next,within our HTML page, we need to refactor the link to our stylesheet

 <link rel = "stylesheet" href = "{% static 'css/style.css' %}" />

Enter fullscreen mode Exit fullscreen mode

and also edit the path to our images:

 <img src="{% static 'images/amy-shamblen-pJ_DCj9KswI-unsplash.jpg' %}" alt="" >

Enter fullscreen mode Exit fullscreen mode

Lastly, we need to remember to inform Django to load static files at the top of the page.

This is what my final HTML page looks like:

{%load static%}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doctors' Connect</title>
    <link rel="stylesheet" href="" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

     <!-- Font awesome -->

     <!-- scroll reveal -->
     <script src=""></script>

     <!-- animate css -->

     <!-- poppins font -->
     <link href='' rel='stylesheet'>

    <!-- Animate on scroll -->
    <link href="" rel="stylesheet">

    <!-- Css files -->
    <link rel = "stylesheet" href = "{% static 'css/style.css' %}" />

  <div class="navbar">
    <span class="navbar-brand mb-0 h1">
        <h2 class = "text-danger font-weight-bold">Doctors' Connect</h2>

      <div class="container-fluid container intro">
          <div class="row">
              <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 intro-text" >
                <h2>Finding love can be really difficult...</h2>
                <h3>However, we try to make it easier</h3>
                <h4>Want to find out more?</h4><br>
                <a class="btn btn-danger btn-lg" href="main/main.html" role="button">GET STARTED</a>
              <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 intro-img" >
               <img src="{% static 'images/amy-shamblen-pJ_DCj9KswI-unsplash.jpg' %}" alt="" data-aos="flip-left"    data-aos-easing="ease-out-cubic"

<div class = "text-center" data-aos="zoom-out-up" data-aos-duration="2000">
    <h6>Designed and built by Dr Yahaya H.K</h6>
    <h6>All rights reserved, 2020.</h6>

<script src="" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

<script> = ScrollReveal();
      sr.reveal('.intro-text', {
    duration: 2500,
    origin: 'bottom',
    distance: "300px",
    viewFactor: 0.3,

<!-- animate on scroll js -->
<script src=""></script>

<!-- Animate on scroll init -->

<script src = "script.js"></script>

Enter fullscreen mode Exit fullscreen mode

Now run your server again with

python runserver
Enter fullscreen mode Exit fullscreen mode

Congratulations, we have now fully rendered the page and our website should display nicely now.

Alt Text

Top comments (0)