{{STATIC_URL }} or {% static "...." %} What`s the correct to use?

203 views
Skip to first unread message

Fellipe Henrique

unread,
Mar 20, 2015, 12:59:08 AM3/20/15
to Django Users
Hi,

In my template, when I made a reference to my Static folder.. what's the correct usage?

{{STATIC_URL }} or {% static "...." %} 

Django 1.7 and Python 3

Thanks!

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Stephen J. Butler

unread,
Mar 20, 2015, 1:06:21 AM3/20/15
to django...@googlegroups.com
It says right in the docs what to do:

https://docs.djangoproject.com/en/1.7/howto/static-files/
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1jwZGwzdxJK4r9ZDrbXbaov3m9pkFRusnnyM3fLHU41Y--RA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Fellipe Henrique

unread,
Mar 20, 2015, 1:20:18 AM3/20/15
to Django Users
Thanks, but I already try the docs, but my static files doesn't  show!

Show me 404 error.. even when I run manage.py collectstatic

Vijay Khemlani

unread,
Mar 20, 2015, 1:25:00 AM3/20/15
to django...@googlegroups.com
What URL is being generated in the final HTML?, and does that path match the one you have in the static folder in your app?

Also, if your are on debug mode, you don't need to run collectstatic

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Stephen J. Butler

unread,
Mar 20, 2015, 1:26:13 AM3/20/15
to django...@googlegroups.com
Some questions you need to answer then:

1. Does the URL to the static resource appear properly in your rendered HTML?
2. Did you configure your web server properly so that the STATIC_ROOT
appears at the right location?
3. After running collectstatic does the STATIC_ROOT directory have the
expected structure?
4. Are file and directory permissions correct so that the server can read them?
5. Can you enter the URL to the static resource in your browser and view it?
6. Is there any information in your web server error_log file that helps?
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1jwZF_RWXWCQTozSO%2BODqNuTyz-29VZ0pvjsw7Jsdosuw31Q%40mail.gmail.com.

Mike Dewhirst

unread,
Mar 20, 2015, 2:25:02 AM3/20/15
to django...@googlegroups.com
On 20/03/2015 12:19 PM, Fellipe Henrique wrote:

Here is a fairly typical static file template line ...

<link rel="stylesheet" type="text/css"
href="{{STATIC_URL}}css/styles.css" media="screen"/>

... and to see where that fits, insert something like this in your local
settings. You will discover any bludners fairly quickly ...

print(('BASE_DIR = %s %s' % (BASE_DIR, '(project root)')))
print(('SETTINGS_DIR = %s' % SETTINGS_DIR))
print(('MEDIA_ROOT = %s %s' % (MEDIA_ROOT, '(store uploaded images)')))
print(('MEDIA_URL = <website>%s %s' % (MEDIA_URL, '(serve uploaded
images)')))
print(('STATIC_ROOT = %s %s' % (STATIC_ROOT, '(collectstatic dest)')))
print(('STATIC_URL = <website>%s %s' % (STATIC_URL, '(serve css, js etc)')))
for i in enumerate(STATICFILES_DIRS, 1):
print(('STATICFILES_DIRS#%s = %s' % i))
for i in enumerate(TEMPLATE_DIRS, 1):
print(('TEMPLATE_DIRS#%s = %s' % i))



> Thanks, but I already try the docs, but my static files doesn't  show!
>
> Show me 404 error.. even when I run manage.py collectstatic
>
> T.·.F.·.A.·.   S+F
> *Fellipe Henrique P. Soares*
>
> e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
> /Blog: http://fhbash.wordpress.com//
> /GitHub:Â https://github.com/fellipeh/
> /Twitter: @fh_bash/
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1jwZF_RWXWCQTozSO%2BODqNuTyz-29VZ0pvjsw7Jsdosuw31Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAF1jwZF_RWXWCQTozSO%2BODqNuTyz-29VZ0pvjsw7Jsdosuw31Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Ilya Kazakevich

unread,
Mar 20, 2015, 12:36:29 PM3/20/15
to django...@googlegroups.com
This one "{% static "...." %}" is MUCH better than {{STATIC_URL }}.
It is recommended way. 

You almost never need to use {{STATIC_URL }}.

Fellipe Henrique

unread,
Mar 20, 2015, 2:14:57 PM3/20/15
to Django Users
So, answer the questions: 

- I in Debug mode: DEBUG = True
- Yes, all my folder has the correct permission, I can edit any files there.

- I  my HTML I have:

  <link rel="stylesheet" href="{% static "public/css/bootstrap.min.css" %}">

and in source code, in browser show these:

 <link rel="stylesheet" href="/static/public/css/bootstrap.min.css">


- When I try to go:  localhost:8000/static   show these:

Page not found (404)

Request Method: GET
Request URL: http://localhost:8000/static/

Directory indexes are not allowed here.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.



Any thoughts?      


T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Frank Bieniek

unread,
Mar 20, 2015, 2:19:00 PM3/20/15
to django...@googlegroups.com
HI
check your main urls.py for something like this:

if settings.DEBUG:
    urlpatterns += patterns('',
        #(r'^media/(?P<path>.*)', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
        (r'^static/(?P<path>.*)', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
    )

This allows static file serving from the runserver - only for local development!!

thanks
Frank

Vijay Khemlani

unread,
Mar 20, 2015, 2:25:25 PM3/20/15
to django...@googlegroups.com
You don't need special rules in the URL config to serve static files in development as far as I know

so you have a route to "/static/public/css/bootstrap.min.css" and I assume that when you try to access


you get a 404 error

Where is bootstrap.min.css located in your project directory? 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Fellipe Henrique

unread,
Mar 20, 2015, 2:34:14 PM3/20/15
to Django Users

On Fri, Mar 20, 2015 at 11:24 AM, Vijay Khemlani <vkhe...@gmail.com> wrote:
http://localhost:8000/static/public/css/bootstrap.min.css

In fact, if I type these in firefox, show me the file... :)

but nothing in my HTML..

here is my html, 

{% load staticfiles %}
<html>
<head>
    <link rel="stylesheet" href="{% static "public/css/bootstrap.min.css" %}">
</head>
<body>
  <h1>Test!!</h1>
</body>
</html>


I really don't know, how is the problem here... :(  in my PyCharm Project, in HTML, show me: Unresolved Reference, to my static files... :(

Néstor

unread,
Mar 20, 2015, 2:38:29 PM3/20/15
to django...@googlegroups.com
Is it OK to have this syntax?

 href="{% static "public/css/bootstrap.min.css" %}">

Should you not do something like this:

href="{% static 'public/css/bootstrap.min.css' %}">

Vijay Khemlani

unread,
Mar 20, 2015, 2:43:32 PM3/20/15
to django...@googlegroups.com
Soo.... when you open your webpage, look at its source code, click on the "/static/public/css/bootstrap.min.css" path it shows the file in Firefox?


Tom Lockhart

unread,
Mar 20, 2015, 2:52:51 PM3/20/15
to django...@googlegroups.com
On Mar 20, 2015, at 7:37 AM, Néstor <rot...@gmail.com> wrote:

Is it OK to have this syntax?
 href=“{% static "public/css/bootstrap.min.css" %}">

fwiw yes it is OK to have that syntax with double quotes inside another set of double quotes.

- Tom

Fellipe Henrique

unread,
Mar 20, 2015, 5:01:01 PM3/20/15
to Django Users

On Fri, Mar 20, 2015 at 11:43 AM, Vijay Khemlani <vkhe...@gmail.com> wrote:
Soo.... when you open your webpage, look at its source code, click on the "/static/public/css/bootstrap.min.css" path it shows the file in Firefox?

Yes.. when I open source code in firefox, click them.. show me the file.. but, nothing to appears correctly my HTML...

I really upset about these... I really don't know what's happen here... 

Vijay Khemlani

unread,
Mar 20, 2015, 5:16:40 PM3/20/15
to django...@googlegroups.com
OK, this is officially no longer a Django problem xD

Try adding the type="text/css" attribute to the link tag, also remember to close it just in case

<link rel="stylesheet" type="text/css" href="{% static 'public/css/bootstrap.min.css' %}
" />

Other than that you may have problems in your HTML

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Tom Lockhart

unread,
Mar 20, 2015, 5:20:50 PM3/20/15
to django...@googlegroups.com
On Mar 20, 2015, at 9:59 AM, Fellipe Henrique <fell...@gmail.com> wrote:


On Fri, Mar 20, 2015 at 11:43 AM, Vijay Khemlani <vkhe...@gmail.com> wrote:
Soo.... when you open your webpage, look at its source code, click on the "/static/public/css/bootstrap.min.css" path it shows the file in Firefox?

Yes.. when I open source code in firefox, click them.. show me the file.. but, nothing to appears correctly my HTML...

I really upset about these... I really don’t know what's happen here... 

Your original problem statement included:

  …
  - When I try to go:  localhost:8000/static   show these:
  Page not found (404)
  …

In my debug setup, I have the same result, and this is normal. You *should* be able to take a specific static resource and put it into the browser as a URL. For example, I can put http://localhost:8000/static/fz/img/loadingAnimation.gif into my browser and that *does* find the gif.

This is compatible with your observation that you can take the link in your html and that *does* work when pasted into your browser.

I’m guessing that there is a problem with your HTML, not with your static setup. Can you distill the problem down to a simple HTML template and resulting page and post both?

When debugging html template troubles you may want to play around with {% comment %}…{% endcomment %} blocks to get rid of code for testing.

hth

- Tom



T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Please consider the environment before printing this message.
This message may be privileged and/or confidential, and the sender does not waive any related rights and obligations.  Any distribution, use or copying of this message or the information it contains by other than an intended recipient is unauthorized.  If you received this message in error, please immediately advise me by return e-mail or phone.  All information, references, images, programs, source code, or other materials whatsoever contained in, or supplied with, this document are TRADE SECRETS and governed by the Uniform Trade Secrets Act.  User assumes all direct and consequential liabilities and costs that result from any unauthorized disclosure or use of this information.

Fellipe Henrique

unread,
Mar 21, 2015, 12:04:31 AM3/21/15
to Django Users
Man!!!

You don't believe what's the problem is!!?!

The problem is my Firefox!! For some reason, firefox make a huge cache.. and it's using very old version of CSS and JS... I clean All my data, and work!!!


Thanks Again!

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Reply all
Reply to author
Forward
0 new messages