Django Tutorial #12 - Static Files & Images

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025

Комментарии • 205

  • @ProSimples
    @ProSimples 5 лет назад +201

    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 %}

  • @ignamikel
    @ignamikel Год назад +4

    bro just wanted to say that 6 years later, your django tutorials are still the best around! 🤗

    • @NetNinja
      @NetNinja  Год назад

      I appreciate that! :) thank you

  • @adarshpunj
    @adarshpunj 5 лет назад +18

    Okay, this is indeed the BEST DJANGO tutorial in the universe. Thanks a lot for creating series.

    • @NetNinja
      @NetNinja  5 лет назад +1

      Thanks so much! :)

  • @user-gs2zp3dm1v
    @user-gs2zp3dm1v 3 года назад +10

    in DJANGO 3 , in settings.py use the following STATICFILES_DIRS = [
    BASE_DIR / "assets",
    ]

  • @maxiequa567
    @maxiequa567 7 лет назад +95

    AWS/Django for production mini-series +1

  • @devkrishanrpurohit5181
    @devkrishanrpurohit5181 4 года назад +14

    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

    • @VS257
      @VS257 3 года назад +1

      Django seem to not recognize the "os" part. It has a yellow sqiggly line underneath... it says "os" is not defined ???? help?

    • @preetsingh2133
      @preetsingh2133 3 года назад +6

      @@VS257 Import os in settings.py file

    • @user-gs2zp3dm1v
      @user-gs2zp3dm1v 3 года назад

      @@VS257 STATICFILES_DIRS = [
      BASE_DIR / "assets",
      ]

  • @stebterp
    @stebterp 5 лет назад +14

    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.

  • @atitallahmariem2522
    @atitallahmariem2522 4 года назад +2

    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 :)

  • @rossideas
    @rossideas 4 года назад +1

    Your communication skills are the best!! Short, sweet and to the point. Thank you.

  • @mel8080
    @mel8080 4 года назад +1

    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!

  • @РауанСатыбалды-л7я
    @РауанСатыбалды-л7я 4 года назад +1

    Thank you!! I searched many information, so I find this! I thougt about give up, but with your help I may continue my hobby!

  • @garynelson1039
    @garynelson1039 6 лет назад

    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.

  • @austinmurphy9074
    @austinmurphy9074 5 лет назад +42

    FIX: in templates {% load static %} in link href="{% static "styles.css" %}"

    • @Yousifalzoubi
      @Yousifalzoubi 5 лет назад

      Thanks man! ;)

    • @JJnnaatt
      @JJnnaatt 4 года назад +1

      Thank you. Use
      href="{% static 'styles.css' %}"
      to avoid escaping the double quotes though

    • @rryann088
      @rryann088 4 года назад

      @@JJnnaatt thanks a lot, does the job neatly :)

  • @AcademiaPython
    @AcademiaPython 4 года назад +1

    best tutorial I've ever seen, I'm loving it.

  • @TheVinfather
    @TheVinfather 3 года назад

    Thanks for the tutorials, I've learnt a lot from you

  • @GabrielOliveira-hm6gi
    @GabrielOliveira-hm6gi 6 лет назад +4

    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.

    • @VadimBesedin
      @VadimBesedin 6 лет назад

      Thanks Gabriel, that was the case with my code :)

    • @v.m.5850
      @v.m.5850 6 лет назад

      Thanks bro, I couldn't figure out the issue for like an hour

    • @imammahadi352
      @imammahadi352 6 лет назад +1

      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?

  • @Anom21
    @Anom21 4 года назад

    Thanks man for making it seem so easy

  • @alexarcasm
    @alexarcasm 4 года назад +1

    To get the Ubuntu font you need to add :

    to the html file

  • @anukulchand868
    @anukulchand868 6 лет назад +2

    Excellent tutorials ! loved it !!

  • @kibi4979
    @kibi4979 4 года назад

    Really cool explanations! Thank you.

  • @peroquecoincidencia9991
    @peroquecoincidencia9991 4 года назад

    you know what you video helped me a lot, wonderful

  • @chukwunta
    @chukwunta 4 года назад

    awesome tutorial bro. You really know your stuff.

  • @stahelpeter
    @stahelpeter 6 лет назад

    Excellent video and well explained. Thank you!

  • @amrino
    @amrino 4 года назад

    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.

  • @Kennerdoll
    @Kennerdoll 2 года назад

    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

    • @NetNinja
      @NetNinja  2 года назад +1

      No problem Kenny :) thanks for the support!

  • @hahahihi6253
    @hahahihi6253 3 года назад

    Thank you very much 😊👍

  • @caesargamer6368
    @caesargamer6368 6 лет назад

    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

  • @9keen
    @9keen 4 года назад +1

    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.

  • @adambernard1404
    @adambernard1404 4 года назад +2

    THANK YOU. I can't believe how bad the official django documentation is for this simple task.

  • @cyberlab551
    @cyberlab551 3 года назад +1

    updated:>>> {% load static %} not use whatever shown on video.

  • @swapnaliarawandekar414
    @swapnaliarawandekar414 5 лет назад

    I just loved this tutorial...

  • @pranavireddy3653
    @pranavireddy3653 6 лет назад

    Thankyou so much it works and it uses me alot.

  • @ShivamSingh-wh9jj
    @ShivamSingh-wh9jj 5 лет назад

    thanks bro you save my life

  • @samdavepollard
    @samdavepollard 6 лет назад

    I've just set a new personal best for hitting a subscribe button.

  • @fooboobear
    @fooboobear 7 лет назад +1

    Nice to see you again

  • @johnwinchester8537
    @johnwinchester8537 5 лет назад +1

    And what if it's not in the BASE_DIR ?? Like I have it in a separate folder called polls

  • @meghaaggarwal8508
    @meghaaggarwal8508 4 года назад

    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..

  • @Sino0on
    @Sino0on 3 года назад +1

    guys, if you have problem with staticfiles, just replace this line to {% load static %}

  • @tauheedissa3293
    @tauheedissa3293 6 лет назад +2

    I have tried everything but this static not working for me. I need your assistance sir.

  • @ghazalakhan2964
    @ghazalakhan2964 6 лет назад +2

    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

    • @caesargamer6368
      @caesargamer6368 6 лет назад

      did you find the answer to your problem? i have the same issue here ):

    • @OvaisDIYGarage
      @OvaisDIYGarage 6 лет назад

      CaesarGamer did you find the solution i am having the same problem

    • @caesargamer6368
      @caesargamer6368 6 лет назад +1

      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.

    • @caesargamer6368
      @caesargamer6368 6 лет назад +1

      All of this of course in the settings.py files, i didnt mention it in the reply, sorry xD

    • @bestplanet6175
      @bestplanet6175 6 лет назад +1

      thank you, it saved my day :)

  • @faisalahmad2322
    @faisalahmad2322 6 лет назад +2

    sir please can you tell how to use images in static css file for background in django?

  • @upcom1ng116
    @upcom1ng116 3 года назад

    Can someone please explain what is "Serve up images" at 0:47?
    I am not sure what he meant

  • @jaywandery9269
    @jaywandery9269 2 года назад +1

    the styles.css styles wont load on my articles.html

  • @yasyfawiwaha4088
    @yasyfawiwaha4088 4 года назад

    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 %}

  • @K4MR4N1UK123
    @K4MR4N1UK123 4 года назад +1

    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.

  • @jdevcast6527
    @jdevcast6527 7 лет назад +1

    Is the font 'Ubuntu' standard or do you have to install it?

  • @puteri8
    @puteri8 5 лет назад +1

    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?

    • @puteri8
      @puteri8 5 лет назад

      found my mistake. Its not ref but rel

  • @AnMaDeBi
    @AnMaDeBi 3 года назад

    Thank you!

  • @clinck3439
    @clinck3439 3 года назад

    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.

  • @williamquanpreuss13
    @williamquanpreuss13 7 лет назад +2

    Pls do aws! I would definitely appreciate that! :)

  • @elliotgarcia8745
    @elliotgarcia8745 6 лет назад

    i like your video very well explained

  • @Sainiajits
    @Sainiajits 3 года назад

    Thank you very much :)

  • @cacheflex8033
    @cacheflex8033 5 лет назад +1

    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! :(

  • @aritora778
    @aritora778 5 лет назад

    thanks a lot.. it helps a lot..

  • @christianjaydango7567
    @christianjaydango7567 7 лет назад

    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?

  • @fasterfasterr6056
    @fasterfasterr6056 4 года назад

    where is advanced , i am waiting for it .you are the best teacher . i need tips to make my website secure .

  • @Lbmaniak
    @Lbmaniak 6 лет назад

    thx
    I hope, someday I will watch playlist for Django in production :-)

  • @sagarkafle9259
    @sagarkafle9259 3 года назад

    I'm confused
    without STATICFILES_DIRS set
    how could /static/style.css open on the project
    Does django already know about the file ?

  • @KacperMarciniak_
    @KacperMarciniak_ 3 года назад

    Thanks a lot

  • @chaitanyashimpi2880
    @chaitanyashimpi2880 5 лет назад

    I am not able to download the course code files of python Django. Can you please help?

  • @alexissuarezalvarez5622
    @alexissuarezalvarez5622 4 года назад

    hi, i didn't quite understand why i used a folder that contains all our static files

  • @fireberrywocky
    @fireberrywocky 5 лет назад +1

    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!

    • @fireberrywocky
      @fireberrywocky 5 лет назад +2

      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!

    • @devkrishanrpurohit5181
      @devkrishanrpurohit5181 4 года назад

      @Volk Raider THE fuCKIGN COMA

    • @paulbrugger9610
      @paulbrugger9610 2 года назад

      yes, add a comma at the end of the second line like NN did.

  • @TheFunkoDunko
    @TheFunkoDunko 5 лет назад

    Does this work after the Django app has already been deployed?

  • @GustavoMachadoAlvaresdeLima
    @GustavoMachadoAlvaresdeLima 6 лет назад

    would be possible to symlink any folder in my system to the myapp/static folder?

  • @iliatalebzade8751
    @iliatalebzade8751 4 года назад

    you're a LEGEND !!!

  • @MegaJagveer
    @MegaJagveer 4 года назад

    Python might make you add a trailing comma in the Touple

  • @asadhameed6610
    @asadhameed6610 4 года назад

    But how would you display a picture on the app (instead of it being a background)?

  • @Viralvlogvideos
    @Viralvlogvideos 3 года назад

    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 :)

  • @witoldszrama6344
    @witoldszrama6344 5 лет назад

    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.

    • @puteri8
      @puteri8 5 лет назад +1

      did u manage to solve it? i am facing the same prob as u

    • @aznsuperher0
      @aznsuperher0 5 лет назад

      @@puteri8 same

  • @DanielLeachTen
    @DanielLeachTen 3 года назад +1

    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!

  • @mdahossain1665
    @mdahossain1665 4 года назад

    why its not rendering transparent png images?

  • @dporro
    @dporro 6 лет назад

    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

    • @garynelson1039
      @garynelson1039 6 лет назад +1

      you've probably solved it, but in my case it was because I missed the comma at the end of the staticfiles tuple

  • @gcloud8397
    @gcloud8397 6 лет назад

    Thank you, sir

  • @hazemsobhy6493
    @hazemsobhy6493 5 лет назад +1

    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'),
    ]

    • @8o8inSquares
      @8o8inSquares 5 лет назад +1

      You can still make it a tuple, all you needed is a comma at the end of 'assets'),
      that's all

  • @debabratatah4887
    @debabratatah4887 6 лет назад

    sir this{% for %}----{% endfor %} type is not working for mine. how to solve it? pls help. I tried a lot but failed

  • @010101dddm
    @010101dddm 4 года назад

    I can not get static files in the browser

  • @gusfowle3001
    @gusfowle3001 3 года назад +1

    If you're running a more recent version of django, use this instead of the os.path.join method. BASE_DIR goes into the base project folder, it then looks for a folder called static.
    STATICFILES_DIRS = [
    BASE_DIR / 'static'
    ]

  • @ShaffleOne
    @ShaffleOne 4 года назад

    Still waiting for Django for production series!!!

  • @KevinTempelx
    @KevinTempelx 4 года назад

    Thanks

  • @RedElectrik
    @RedElectrik 4 года назад

    no idea what im doing wrong. I cant go to a url and work

  • @spiritcxz
    @spiritcxz 5 лет назад

    you can share github link on this project?

  • @dutch9238
    @dutch9238 4 года назад +1

    I can't even access 127.0.0.1/8000/static without getting a 404 no matter what I do

    • @ninjatribble7961
      @ninjatribble7961 4 года назад

      Same here

    • @dutch9238
      @dutch9238 4 года назад

      @@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 ruclips.net/video/QC2cLkHoXLk/видео.html

    • @dutch9238
      @dutch9238 4 года назад

      @@ninjatribble7961 let me know if you have questions, I might remember what I did to fix it

  • @lawesosah9214
    @lawesosah9214 5 лет назад

    I had a problem loading my styles.css . For some reason they don't apply to the webpage

    • @asiful_alam
      @asiful_alam 5 лет назад +1

      same here! got any solution??

    • @lawesosah9214
      @lawesosah9214 5 лет назад

      @@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

  • @prasadkadu9737
    @prasadkadu9737 3 года назад

    please make new videos with other example and updated django version

  • @lanceloot
    @lanceloot 5 лет назад

    year?

  • @srwaan
    @srwaan 4 года назад

    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?)

    • @marcin150893
      @marcin150893 4 года назад

      You need to get Ubuntu font from google fonts

  • @narendraverma825
    @narendraverma825 5 лет назад

    How to use with img tag?

  • @birdster42
    @birdster42 2 года назад

    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*

  • @dec23
    @dec23 6 лет назад +1

    God Bless You!!!!

  • @faisalhossain2002
    @faisalhossain2002 4 года назад

    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

  • @enesyurtsever3785
    @enesyurtsever3785 4 года назад

    I want to use {% load static%} with this;
    how i use idk

  • @oberaij
    @oberaij 6 лет назад

    There's no file in your github repo sir.

    • @NetNinja
      @NetNinja  6 лет назад +4

      Hey, did you select the appropriate branch (select box near the top left)? Just had a look and it's all there.

    • @oberaij
      @oberaij 6 лет назад

      Thank you.

  • @DuongTran-zh6td
    @DuongTran-zh6td 2 года назад

    2:11 tao urlpattern trong url (folder project)
    4:06 chinh setting (folder project)
    6:00 {{ load_static ... }}trong template
    7:17 them file .css

  • @beosoftlabs
    @beosoftlabs 6 лет назад

    AWS series please...

  • @dipeshmandanka
    @dipeshmandanka 3 года назад

    Life saver !!

  • @boubacardiallo7287
    @boubacardiallo7287 5 лет назад

    loved it

  • @dl1819
    @dl1819 5 лет назад +1

    haha was banging my head on my desk trying to figure out what the problem was.

  • @suryaadhikari3959
    @suryaadhikari3959 5 лет назад

    ValueError: not enough values to unpack (expected 2, got 1)

  • @VadimBesedin
    @VadimBesedin 6 лет назад

    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.

    • @richardc9325
      @richardc9325 6 лет назад

      well in python from import means to import everything compared to just import. I guess just use from import

  • @alirezatajadod8437
    @alirezatajadod8437 6 лет назад

    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.