Maxsteel Maxsteel - 7 months ago 21
Python Question

Bootstrap template with Django

I have a complete skeleton of a website in Bootstrap 3. I am very new to Django (not to Python though). I want to integrate this particular theme and build the backend using Django. I cannot find any website for instructions on this. How can this be done? Any help is appreciated.

Answer

You just need to prepare your templates that you do usually.In your settings file, make the following changes.

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_BASE_PATH, 'templates').replace('\\', '/'),
    )

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    )

STATIC_URL = '/static/'

MEDIA_URL = 'media/'

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)

Change your urls.py file to include the urls for static files. Like this:

urlpatterns = patterns('',

    url(r'^$',LandingPage.as_view(),name='home'),)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Place your templates in a templates folder inside your app/project and all the css, js and images inside static folder. Be sure to to include {{STATIC_URL}} before all css,js and image urls inside all the templates. Something like this:

<script src="{{STATIC_URL}}js/jquery.js"></script>

For more information on handling templates in django, try this

Comments