Jon
Jon

Reputation: 3204

Link stylesheets to Django template

I have been looking at this tutorial and now have a stylesheet in /static/styles/.

The problem is that the template doesn't pick this up:

<html>
    <head>    
        <link rel="stylesheet" type="text/css" href="static/css/stylesheet.css" />
        <title>Search</title>
    </head>
    <body>
        ...

Do I need something in my settings file? Where am I going wrong?

My project structure is:

project
    - manage.py
    - project
        - static
        - templates
        -  __init__
        - etc..

EDIT My urls.py now looks like this:

from django.conf.urls import patterns, include, url
from bible import views
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'bible.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^verses/', views.search),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

And this is my view.py file

from django.http import HttpResponse, Http404
from django.shortcuts import render
from bible.models import TBbe, TBookNames


def search(request):
    errors = []
    if 'b' in request.GET:
        if 'c' in request.GET:
            if 'v' in request.GET:
                book = request.GET['b']
                chapter = request.GET['c']
                verse = request.GET['v']
                verses = TBbe.objects.filter(b=book, c=chapter, v=verse)
                book = TBookNames.objects.filter(id=book)
                books = TBookNames.objects.all;
                return render(request, 'verses.html', {'verses': verses, 'book': book, 'books':   books})
            else:
                raise Http404()
        else:
            raise Http404()
    else:
        books = TBookNames.objects.all;
        return render(request, 'search_verses.html', {'books': books})

Upvotes: 4

Views: 15529

Answers (4)

cdvv7788
cdvv7788

Reputation: 2099

  1. Make sure you have your static directory defined in your settings.py
  2. Add the following to your template where needed:
{% load staticfiles %}
{% static 'css/mystyle.css' %}

Upvotes: 2

Jagrut Trivedi
Jagrut Trivedi

Reputation: 1319

You need to add below line to your settings.py along with load static and static rules plus debug mode true. No need to add anything into urls.py file. Django will automatically find static folder inside each app folder. This I found from the documents https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-STATICFILES_FINDERS.

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

Upvotes: 2

Jon
Jon

Reputation: 3204

The problem seemed to be that I needed the following code in my settings.py file:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),
)

which is the same as the code to reference my templates folder. If you put the static files at the same level, it should work. This is also providing you have followed the advice of everyone on here, importing static etc.

Upvotes: 5

Daniel Roseman
Daniel Roseman

Reputation: 599698

For a start, you should have a leading slash:

href="/static/css/stylesheet.css" 

but really you should use the static tag:

{% load static %}
...
href="{% static 'css/stylesheet.css' %}"

Upvotes: 0

Related Questions