Import css in Django

92 views
Skip to first unread message

Jonathan Cheng

unread,
Sep 11, 2016, 2:59:56 PM9/11/16
to Django users
I use django1.10

i reference the official doc
https://docs.djangoproject.com/en/1.10/intro/tutorial06/

my project name:mysite

structure:
 
- mysite-
                  
             - mysite - urls.py

                       -views.py

                        ...

             - templates - index.html
                  
                         - images
             - static - mysite - assets - css - main.css
                    
                                        - js

                                        ...
In my index.html

add

`{% load staticfiles % }
        <link rel="stylesheet" text = 'html/css' href="{% static 'mysite/static/main.css' %}" />`

it said i have a Invalid block tag about TemplateSyntaxError
`<link rel="stylesheet" type = 'text/css' href="{% static 'mysite/static/assets/css/main.css' %}" />`

views.py (about static files part)
```STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/mysite/static/',
]
STATIC_URL = '/static/'
STATIC_ROOT='/mysite/static/'```

what's part should i notice?

ludovic coues

unread,
Sep 11, 2016, 3:45:24 PM9/11/16
to django...@googlegroups.com
Have you tried to add a few newlines to be sure about which line is the issue ?

Also, in you email, there is a space between % and } in the load staticfiles tag
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/ef2ea6eb-d1cb-40e2-abef-1af246e02e98%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Jonathan Cheng

unread,
Sep 11, 2016, 3:58:38 PM9/11/16
to Django users
thx reply,but it just can see the original html with no css,is my path setted correct?

ludovic coues於 2016年9月11日星期日 UTC+8下午11時45分24秒寫道:

ludovic coues

unread,
Sep 11, 2016, 4:05:12 PM9/11/16
to django...@googlegroups.com
I have a hard time to read the paths you use. The command tree produce
a nice tree view of a directory, it might help. You can also use
service like http://paste2.org/ or https://gist.github.com/ to share
it
> https://groups.google.com/d/msgid/django-users/a699584c-d57f-4b84-a38b-fe03638caefd%40googlegroups.com.

Jonathan Cheng

unread,
Sep 11, 2016, 4:18:57 PM9/11/16
to Django users

https://gist.github.com/cj10243/fb557903441d4a3e95190b854fbaabe1

ludovic coues於 2016年9月12日星期一 UTC+8上午12時05分12秒寫道:

ali Eblice

unread,
Sep 11, 2016, 4:44:09 PM9/11/16
to Django users
your "mysite" folder and "static" folder are in a same directory but in your settings.py you wrote "STATIC_ROOT='/mysite/static/'" which says that static should be subdirectory of mysite which its not a subdirectory its in a same directory of mysite

ludovic coues

unread,
Sep 11, 2016, 4:53:08 PM9/11/16
to django...@googlegroups.com
I mentionned tree because I have doubt about what "static - assets -
css - main.css" means. If your path is really
/mysite/static/assets/css/main.css, then you want <link
rel="stylesheet" text = 'html/css' href="{% static
'assets/css/main.css' %}" />.

At least with a debug configuration, django will search for static
file in each apps' static directory for the path you used as arguments
for static.
With a production configuration, you should use manage.py
collectstatic which will grab all static file and copy them in a
single directory.
> https://groups.google.com/d/msgid/django-users/6e864bf6-c12b-49bc-b906-d2c35a5e884b%40googlegroups.com.

Jonathan Cheng

unread,
Sep 11, 2016, 5:04:22 PM9/11/16
to Django users
here i explain , because  i just test how main.css work, finally i want to use the assets and images .
and i use the python mange.py collectstatic
it showed

ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

ludovic coues於 2016年9月12日星期一 UTC+8上午12時53分08秒寫道:

Jonathan Cheng

unread,
Sep 11, 2016, 5:09:29 PM9/11/16
to django...@googlegroups.com
thx i change into STATIC_ROOT='/static/'
it seems to no error
but when i connect to my runserver
the css of  assets just doesn't work

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/h2vrmiJfcbY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Jonathan Cheng

unread,
Sep 11, 2016, 5:15:15 PM9/11/16
to Django users
i fixed usint STATIC_ROOD = '/static/'

it seems to no error
but when i connect to my runserver
the css of  assets just doesn't work


Jonathan Cheng於 2016年9月12日星期一 UTC+8上午1時04分22秒寫道:

ludovic coues

unread,
Sep 11, 2016, 5:32:42 PM9/11/16
to django...@googlegroups.com
If you look at the html send by django, what is the href value ?

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

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

Mike Dewhirst

unread,
Sep 12, 2016, 2:05:10 AM9/12/16
to django...@googlegroups.com
Jonathan

I use the attached announce.py to display exactly where all the
directories and URLs are. I had trouble in the beginning getting it
straight in my head but seeing it every time I looked at the dev-server
output helped a lot.

Comments in announce.py show how to use it.

Good luck

Mike

On 12/09/2016 1:58 AM, Jonathan Cheng wrote:
> thx reply,but it just can see the original html with no css,is my path
> setted correct?
>
> ludovic coues於 2016年9月11日星期日
> UTC+8ä¸‹å ˆ11時45分24ç§’å¯«é “ï¼š
>
> Have you tried to add a few newlines to be sure about which line
> is the issue ?
>
> Also, in you email, there is a space between % and } in the load
> staticfiles tag
>
> 2016-09-11 7:29 GMT+02:00 Jonathan Cheng <jonatha...@gmail.com
> <javascript:>>:
> > I use django1.10
> >
> > i reference the official doc
> > https://docs.djangoproject.com/en/1.10/intro/tutorial06/
> <https://docs.djangoproject.com/en/1.10/intro/tutorial06/>
> >
> > my project name:mysite
> >
> > structure:
> >
> > - mysite-
> >
> > Â Â Â Â Â Â Â - mysite - urls.py
> >
> > Â Â Â Â Â Â Â Â Â Â Â Â -views.py
> >
> > Â Â Â Â Â Â Â Â Â Â Â Â ...
> >
> > Â Â Â Â Â Â Â - templates - index.html
> >
> > Â Â Â Â Â Â Â Â Â Â Â Â Â - images
> > Â Â Â Â Â Â Â - static - mysite - assets - css - main.css
> >
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â - js
> >
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...
> > In my index.html
> >
> > add
> >
> > `{% load staticfiles % }
> > Â Â Â Â <link rel="stylesheet" text = 'html/css' href="{%
> static
> > 'mysite/static/main.css' %}" />`
> >
> > it said i have a Invalid block tag about TemplateSyntaxError
> > `<link rel="stylesheet" type = 'text/css' href="{% static
> > 'mysite/static/assets/css/main.css' %}" />`
> >
> > views.py (about static files part)
> > ```STATICFILES_DIRS = [
> > Â Â os.path.join(BASE_DIR, "static"),
> > Â Â '/mysite/static/',
> > ]
> > STATIC_URL = '/static/'
> > STATIC_ROOT='/mysite/static/'```
> >
> > what's part should i notice?
> >
> > --
> > 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 <javascript:>.
> > To post to this group, send email to django...@googlegroups.com
> <javascript:>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/ef2ea6eb-d1cb-40e2-abef-1af246e02e98%40googlegroups.com>.
>
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
> --
>
> Cordialement, Coues Ludovic
> +336 148 743 42
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a699584c-d57f-4b84-a38b-fe03638caefd%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a699584c-d57f-4b84-a38b-fe03638caefd%40googlegroups.com?utm_medium=email&utm_source=footer>.
announce.py
Message has been deleted
Message has been deleted

Jonathan Cheng

unread,
Sep 12, 2016, 2:57:36 PM9/12/16
to Django users
i use href="{% static '/assets/css/main.css' %}
and the css worked
but i want to use the all files in assets
is there any other way to do it?

ludovic coues於 2016年9月12日星期一 UTC+8上午1時32分42秒寫道:

ludovic coues

unread,
Sep 12, 2016, 6:14:10 PM9/12/16
to django...@googlegroups.com
manage.py collectstatic is made for that. It will copy every static file in your project into a single directory. Even those included in django like the css of django admin.


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

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

Jonathan Cheng

unread,
Sep 15, 2016, 3:05:58 PM9/15/16
to Django users
thx everyone,all bugs were fixed early.


Jonathan Cheng於 2016年9月11日星期日 UTC+8下午10時59分56秒寫道:

Jonathan Cheng

unread,
Sep 15, 2016, 3:10:24 PM9/15/16
to Django users
thx your code,though i cant run it in cmd

Mike Dewhirst於 2016年9月12日星期一 UTC+8上午10時05分10秒寫道:

Mike Dewhirst

unread,
Sep 16, 2016, 12:43:17 AM9/16/16
to django...@googlegroups.com
On 16/09/2016 1:10 AM, Jonathan Cheng wrote:
> thx your code,though i cant run it in cmd
That is correct. It is designed to be imported and called from settings.py.

I'll send you a screenshot off-line

Mike



Jonathan Cheng

unread,
Sep 16, 2016, 1:47:43 AM9/16/16
to Django users
after importing announce,directories in settings.py
if DEBUG:
        announce
(__name__, databases=DATABASES, backend="Postgres: 9.5", caches=CACHES)

it said CACHES isn't defined when i running the server
python manage.py runserver












Mike Dewhirst於 2016年9月16日星期五 UTC+8上午8時43分17秒寫道:

Mike Dewhirst

unread,
Sep 16, 2016, 2:11:37 AM9/16/16
to django...@googlegroups.com
On 16/09/2016 11:47 AM, Jonathan Cheng wrote:
> after importing announce,directories in settings.py
> |
> |
> ifDEBUG:
> Â Â Â Â announce(__name__,databases=DATABASES,backend="Postgres:
> 9.5",caches=CACHES)
> |
> |
> it said CACHES isn't defined when i running the server
> |
> python manage.py runserver
> |
>

Ok. In announce.py line 11 change caches to caches=None

11. def announce(name, databases, backend, caches=None):


I define CACHES *only* for my development settings and I *only* call
announce() during development.

If you decide to do this, put it behind a conditional ...

if DEBUG:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}

Mike



>
>
>
>
>
>
>
>
>
>
> Mike Dewhirst於 2016年9月16日星期五
> UTC+8ä¸Šå ˆ8時43分17ç§’å¯«é “ï¼š
>
> On 16/09/2016 1:10 AM, Jonathan Cheng wrote:
> > thx your code,though i cant run it in cmd
> That is correct. It is designed to be imported and called from
> settings.py.
>
> I'll send you a screenshot off-line
>
> Mike
>
>
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a9036fd7-62bc-42ca-901e-418eb57491ff%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a9036fd7-62bc-42ca-901e-418eb57491ff%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages