How to Setup Django Static files in Django Project and Application
June 14, 2021 2022-12-19 3:09How to Setup Django Static files in Django Project and Application
How to Setup Django Static files in Django Project and Application
How to Setup Django Static files in Django Project and Application
Why need Django Static Files
Setup Django Static files in Settings.py file
To Setup Django Static files are very important because without static files you cannot archive targets in the project. So you have to manage static files in the Django project. There are some configurations settings you have to follow:
In INSTALLED_APPS, you have to mention django.contrib.staticfiles, but you don’t know it will be included by default, if you don’t find then you have to add.
In your settings.py file, define STATIC_URL as;
STATIC_URL = ‘/static/’
After doing this you have to create a folder named ‘static’ in which all the files JS, JQ, CSS, Images will be stored there. When you create a folder you have to give a path in your Django settings.py file. For example
STATIC_DIR = os.path.join(BASE_DIR, ‘static’)
STATICFILES_DIRS = [ STATIC_DIR ]
Above two lines of code, you have to mention in your setting file to use a static folder as you created in the root folder.
Use of {% load static %} tag in Django template
If you want to use static files like CSS, Images, etc you have to load static first at the top of the Django HTMLÂ Template. Without loading static you cannot use any static files on your web page you will get an error. If you want to insert an image then you have to load the static file at the top and also mention with image path as shown in the example:
You have to follow this pattern {% static ” %}, in the single quotes you have to give a file path so it will proper
Conclusion
As you have read an article related to Setup Django Static files, after that you can manage all static files that are related to your Django Project.
If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
Recommended Django Tutorial for Beginners
Django Template Inheritance with Examples | Explanation
How to Add Django Website Templates in Django Project and Application
Ultimate Guide | Creating Application in Django | Django commands
Django Form Validation | Complete Django Tutorials
Django Forms Widget | Complete Discussion | Django Tutorials