Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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})
like image 796
Jon Avatar asked Oct 07 '14 13:10

Jon


1 Answers

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.

like image 200
Jon Avatar answered Nov 08 '22 19:11

Jon