Re: Help implement CSS into Django!!!!

90 views
Skip to first unread message

Nikolas Stevenson-Molnar

unread,
Jan 5, 2013, 10:06:15 PM1/5/13
to django...@googlegroups.com
In a production environment, yes your static files (incl CSS) will need
to be served outside of Django (can be on the same server, but it's
generally recommended to use a separate serve or CDN). On your local
development server (where you're using the runserver command), you can
use the built-in staticfiles app. This document will give you the full
rundown: https://docs.djangoproject.com/en/1.4/howto/static-files/

Essentially, you add staticfiles to your installed apps in your
settings, tell it where to find your static content on disk, and then
use {{ STATIC_URL }} in your templates.

_Nik

On 1/5/2013 6:12 PM, Czaro wrote:
> I've been trying to get my CSS to work with my python powered site
> forever but I have no luck. I read the djangobook and a massive load
> of other resources but they all say a different thing and nothing
> worked for me. I would appreciate a few helpful steps on how to get my
> CSS to work.
> Some details:
> I;m in development mode.
> I'm using runserver and when i run it I only see the HTML and not the CSS.
> I'm also not sure if my CSS files need to be hosted on a separate server.
> Any help will be greatly appreciated.
> Thanks!!!!!!!!!!1
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/WpOUKKUuRD4J.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Abraham Yusuf

unread,
Jan 6, 2013, 8:43:49 AM1/6/13
to django...@googlegroups.com

On Sunday 06 January 2013 11:49:46 AM django...@googlegroups.com wrote:

Czaro Jan 05 06:12PM -0800   I've been trying to get my CSS to work with my python powered site forever but I have no luck. I read the djangobook and a massive load of other resources but they all say a different thing and nothing worked for me. I would appreciate a few helpful steps on how to get my CSS to work. Some details: I;m in development mode. I'm using runserver and when i run it I only see the HTML and not the CSS. I'm also not sure if my CSS files need to be hosted on a separate server. Any help will be greatly appreciated. Thanks!!!!!!!!!!1   Nikolas Stevenson-Molnar Jan 05 07:06PM -0800   In a production environment, yes your static files (incl CSS) will need to be served outside of Django (can be on the same server, but it's generally recommended to use a separate serve or CDN). On your local development server (where you're using the runserver command), you can use the built-in staticfiles app. This document will give you the full rundown: https://docs.djangoproject.com/en/1.4/howto/static-files/   Essentially, you add staticfiles to your installed apps in your settings, tell it where to find your static content on disk, and then use {{ STATIC_URL }} in your templates.   _Nik   On 1/5/2013 6:12 PM, Czaro wrote:



Hi Czaro,

 

I am assuming the following directory structure and django 1.4 or higher:

myproject/

manage.py

myproject/

__init__.py

settings.py

urls.py

wsgi.py

appname/

__init__.py

models.py

tests.py

views.py

static/

appname/

mystyle.css

 

Like Nikolas said, ensure that 'django.contrib.staticfiles' is in the installed apps, 'django.contrib.staticfiles.finders.AppDirectoriesFinder' is in the STATICFILES_FINDERS and STATIC_ROOT points to the absolute path to the directory where your static files should be collected to (eg STATIC_ROOT = '/home/user/dj_static/'). Also be sure that your css file is stored according to the above directory structure (in this case I named it 'mystyle.css'). Now, just leave your STATIC_URL pointing to '/static/' (the default). In your template, say in the head section, put <link href="{{ STATIC_URL }}/appname/mystyle.css" rel="stylesheet"></link>. Finally, run '$python manage.py collectstatic' in the shell, when prompted type 'yes', now run '$python manage.py runserver'. Goto the page in your browser.

 

Hope this was helpful.

 

--Ab

Abraham Yusuf

unread,
Jan 6, 2013, 9:16:27 AM1/6/13
to django...@googlegroups.com

On Sunday 06 January 2013 11:49:46 AM django...@googlegroups.com wrote:

When i set in STATIC_ROOT = '/home/domain/www/my_proj/htdocs/static'   In apache error log i got:   File does not exist: /home/domain/www/my_proj/my_proj/static   I don't get, why django don't want to take new setting...

Hi,

 

Check your apache virtualhost configuration. I looks like the paths are different (ie '/home/domain/www/my_proj/htdocs/static' != '/home/domain/www/my_proj/my_proj/static'). Besides, did you actually create '/home/domain/www/my_proj/htdocs/static'? Is it readable by the web server (as per permissions)?

 

Best of luck!

 

--Ab

Pedro J. Aramburu

unread,
Jan 6, 2013, 9:23:34 AM1/6/13
to django...@googlegroups.com
Czaro, to better understand the problem it will be good if you could tell us something more about how are you calling your css files from your templates and where are they located. I will also assume Django 1.4 to tell you some tips (mostly already covered).
  • Set your STATIC_ROOT.
    I use STATIC_ROOT = os.path.join(PROJECT_ROOT, 'public/static') where PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)). This gives me the folder where the settings file is, which is the "project level" folder. I use the public subfolder to put the static and media folders.
  • Set your STATICFILES_FINDERS so it can find your files if you have them on your apps.
    If they are in your apps the AppDirectoriesFinder will look on a static folder within the app. It's usually good to have a subfolder with the same name of the app (app_name/static/app_name) so there won't be collisions when you do the collectstatic operation.
  • In your templates you can
    1. <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/styles.css" />
      But you have to check if your static template context processor is there django.core.context_processors.static (I think it is by default).
    2. Using a template tag (which is more... template-friendly? It reads better I believe):
      {% load staticfiles %}
    3. <link rel="stylesheet" href="{% static "styles.css" %}" type="text/css" />
  • When you are on the builtin development server, Django will take care of looking where the file is. You only need the collectstatic operation on production time because if you do it one time, for example, django will default to that file first (I think), serving you maybe an old copy of the file. Collect static looks with the finders for the static files and copy them onto the STATIC_ROOT.
  • If you are using another development server (like apache) and you haven't pointed the static files from there you can add some tricks to the urls.py file https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-static-files-in-development and django will serve them too. For the built in runserver command it does it by default so you don't have to modify anything.
I think that's all. Let us know how did it go.

Ben Carleton

unread,
Jan 5, 2013, 9:50:34 PM1/5/13
to django...@googlegroups.com
On Jan 5, 2013, at 9:12 PM, Czaro <cezary...@gmail.com> wrote:

I've been trying to get my CSS to work with my python powered site forever but I have no luck. I read the djangobook and a massive load of other resources but they all say a different thing and nothing worked for me. I would appreciate a few helpful steps on how to get my CSS to work.
Some details:
I;m in development mode.
I'm using runserver and when i run it I only see the HTML and not the CSS.
I'm also not sure if my CSS files need to be hosted on a separate server.
Any help will be greatly appreciated.
Thanks!!!!!!!!!!1

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/WpOUKKUuRD4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Have you set STATICFILES_DIRS, STATICFILES_ROOT, and STATIC_URL in your settings.py and run "manage.py collectstatic"? You should be putting your files in the directory(s) you have in STATICFILES_DIRS and then running manage.py collectstatic. In development, runserver will serve your static files but in production django will not handle those URLs and you will need to either host them elsewhere or set up your web server to serve them.

Vincent Fulco

unread,
Apr 13, 2013, 10:52:58 AM4/13/13
to django...@googlegroups.com
Running a dev server, is the following required or not--> "manage.py collectstatic"? The official docs would indicate no and there is too much mis-information floating around so it is hard to verify one way or the other for a noob.  Django 1.4 would appear to be able to find static files in dev mode and only in an external production setup is the command required.  TIA, V. 
Reply all
Reply to author
Forward
0 new messages