Django 2 Images from STATIC_URL is not loading in Css2019 Community Moderator ElectionDoes Django scale?Django external css file problemcannot access static files in DjangoLink to a django static file doesnt workHow to link my css, js and image file link in djangoDjango : extract image from database and display in templateBest configuration for Django staticfilesLinking up CSS (and static files in general) to html in DjangoDisplay images that reside in a folder outside of django project folderDjango static file is not loading in my computer (windows 10) but its working on others computer (windows 10)

Specifying a starting column with colortbl package and xcolor

This Alpine town?

Rationale to prefer local variables over instance variables?

Can we track matter through time by looking at different depths in space?

Should I take out a loan for a friend to invest on my behalf?

What's the 'present simple' form of the word "нашла́" in 3rd person singular female?

Can I negotiate a patent idea for a raise, under French law?

Why couldn't the separatists legally leave the Republic?

Do cubics always have one real root?

Which classes are needed to have access to every spell in the PHB?

Can one live in the U.S. and not use a credit card?

Proving a statement about real numbers

How to resolve: Reviewer #1 says remove section X vs. Reviewer #2 says expand section X

What is this diamond of every day?

Recommendation letter by significant other if you worked with them professionally?

Are all players supposed to be able to see each others' character sheets?

MySQL importing CSV files really slow

Is this Paypal Github SDK reference really a dangerous site?

What is better: yes / no radio, or simple checkbox?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

From an axiomatic set theoric approach why can we take uncountable unions?

Shifting between bemols and diesis in the key signature

Confusion about Complex Continued Fraction

What is the generally accepted pronunciation of “topoi”?



Django 2 Images from STATIC_URL is not loading in Css



2019 Community Moderator ElectionDoes Django scale?Django external css file problemcannot access static files in DjangoLink to a django static file doesnt workHow to link my css, js and image file link in djangoDjango : extract image from database and display in templateBest configuration for Django staticfilesLinking up CSS (and static files in general) to html in DjangoDisplay images that reside in a folder outside of django project folderDjango static file is not loading in my computer (windows 10) but its working on others computer (windows 10)










1















I'm working on a Django(2.1.7) project in which I need to load some images in css file.
Here's what I have so far:



From settings.py:



STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]


I have a directory in my main project folder named as assets and also have 'django.contrib.staticfiles' in INSTALLED_APPS and then I have mentioned some images in css as:



background: url('/assets/images/demo.png') center no-repeat;


The image is available in the images folder but not displaying in the template.



What can be wrong here?










share|improve this question






















  • Did you specify % load static % at the beginning of your template?

    – Rajan Sharma
    Mar 6 at 14:30












  • the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

    – dirkgroten
    Mar 6 at 14:31











  • Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

    – dirkgroten
    Mar 6 at 14:32











  • Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

    – Abdul Rehman
    Mar 6 at 14:32











  • Hi @dirkgroten I have images folder inside my assets folder`.

    – Abdul Rehman
    Mar 6 at 14:33















1















I'm working on a Django(2.1.7) project in which I need to load some images in css file.
Here's what I have so far:



From settings.py:



STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]


I have a directory in my main project folder named as assets and also have 'django.contrib.staticfiles' in INSTALLED_APPS and then I have mentioned some images in css as:



background: url('/assets/images/demo.png') center no-repeat;


The image is available in the images folder but not displaying in the template.



What can be wrong here?










share|improve this question






















  • Did you specify % load static % at the beginning of your template?

    – Rajan Sharma
    Mar 6 at 14:30












  • the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

    – dirkgroten
    Mar 6 at 14:31











  • Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

    – dirkgroten
    Mar 6 at 14:32











  • Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

    – Abdul Rehman
    Mar 6 at 14:32











  • Hi @dirkgroten I have images folder inside my assets folder`.

    – Abdul Rehman
    Mar 6 at 14:33













1












1








1








I'm working on a Django(2.1.7) project in which I need to load some images in css file.
Here's what I have so far:



From settings.py:



STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]


I have a directory in my main project folder named as assets and also have 'django.contrib.staticfiles' in INSTALLED_APPS and then I have mentioned some images in css as:



background: url('/assets/images/demo.png') center no-repeat;


The image is available in the images folder but not displaying in the template.



What can be wrong here?










share|improve this question














I'm working on a Django(2.1.7) project in which I need to load some images in css file.
Here's what I have so far:



From settings.py:



STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]


I have a directory in my main project folder named as assets and also have 'django.contrib.staticfiles' in INSTALLED_APPS and then I have mentioned some images in css as:



background: url('/assets/images/demo.png') center no-repeat;


The image is available in the images folder but not displaying in the template.



What can be wrong here?







python django django-2.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 14:28









Abdul RehmanAbdul Rehman

1,061626




1,061626












  • Did you specify % load static % at the beginning of your template?

    – Rajan Sharma
    Mar 6 at 14:30












  • the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

    – dirkgroten
    Mar 6 at 14:31











  • Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

    – dirkgroten
    Mar 6 at 14:32











  • Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

    – Abdul Rehman
    Mar 6 at 14:32











  • Hi @dirkgroten I have images folder inside my assets folder`.

    – Abdul Rehman
    Mar 6 at 14:33

















  • Did you specify % load static % at the beginning of your template?

    – Rajan Sharma
    Mar 6 at 14:30












  • the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

    – dirkgroten
    Mar 6 at 14:31











  • Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

    – dirkgroten
    Mar 6 at 14:32











  • Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

    – Abdul Rehman
    Mar 6 at 14:32











  • Hi @dirkgroten I have images folder inside my assets folder`.

    – Abdul Rehman
    Mar 6 at 14:33
















Did you specify % load static % at the beginning of your template?

– Rajan Sharma
Mar 6 at 14:30






Did you specify % load static % at the beginning of your template?

– Rajan Sharma
Mar 6 at 14:30














the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

– dirkgroten
Mar 6 at 14:31





the path you specify in your css is relative to your css file path. So this would only work if your css file is in a directory that contains the 'assets' directory.

– dirkgroten
Mar 6 at 14:31













Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

– dirkgroten
Mar 6 at 14:32





Assuming you also have a /assets/css/ directory where your css file is located, the link to your image should be "../images/demo.png".

– dirkgroten
Mar 6 at 14:32













Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

– Abdul Rehman
Mar 6 at 14:32





Hi @RajanSharma, I'm inside the css file where % load static % doesn't work and template is working.

– Abdul Rehman
Mar 6 at 14:32













Hi @dirkgroten I have images folder inside my assets folder`.

– Abdul Rehman
Mar 6 at 14:33





Hi @dirkgroten I have images folder inside my assets folder`.

– Abdul Rehman
Mar 6 at 14:33












2 Answers
2






active

oldest

votes


















1














The path you specify in your css is relative to your css file path. So this url would only work if your css file is in a directory that contains the assets directory.



The question is where is your css file. Assuming it's in a /assets/css folder (inside assets), your url should be:



background: url('../images/demo.png') center no-repeat;





share|improve this answer






























    0














    Add below snippet to your urls.py file



    # <----------- rest of the code----------->
    urlpatterns += static('/assets/', document_root=settings.STATIC_ROOT) + static(
    settings.STATIC_URL, document_root=settings.STATIC_ROOT
    )





    share|improve this answer


















    • 1





      This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

      – dirkgroten
      Mar 6 at 15:03











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025449%2fdjango-2-images-from-static-url-is-not-loading-in-css%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The path you specify in your css is relative to your css file path. So this url would only work if your css file is in a directory that contains the assets directory.



    The question is where is your css file. Assuming it's in a /assets/css folder (inside assets), your url should be:



    background: url('../images/demo.png') center no-repeat;





    share|improve this answer



























      1














      The path you specify in your css is relative to your css file path. So this url would only work if your css file is in a directory that contains the assets directory.



      The question is where is your css file. Assuming it's in a /assets/css folder (inside assets), your url should be:



      background: url('../images/demo.png') center no-repeat;





      share|improve this answer

























        1












        1








        1







        The path you specify in your css is relative to your css file path. So this url would only work if your css file is in a directory that contains the assets directory.



        The question is where is your css file. Assuming it's in a /assets/css folder (inside assets), your url should be:



        background: url('../images/demo.png') center no-repeat;





        share|improve this answer













        The path you specify in your css is relative to your css file path. So this url would only work if your css file is in a directory that contains the assets directory.



        The question is where is your css file. Assuming it's in a /assets/css folder (inside assets), your url should be:



        background: url('../images/demo.png') center no-repeat;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 6 at 14:33









        dirkgrotendirkgroten

        5,44611324




        5,44611324























            0














            Add below snippet to your urls.py file



            # <----------- rest of the code----------->
            urlpatterns += static('/assets/', document_root=settings.STATIC_ROOT) + static(
            settings.STATIC_URL, document_root=settings.STATIC_ROOT
            )





            share|improve this answer


















            • 1





              This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

              – dirkgroten
              Mar 6 at 15:03
















            0














            Add below snippet to your urls.py file



            # <----------- rest of the code----------->
            urlpatterns += static('/assets/', document_root=settings.STATIC_ROOT) + static(
            settings.STATIC_URL, document_root=settings.STATIC_ROOT
            )





            share|improve this answer


















            • 1





              This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

              – dirkgroten
              Mar 6 at 15:03














            0












            0








            0







            Add below snippet to your urls.py file



            # <----------- rest of the code----------->
            urlpatterns += static('/assets/', document_root=settings.STATIC_ROOT) + static(
            settings.STATIC_URL, document_root=settings.STATIC_ROOT
            )





            share|improve this answer













            Add below snippet to your urls.py file



            # <----------- rest of the code----------->
            urlpatterns += static('/assets/', document_root=settings.STATIC_ROOT) + static(
            settings.STATIC_URL, document_root=settings.STATIC_ROOT
            )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 6 at 14:46









            Devang PadhiyarDevang Padhiyar

            406119




            406119







            • 1





              This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

              – dirkgroten
              Mar 6 at 15:03













            • 1





              This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

              – dirkgroten
              Mar 6 at 15:03








            1




            1





            This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

            – dirkgroten
            Mar 6 at 15:03






            This is not needed since the OP is using 'django.contrib.staticfiles' so in debug=True mode, runserver automatically finds the static files in the various /static directories of the apps and inside the /assets directory (since he added that to STATICFILES_DIRS). And even then, this should definitely be removed in production settings (so wrap in if settings.DEBUG == True:). And finally, it only serves the files if they're in STATIC_ROOT so you'd have to run collectstatic on your development machine all the time a change is made to static files.

            – dirkgroten
            Mar 6 at 15:03


















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025449%2fdjango-2-images-from-static-url-is-not-loading-in-css%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович