2

I not sure how to include css to my html. I have an app in my project that has it's own template folder. I want to get the sample.css into my html.

Something in the lines of <link rel="stylesheet" type="text/css" href="{% %}">

Not really sure what to put in between {% %}

structure:

/proj
   /app
      /templates
         sample.css
         sample.html

Here is my template_dirs:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    os.path.join(BASE_DIR,'home/templates'),
    os.path.join(BASE_DIR,'gallery/templates'),
    os.path.join(BASE_DIR,'contact/templates'),
    os.path.join(BASE_DIR,'aboutme/templates'),
)

If any other info is required. Please let me know!

updated struture

/proj
   /app
      /static
         /css
            sample.css
      /templates
         sample.html

2 Answers 2

2

css files, as well as js files and images, are considered static files.

The usual approach is to use django.contrib.staticfiles built-in django app to manage static files.

If you set up the app correctly this is how your link would look like:

<link rel="stylesheet" type="text/css" href="{% static 'sample.css' %}">
Sign up to request clarification or add additional context in comments.

9 Comments

@Liondancer Also you should put css files in other folder. Its a common pattern to put them in a folder called app/static/css.
@RaydelMiranda so rename my template to static?
@Liondancer don't mix up templates and static files. These are two complete separate parts of your app. Templates live in templates directory, static files live in static directory.
@crozzfire yeah, this is even better, but, for starters, the OP should at least separate templates and static files and configure it to work. You know, making it work for the first time could be challenging.
@Liondancer, what are you waiting for!? This looks as a solid answer to me. ;)
|
2

as @alecxe mentions, have them in a folder named static and include these in your settings file.

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.