How to render an image in Django?

Viewed 38

I am using the latest version of Django and I am having problems rendering my images. My img tag doesn't work so I googled it and the problem seems to be the static files. So I followed some tutorials online but none of them worked for me. It will be awesome if you could provide me with some guidance, thanks. Path of my static folder Do I need to move my static folder else where, do I need to add some line of code?

Path of my index.html

Below is my html code...

{% extends 'landing/base.html' %}

{% load static %}

{% block content %}
<!DOCTYPE html>
<html>
<head>
  <style>
    html,body{overflow-y: scroll; }

          h3 {
              font-weight: lighter;
          }
          .btn-space {
              margin-right: 5px;
              margin-left: 5px;
          }

          h1{
              font-size: 75px;
              font-weight: bold;
              background: -webkit-linear-gradient(#00ffd0, #c493ff);
              -webkit-background-clip: text;
              -webkit-text-fill-color: transparent;
              padding-top:13%;
              justify-content: center;
              font-family: 'Lato', sans-serif;
          }

          p{
              background: -webkit-linear-gradient(#ffffff, #4287f5);
              -webkit-background-clip: text;
              -webkit-text-fill-color: transparent;
              padding:15px;
              display: flex;
              justify-content: center;
              font-family: 'Lato', sans-serif;
          }
          .c{
            font-size: 15px;
            background: -webkit-linear-gradient(#ffffff, #3c3b3d);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
          }

          body {
            background-color: black
            /*background-image: url('a/circle.svg');
            background-size: cover;*/
          }
          .d {
            font-size: 25px;
            background: -webkit-linear-gradient(#ffffff, #c493ff);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
          }
          .e {
            padding-left:  20%;
            padding-right:  20%;
            text-align: center;
            background: -webkit-linear-gradient(#ffffff, #f57842);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
          }
          .f {
            background: -webkit-linear-gradient(#c493ff,#f54242);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
          }
          hr{
            height: 1px;
            background-color: white;
            border: none;
        }
        .img:hover {}
  </style>
</head>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<!--hhjhhhkjhgft6yggfre5r678uihjggcfxsre6r68oyigjhcgxg-->
<div class="container bady">
  <div class="row justify-content-center mt-5">
      <div class="col-md-10 col-sm-12 text-center">
          <h1 class="">Anonymously Connect With People</h1>
          <div>
          <p class="mt-3 d">Stay Up To Date On The Latest Scoops And Join Conversations With Your Friends!</p>
          <a href="{% url 'account_login' %}" class="btn btn-outline-light mr-2 btn-space btn-lg">Login</a>
          <a href="{% url 'account_signup' %}" class="btn btn-outline-light btn-space btn-lg">Register</a>
          <p class="c">Join The Social Activities Club</p>

        </div>
          <div class="d-flex justify-content-center mt-5">
          </div>
      </div>
  </div>
</div>
  <p>
    <img src="{% static "images/Tawsk.jpg" %}" alt="home" />
  </p>
</html>

{% endblock content %}

1 Answers

Configure STATIC_URL and STATICFILES_DIRS in settings.py.

For ex :

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "socialnetwork/static/"),
]

Please refer the docs here https://docs.djangoproject.com/en/3.2/howto/static-files/

Related