css js and images are not loading on django p2 best method
css js and images are not loading on django p2 best method
If your css js and images are not loading on django website then you definitely need to go through this post thoroughly. So let’s get started. What If you made All the frontend files and want to create a Django backend for that website and getting errors which is generally happen, checkout this link to solve all other problems, so first go and see those solved problems from here: link frontend website with django backend p1.
First open your settings.py file ( if you don’t know what and where the folder is then first see the linked frontend website with django backend p1 post first) and go at the bottom of the page, there you need to add two lines of code. Which is below:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
If you saw the first line on that file anywhere, you can only paste the second line of code, otherwise you can paste both of them. After press ctrl+s to save the file, and close the file. Now in your project folder and open urls.py file there you need to add the below highlighted section to the file:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path(admin/', admin.site.urls),
path('',include(.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now go to your app folder ( if you don’t know what app folder is then first see the linked link frontend website with django backend p1 post first) and there make a new folder called static there you have to make three folders called css, js and images you have to paste all the css files in the css folder and all javascript files on js folder and all images to the images folder so that everything will be organized on each folder.
Now go to your templates( html files ) and there on the top you need to provide a piece of code so that every thing of the static files can be accessible for the template. The code is {% load static %} this is a short django template code that will load static links on your template.
Now where you have loaded the link tag there you only have to provide the location as /static/css/.css/ and same in your javascript files as /static/js/.js/ you have to do the same on the images, in href you need to provide /static/images// this will load all of your css, js and images to your templates.
This will solve the problem of not loading those items on your template.
I you didn’t see the first part of Django backend problem solving link frontend website with django backend p1 then go and see that first.
other posts to check out: Beautiful Website Making Tip #2
django docs: https://docs.djangoproject.com/en/3.1/
Hope to see you on another post.
Well composed articles like yours renews my faith in today’s writers.You’ve written informatiokn I can finally agree on and also use.Many thanks for sharing.
Thank you for the feedback. I will be working for making articles more attractive.
Please keep us up to date like this. Thanks for sharing…
Very good info. Lucky me I discovered your blog by accident.
I have book-marked it for later!
You need tto take part in a contest for onee oof the most
useful websites online. I will recommend thiss site!
thanks for the feedback. I’ll be looking at your recommendation.
Would love to perpetually get updated outstanding web blog!
Wow, this article is nice, my sister is analyzing sucdh things, so I amm going to
inform her.
Hello! I ish to say that this ost is awesome, great written and come wioth approximately all important infos.
I’d like too look extra posts like this! 🙂
Hello there! Would you mind if I share our
blog wit my twitter group? There’s a lot of people that I think would really appreciate your content.Please let me know.
Thanks!
Yes,of course you can share that.
Great post! We will be linking to this great post on our website.
Keepp up the great writing.