Correct way to upload image to website

Hey. I am new to Django and web development. Slowly learning how to build my website. In my app project, I have created a static folder where I keep my css and images. I want to know what is the correct way to upload images:

See the image above. I am able to upload image using 2 methods. One using the static method and the other one just providing the full path which in this case is static/baltu_vila_logo.png

What are the differences of these 2 methods and which one to use?

I’m a bit confused by what you’re trying to ask.

Generally speaking, the term “upload” is describing the process of sending files from the client to the server, neither of which involve either of the forms you’ve presented here.

Either of those two methods you’ve shown are ways to display images already existing on the server, on a web page. Of those two options, using the {% static ... method is far superior than trying to code full paths. (It gives you a lot more flexibility when deploying your project to a production environment.)

Ken

Thanks for the reply. Yes I have meant just displaying the images on the website. The tutorial that I have seen initially said that I need to use :
{% load static %}

at the beggining of my index.html to use static images and etc. I have commented out that line of code and my image still displays without any problems. Could you explain me a little bit what {% load static %} is used for?

Documentation for the static tag

When you’re posting code here, please do not post images. They’re not readable on every device. Post the actual code, between lines consisting of three backtick (`) characters. (That means you’ll have a line of ```, your code, and then another line of ```. Make sure you use the backtick - `, and not the apostrophe - '.)

Ken

{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!">

I am able to display my static image without

{% load static %}

Please post the complete template.

What do you refer to as a template?
My project structure is :

I have started an app named “hello”. Inside hello I have created a “static” fodler where I keep the css and images

my index.html:

<!DOCTYPE html>
<!--{% load static %}-->

<link rel="stylesheet" href="{%static 'css/main.css' %}">
<html>
	<head>	
		<title>Baltu vila</title>
	</head>
	<body>
	<div class="hero-image">
	 	<div class="hero-text">
	    	<h1>test</h1>
	    	<p>test</p>
	  	</div>
	</div>
	<img src="{% static "baltu_vila_logo.png" %}" >
	<h1> hello world12</h1>

	</body>


</html>

In my settings.py project folder I have added a line:

STATICFILES_DIRS = ["C:/Users/Lukas/Desktop/dev/django/django_app/hello/static",
]

Your index.html file is a Django template. It’s not sent to the browser as-is, it’s processed by a template engine to convert it to the html that is actually sent to the browser.

As a result:

does not comment out the load static. The template engine is still interpreting that line, and using it to load the static templates.
What you’ve done is place it within an html comment, which doesn’t really “mean” anything in the context of the rendering engine.

If you wish to comment out that line for the rendering engine, you either need to use the comment syntax or the multiline comment format.

1 Like