Advertisement
quantim

Setting up Bootstrap in Django

Apr 18th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. Assume the following directory structure:
  2.  
  3. ~/workspace/mysite/mysite
  4. ~/workspace/mysite/siteapp
  5.  
  6. Bootstrap files reside at either the project level (~/workspace/mysite/mysite/static/) or at the application level
  7. (~/workspace/mysite/siteapp/static/). Static files at the project level can be accessed by all applications. Static files at
  8. the application level can only be accessed by that particular application.
  9.  
  10.  
  11. //////////////////////////////////////////////////////////////
  12. For the Project level
  13. 1) Download Bootstrap
  14. $ cd ~/workspace/mysite/mysite/static
  15. $ wget https://github.com/twbs/bootstrap/releases/download/v3.3.6/bootstrap-3.3.6-dist.zip
  16. $ unzip bootstrap-3.3.6-dist.zip
  17. $ rm bootstrap-3.3.6-dist.zip
  18.  
  19. 2) Edit settings.py
  20. We need to edit settings.py to be able to find the new static files. Make sure settings.py contains the following code
  21.  
  22. INSTALLED_APPS = [
  23. 'siteapp',
  24. ...
  25. 'django.contrib.staticfiles',
  26. ]
  27.  
  28. ...
  29.  
  30. STATIC_URL = '/static/'
  31.  
  32. ...
  33.  
  34. # Additional locations of static files
  35. STATICFILES_DIRS = (
  36. # Put strings here, like "/home/html/static" or "C:/www/django/static".
  37. # Always use forward slashes, even on Windows.
  38. # Don't forget to use absolute paths, not relative paths.
  39. os.path.join(
  40. os.path.dirname(__file__),
  41. 'static',
  42. ),
  43. )
  44.  
  45. 3) Now, when we make our first template, we can simply include the bootstrap.min.css file as following:
  46.  
  47. {% load staticfiles %}
  48. <html>
  49. <head>
  50. <link href="{% static 'bootstrap-3.3.6-dist/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
  51. </head>
  52. ...
  53.  
  54. ///////////////////////////////////////////////////////////////////////////
  55. For the application level:
  56. 1) $ cd ~/workspace/mysite/siteapp/static/siteapp/
  57.  
  58. Install
  59.  
  60.  
  61. 2) Ensure the following code is present in settings.py
  62.  
  63. INSTALLED_APPS = [
  64. 'siteapp',
  65. ...
  66. 'django.contrib.staticfiles',
  67. ]
  68.  
  69. ...
  70.  
  71. STATIC_URL = '/static/'
  72.  
  73. 3) Include the following code in your templates:
  74. {% load staticfiles %}
  75. <link href="{% static 'bootstrap-3.3.6-dist/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
  76.  
  77. ////////////////////////////////////////////////
  78.  
  79. Finally, we want to create a base.html template which all other templates will inherit from. That way if we make a change to
  80. the bootstrap style-sheet, changes will be propagated to all other templates.
  81. Add the following code to ~/workspace/mysite/siteapp/templates/base.html
  82.  
  83. {% load staticfiles %}
  84. <html lang="en">
  85. <head>
  86. <link href="{% static 'bootstrap/css/bootstrap.min.css' %}"
  87. rel="stylesheet" media="screen">
  88. <meta charset="utf-8"/>
  89. <title>MySite</title>
  90. </head>
  91.  
  92. <body>
  93. {% block content %}
  94. {% endblock %}
  95.  
  96. <script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement