Sunday, January 6, 2013

Re: Help implement CSS into Django!!!!

On Sunday 06 January 2013 11:49:46 AM django-users@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

No comments:

Post a Comment