Django Tutorial #12 - Static Files & Images
Вставка
- Опубліковано 26 лис 2017
- Hey ninjas, in this django tutorial I'll explain how we can set up our django project to use static files such as images, css and JavaScript files.
DONATE :) - www.paypal.me/thenetninja
----- COURSE LINKS:
+ Python tutorials - goo.gl/xD2AvX
+ Course files - github.com/iamshaunjp/django-...
+ Django docs - docs.djangoproject.com/en/1.11/
+ Atom editor - atom.io/a
+ CMDER - cmder.net/
======== Other Tutorials =========
----- NODE.JS TUTORIALS
• Node JS Tutorial for B...
----- MONGODB TUTORIALS
• MongoDB Tutorial for B...
----- SUBSCRIBE TO CHANNEL - / @netninja
======== Social Links ==========
Twitter - @TheNetNinja - / thenetninjauk
Patreon - / thenetninja
If you're running Django 3 and were on an older version before, you need to replace:
{% load static from staticfiles %} or {% load staticfiles %}
with just:
{% load static %}
thanks man!
Thanks Elias.
Thanks man
this comment needs to be pinned.
That’s true
in DJANGO 3 , in settings.py use the following STATICFILES_DIRS = [
BASE_DIR / "assets",
]
Not all heroes wear capes.
bro just wanted to say that 6 years later, your django tutorials are still the best around! 🤗
I appreciate that! :) thank you
if the file doesn't show up, please make sure you added the finishing coma in the tuple
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'assets'),
)
this is in the settings.py
Django seem to not recognize the "os" part. It has a yellow sqiggly line underneath... it says "os" is not defined ???? help?
@@VS257 Import os in settings.py file
@@VS257 STATICFILES_DIRS = [
BASE_DIR / "assets",
]
AWS/Django for production mini-series +1
I agree
Please Shaun
🙏
it would be awesome
Please sir do it
Okay, this is indeed the BEST DJANGO tutorial in the universe. Thanks a lot for creating series.
Thanks so much! :)
u are awesome :) i was so confused and scared about django but the more i advance the more it getting clear and simple thank u , keep creating such perfect series plz :)
I really like your tutorial videos. Each video/Topic is short, and gets straight to the point. I know this version may be a little outdated, but I prefer to go by your videos!
Your communication skills are the best!! Short, sweet and to the point. Thank you.
Thank you very much for this series!
I'd like to add that I'd be very interested in a Django for production + AWS series.
FIX: in templates {% load static %} in link href="{% static "styles.css" %}"
Thanks man! ;)
Thank you. Use
href="{% static 'styles.css' %}"
to avoid escaping the double quotes though
@@JJnnaatt thanks a lot, does the job neatly :)
I'm still going, but this series is exceptional. I have been using Django for about 2 years by mostly modifying the starter app that comes in Visual Studio (haters gonna hate) but I never understood it. This has filled in all the blanks. Can't thank you enough.
Thank you!! I searched many information, so I find this! I thougt about give up, but with your help I may continue my hobby!
Thanks for the tutorials!
I got some question, how do I access another static file in another directory from my css file? the curly braces seems to cause trouble when I used {% load static %}
would be possible to symlink any folder in my system to the myapp/static folder?
Is the font 'Ubuntu' standard or do you have to install it?
What a great tutorial, thank you. Succinct, clear and quick!
I had three issues that others may have had.
1. You need to "import os" at the top of the settings.py file
2. When trying to access my admin site after these changes I had an 'incorrect padding' error. I found a solution on stack overflow - you need to download a more recent version of Django (pip install django==3.1.1)
3. {% load static from staticfiles %} did not work. I found a quick fix on stackoverflow. Just change it to {% load static %} and it works really well.
Beautiful tutorials!!!!! I just got one issue, everything seems to work but, I didn't get the same font style. Mine is sort of like Times New Roman. Please help! :(
And what if it's not in the BASE_DIR ?? Like I have it in a separate folder called polls
But how would you display a picture on the app (instead of it being a background)?
Excellent tutorials ! loved it !!
Does this work after the Django app has already been deployed?
what's up dude? I have a question.
How do we use static files (css file for example) that is intended only for a specific app? Where do we put these static files?
sir please can you tell how to use images in static css file for background in django?
best tutorial I've ever seen, I'm loving it.
I am not able to download the course code files of python Django. Can you please help?
Excellent video and well explained. Thank you!
sir this{% for %}----{% endfor %} type is not working for mine. how to solve it? pls help. I tried a lot but failed
Hello ninja, first of all, great job with the tutorials, they are amazing!
I only have one question, Are you planning in the near future to show us how to serve static files for production?
i would really appreaciate. xD
Really cool explanations! Thank you.
I'm confused
without STATICFILES_DIRS set
how could /static/style.css open on the project
Does django already know about the file ?
Thanks for the tutorials, I've learnt a lot from you
Can someone please explain what is "Serve up images" at 0:47?
I am not sure what he meant
hi, i didn't quite understand why i used a folder that contains all our static files
awesome tutorial bro. You really know your stuff.
Thanks man for making it seem so easy
the styles.css styles wont load on my articles.html
Nice to see you again
you know what you video helped me a lot, wonderful
I want to use {% load static%} with this;
how i use idk
you can share github link on this project?
Hi there. Your videos are really helpful! However, i am having problem with my static files. the CSS file does not seem to be working. How can i make it work?
found my mistake. Its not ref but rel
thx
I hope, someday I will watch playlist for Django in production :-)
I wish you could make Django/AWS for production a mini series or a simple tutorial Shaun! Not many people guide for industrial production level basis..
Thank you very much for this video
I just loved this tutorial...
Hey! I have a problem with my browser loading the css file, I do exactly what you do but it cant load the css file
you've probably solved it, but in my case it was because I missed the comma at the end of the staticfiles tuple
why its not rendering transparent png images?
in my case it did't work with
{%load static from staticfiles%}
it worked with
{%load static %}
but in your case it work with staticfiles .
why its happend
Hello,
I can' get past the "Directory indexes are not allowed here" on the Get static call to the server. It seems to me i've done everything right. I've gone as far as copying netninja's code from github into my own script and reviewing the django documentation they have on the web.
github.com/ATajadod94/django-practice-project/tree/master/practice/practice
my folder is uploaded on github which contains both the setting and url python files. If anyone can help that would be great.It seems to read the static page so i'm doing at least something right , but obviously not enough. Also, when I try to import static into my tempelate, it doesnt read it, I assume for the same reasons.
How to use with img tag?
Can I ask you something? What’s the benefit of Django jargon when we can do things in a lot easier fashion using the same old html,php,Css JavaScript etc? I don’t see any point of this Django other than causing confusions and making simplest of things a lot harder to do. Same as mvc I never saw any point in that too. There is absolutely no way any one can remeber so many things to do in different files and folders at the place of work.
thanks bro you save my life
I struggled to find a solution that will work but this has made my work easy, thanks bro a subscribe 4 you and a like on top
No problem Kenny :) thanks for the support!
Thank you very much 😊👍
THANK YOU. I can't believe how bad the official django documentation is for this simple task.
no idea what im doing wrong. I cant go to a url and work
If your changes in css is not reflecting on web page then it might be because of your browser is caching css file in the browers,
Try refreshing web page by holding ctrl or shift, this should work.
Pls do aws! I would definitely appreciate that! :)
I can not get static files in the browser
To get the Ubuntu font you need to add :
to the html file
Thank you so much!
Thank you very much :)
I have tried everything but this static not working for me. I need your assistance sir.
Same here
thanks a lot.. it helps a lot..
Hi, I have really strange problem with static files. In my css file i can't affect body at all. My css file is connected for sure because i can change other things like f.e. h1 but body won't work no matter what I am doing.
did u manage to solve it? i am facing the same prob as u
@@puteri8 same
If you are having trouble with the static filers thing, try to check the folder level, it has to be on the same level of apps.
Thanks Gabriel, that was the case with my code :)
Thanks bro, I couldn't figure out the issue for like an hour
At first i did it.But it still didn't work. But when i put the static folder inside the article app,then it works . I am still trying to find where the problem was?
Thankyou so much it works and it uses me alot.
It uses u? :P
xD
i was rolling to make it work but it work for me after change this line of code its not a tuble
its a list ( ) => [ ]
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'assets'),
]
You can still make it a tuple, all you needed is a comma at the end of 'assets'),
that's all
Hi, thank you for the tutorial. I am facing issue while loading my static/styles.css file on browser. It says Not Found
The requested URL /static/styles.css was not found on this server.
I have followed same procedure line by line. I have not missed "," after staticfiles_DIRS tuple.
Please help
did you find the answer to your problem? i have the same issue here ):
CaesarGamer did you find the solution i am having the same problem
What worked for me was to use a list, and not a tuple, like this:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles'),
]
You see, he says you can use a list or a tuple, and maybe it works in older django versions, but for some reason i had to use a list, tuples don't work for me in this, also i named my folder different (saticfiles), but that has nothing to do with the error.
I hope this help you.
All of this of course in the settings.py files, i didnt mention it in the reply, sorry xD
thank you, it saved my day :)
Thank you very much
Thank you!
where is advanced , i am waiting for it .you are the best teacher . i need tips to make my website secure .
i like your video very well explained
Thanks :)
you're a LEGEND !!!
guys, if you have problem with staticfiles, just replace this line to {% load static %}
ValueError: not enough values to unpack (expected 2, got 1)
Thanks a lot
If you are still having trouble loading the /static/styles.css page - try restarting. I tried every fix on here and nothing was working. Restarted and it worked *face palm*
Thank you, sir
I've just set a new personal best for hitting a subscribe button.
I had to take the preceding forward slash out of STATIC_URL: STATIC_URL = 'static/' for it to work... in case anyone else is stuck!
os.path.join.(BASE_DIR,'assets'),
^
SyntaxError: invalid syntax
os.path.join(BASE_DIR, 'assets'),
spot the difference now?
remove the . after join
year?
I had a problem loading my styles.css . For some reason they don't apply to the webpage
same here! got any solution??
@@asiful_alam yeah actually. It turns out I just forgot to remove the DOCTYPE HTML thing at the beginning of the template. It worked after that. You should try it. So funny how I literally spent hours trying to fix such a tiny problem
for some who got stucked with the STATICFILES_DIR and File Not Found problem, I hope you didnt repeat the same silly mistake i did, which is the 'assets' folder i put it wrongly in the sub 'Djangonautic' folder, in which it supposed to be in the base 'Djangonautic' folder.
well... thank u
updated:>>> {% load static %} not use whatever shown on video.
loved it
Everything is working fine but the font. I downloaded the entire Ubuntu family yet still displays a Times New Roman font. Not every font works tho (Arial works but not comic sans?)
You need to get Ubuntu font from google fonts
Life saver !!
God Bless You!!!!
Python might make you add a trailing comma in the Touple
Still waiting for Django for production series!!!
2:16 my friend i didn't get this one can u please simply in few words it will be very much helpful for me to learn django :)
Thanks
please make new videos with other example and updated django version
AWS series please...
i cant find the css file
same here did you find a solution for it?
Thank you!
i added comma but still have the problem
I can't even access 127.0.0.1/8000/static without getting a 404 no matter what I do
Same here
@@ninjatribble7961 cant remember what I did to solve this issue, but I then ran into another issue with user media and this vid helped me out ua-cam.com/video/QC2cLkHoXLk/v-deo.html
@@ninjatribble7961 let me know if you have questions, I might remember what I did to fix it
I have tried honestly every single thing in the comments for serving up the static files, and still nothing! I'm am ready to cry!
Oh my f!!! FINALLY!!! At 2AM guys!! 2 fucking AM I figured out the problem.
Should be:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "assets/styles.css")
)
Django wants to know the path and not the location of the folder. Now I'm wondering what will happen if there is more than one file in there. This is frustrating me!
@Volk Raider THE fuCKIGN COMA
yes, add a comma at the end of the second line like NN did.
It looks that it's getting trickier and trickier...
How do you know what to IMPORT and FROM to import it?
And i think you need to know 2 things:
1 - That there is no such functionality in Django by default;
2 - That there is SOMETHING that you can import to get that needed functionality.
well in python from import means to import everything compared to just import. I guess just use from import