add_to_django_project.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. ==================================================
  2. How to add Wagtail into an existing Django project
  3. ==================================================
  4. To install Wagtail completely from scratch, create a new Django project and an app within that project. For instructions on these tasks, see :doc:`Writing your first Django app <django:intro/tutorial01>`. Your project directory will look like the following::
  5. myproject/
  6. myproject/
  7. __init__.py
  8. settings.py
  9. urls.py
  10. wsgi.py
  11. myapp/
  12. __init__.py
  13. models.py
  14. tests.py
  15. admin.py
  16. views.py
  17. manage.py
  18. From your app directory, you can safely remove ``admin.py`` and ``views.py``, since Wagtail will provide this functionality for your models. Configuring Django to load Wagtail involves adding modules and variables to ``settings.py`` and URL configuration to ``urls.py``. For a more complete view of what's defined in these files, see :doc:`Django Settings <django:topics/settings>` and :doc:`Django URL Dispatcher <django:topics/http/urls>`.
  19. What follows is a settings reference which skips many boilerplate Django settings. If you just want to get your Wagtail install up quickly without fussing with settings at the moment, see :ref:`complete_example_config`.
  20. Middleware (``settings.py``)
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. .. code-block:: python
  23. MIDDLEWARE = [
  24. 'django.contrib.sessions.middleware.SessionMiddleware',
  25. 'django.middleware.common.CommonMiddleware',
  26. 'django.middleware.csrf.CsrfViewMiddleware',
  27. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  28. 'django.contrib.messages.middleware.MessageMiddleware',
  29. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  30. 'django.middleware.security.SecurityMiddleware',
  31. 'wagtail.contrib.redirects.middleware.RedirectMiddleware',
  32. ]
  33. Wagtail depends on the default set of Django middleware modules, to cover basic security and functionality such as login sessions. One additional middleware module is provided:
  34. ``RedirectMiddleware``
  35. Wagtail provides a simple interface for adding arbitrary redirects to your site and this module makes it happen.
  36. Apps (``settings.py``)
  37. ~~~~~~~~~~~~~~~~~~~~~~
  38. .. code-block:: python
  39. INSTALLED_APPS = [
  40. 'myapp', # your own app
  41. 'wagtail.contrib.forms',
  42. 'wagtail.contrib.redirects',
  43. 'wagtail.embeds',
  44. 'wagtail.sites',
  45. 'wagtail.users',
  46. 'wagtail.snippets',
  47. 'wagtail.documents',
  48. 'wagtail.images',
  49. 'wagtail.search',
  50. 'wagtail.admin',
  51. 'wagtail.core',
  52. 'taggit',
  53. 'modelcluster',
  54. 'django.contrib.auth',
  55. 'django.contrib.contenttypes',
  56. 'django.contrib.sessions',
  57. 'django.contrib.messages',
  58. 'django.contrib.staticfiles',
  59. ]
  60. Wagtail requires several Django app modules, third-party apps, and defines several apps of its own. Wagtail was built to be modular, so many Wagtail apps can be omitted to suit your needs. Your own app (here ``myapp``) is where you define your models, templates, static assets, template tags, and other custom functionality for your site.
  61. Wagtail Apps
  62. ------------
  63. ``wagtail.core``
  64. The core functionality of Wagtail, such as the ``Page`` class, the Wagtail tree, and model fields.
  65. ``wagtail.admin``
  66. The administration interface for Wagtail, including page edit handlers.
  67. ``wagtail.documents``
  68. The Wagtail document content type.
  69. ``wagtail.snippets``
  70. Editing interface for non-Page models and objects. See :ref:`Snippets`.
  71. ``wagtail.users``
  72. User editing interface.
  73. ``wagtail.images``
  74. The Wagtail image content type.
  75. ``wagtail.embeds``
  76. Module governing oEmbed and Embedly content in Wagtail rich text fields. See :ref:`inserting_videos`.
  77. ``wagtail.search``
  78. Search framework for Page content. See :ref:`wagtailsearch`.
  79. ``wagtail.sites``
  80. Management UI for Wagtail sites.
  81. ``wagtail.contrib.redirects``
  82. Admin interface for creating arbitrary redirects on your site.
  83. ``wagtail.contrib.forms``
  84. Models for creating forms on your pages and viewing submissions. See :ref:`form_builder`.
  85. Third-Party Apps
  86. ----------------
  87. ``taggit``
  88. Tagging framework for Django. This is used internally within Wagtail for image and document tagging and is available for your own models as well. See :ref:`tagging` for a Wagtail model recipe or the `Taggit Documentation`_.
  89. .. _Taggit Documentation: https://django-taggit.readthedocs.org/en/latest/index.html
  90. ``modelcluster``
  91. Extension of Django ForeignKey relation functionality, which is used in Wagtail pages for on-the-fly related object creation. For more information, see :ref:`inline_panels` or `the django-modelcluster github project page`_.
  92. .. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster
  93. URL Patterns
  94. ~~~~~~~~~~~~
  95. .. code-block:: python
  96. from django.contrib import admin
  97. from wagtail.core import urls as wagtail_urls
  98. from wagtail.admin import urls as wagtailadmin_urls
  99. from wagtail.documents import urls as wagtaildocs_urls
  100. urlpatterns = [
  101. path('django-admin/', admin.site.urls),
  102. path('admin/', include(wagtailadmin_urls)),
  103. path('documents/', include(wagtaildocs_urls)),
  104. # Optional URL for including your own vanilla Django urls/views
  105. re_path(r'', include('myapp.urls')),
  106. # For anything not caught by a more specific rule above, hand over to
  107. # Wagtail's serving mechanism
  108. re_path(r'', include(wagtail_urls)),
  109. ]
  110. This block of code for your project's ``urls.py`` does a few things:
  111. * Load the vanilla Django admin interface to ``/django-admin/``
  112. * Load the Wagtail admin and its various apps
  113. * Dispatch any vanilla Django apps you're using other than Wagtail which require their own URL configuration (this is optional, since Wagtail might be all you need)
  114. * Lets Wagtail handle any further URL dispatching.
  115. That's not everything you might want to include in your project's URL configuration, but it's what's necessary for Wagtail to flourish.
  116. .. _complete_example_config:
  117. Ready to Use Example Configuration Files
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. These two files should reside in your project directory (``myproject/myproject/``).
  120. ``settings.py``
  121. ---------------
  122. .. code-block:: python
  123. import os
  124. PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  125. BASE_DIR = os.path.dirname(PROJECT_DIR)
  126. DEBUG = True
  127. # Application definition
  128. INSTALLED_APPS = [
  129. 'myapp',
  130. 'wagtail.contrib.forms',
  131. 'wagtail.contrib.redirects',
  132. 'wagtail.embeds',
  133. 'wagtail.sites',
  134. 'wagtail.users',
  135. 'wagtail.snippets',
  136. 'wagtail.documents',
  137. 'wagtail.images',
  138. 'wagtail.search',
  139. 'wagtail.admin',
  140. 'wagtail.core',
  141. 'taggit',
  142. 'modelcluster',
  143. 'django.contrib.auth',
  144. 'django.contrib.contenttypes',
  145. 'django.contrib.sessions',
  146. 'django.contrib.messages',
  147. 'django.contrib.staticfiles',
  148. ]
  149. MIDDLEWARE = [
  150. 'django.contrib.sessions.middleware.SessionMiddleware',
  151. 'django.middleware.common.CommonMiddleware',
  152. 'django.middleware.csrf.CsrfViewMiddleware',
  153. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  154. 'django.contrib.messages.middleware.MessageMiddleware',
  155. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  156. 'django.middleware.security.SecurityMiddleware',
  157. 'wagtail.contrib.redirects.middleware.RedirectMiddleware',
  158. ]
  159. ROOT_URLCONF = 'myproject.urls'
  160. TEMPLATES = [
  161. {
  162. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  163. 'DIRS': [
  164. os.path.join(PROJECT_DIR, 'templates'),
  165. ],
  166. 'APP_DIRS': True,
  167. 'OPTIONS': {
  168. 'context_processors': [
  169. 'django.template.context_processors.debug',
  170. 'django.template.context_processors.request',
  171. 'django.contrib.auth.context_processors.auth',
  172. 'django.contrib.messages.context_processors.messages',
  173. ],
  174. },
  175. },
  176. ]
  177. WSGI_APPLICATION = 'myproject.wsgi.application'
  178. # Database
  179. DATABASES = {
  180. 'default': {
  181. 'ENGINE': 'django.db.backends.postgresql',
  182. 'NAME': 'myprojectdb',
  183. 'USER': 'postgres',
  184. 'PASSWORD': '',
  185. 'HOST': '', # Set to empty string for localhost.
  186. 'PORT': '', # Set to empty string for default.
  187. 'CONN_MAX_AGE': 600, # number of seconds database connections should persist for
  188. }
  189. }
  190. # Internationalization
  191. LANGUAGE_CODE = 'en-us'
  192. TIME_ZONE = 'UTC'
  193. USE_I18N = True
  194. USE_L10N = True
  195. USE_TZ = True
  196. # Static files (CSS, JavaScript, Images)
  197. STATICFILES_FINDERS = [
  198. 'django.contrib.staticfiles.finders.FileSystemFinder',
  199. 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  200. ]
  201. STATICFILES_DIRS = [
  202. os.path.join(PROJECT_DIR, 'static'),
  203. ]
  204. STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  205. STATIC_URL = '/static/'
  206. MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  207. MEDIA_URL = '/media/'
  208. ADMINS = [
  209. # ('Your Name', 'your_email@example.com'),
  210. ]
  211. MANAGERS = ADMINS
  212. # Default to dummy email backend. Configure dev/production/local backend
  213. # as per https://docs.djangoproject.com/en/stable/topics/email/#email-backends
  214. EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'
  215. # Hosts/domain names that are valid for this site; required if DEBUG is False
  216. ALLOWED_HOSTS = []
  217. # Make this unique, and don't share it with anybody.
  218. SECRET_KEY = 'change-me'
  219. EMAIL_SUBJECT_PREFIX = '[Wagtail] '
  220. INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
  221. # A sample logging configuration. The only tangible logging
  222. # performed by this configuration is to send an email to
  223. # the site admins on every HTTP 500 error when DEBUG=False.
  224. # See https://docs.djangoproject.com/en/stable/topics/logging for
  225. # more details on how to customize your logging configuration.
  226. LOGGING = {
  227. 'version': 1,
  228. 'disable_existing_loggers': False,
  229. 'filters': {
  230. 'require_debug_false': {
  231. '()': 'django.utils.log.RequireDebugFalse'
  232. }
  233. },
  234. 'handlers': {
  235. 'mail_admins': {
  236. 'level': 'ERROR',
  237. 'filters': ['require_debug_false'],
  238. 'class': 'django.utils.log.AdminEmailHandler'
  239. }
  240. },
  241. 'loggers': {
  242. 'django.request': {
  243. 'handlers': ['mail_admins'],
  244. 'level': 'ERROR',
  245. 'propagate': True,
  246. },
  247. }
  248. }
  249. # WAGTAIL SETTINGS
  250. # This is the human-readable name of your Wagtail install
  251. # which welcomes users upon login to the Wagtail admin.
  252. WAGTAIL_SITE_NAME = 'My Project'
  253. # Replace the search backend
  254. #WAGTAILSEARCH_BACKENDS = {
  255. # 'default': {
  256. # 'BACKEND': 'wagtail.search.backends.elasticsearch5',
  257. # 'INDEX': 'myapp'
  258. # }
  259. #}
  260. # Wagtail email notifications from address
  261. # WAGTAILADMIN_NOTIFICATION_FROM_EMAIL = 'wagtail@myhost.io'
  262. # Wagtail email notification format
  263. # WAGTAILADMIN_NOTIFICATION_USE_HTML = True
  264. # Reverse the default case-sensitive handling of tags
  265. TAGGIT_CASE_INSENSITIVE = True
  266. ``urls.py``
  267. -----------
  268. .. code-block:: python
  269. from django.urls import include, path, re_path
  270. from django.conf.urls.static import static
  271. from django.views.generic.base import RedirectView
  272. from django.contrib import admin
  273. from django.conf import settings
  274. import os.path
  275. from wagtail.core import urls as wagtail_urls
  276. from wagtail.admin import urls as wagtailadmin_urls
  277. from wagtail.documents import urls as wagtaildocs_urls
  278. urlpatterns = [
  279. path('django-admin/', admin.site.urls),
  280. path('admin/', include(wagtailadmin_urls)),
  281. path('documents/', include(wagtaildocs_urls)),
  282. # For anything not caught by a more specific rule above, hand over to
  283. # Wagtail's serving mechanism
  284. re_path(r'', include(wagtail_urls)),
  285. ]
  286. if settings.DEBUG:
  287. from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  288. urlpatterns += staticfiles_urlpatterns() # tell gunicorn where static files are in dev mode
  289. urlpatterns += static(settings.MEDIA_URL + 'images/', document_root=os.path.join(settings.MEDIA_ROOT, 'images'))
  290. urlpatterns += [
  291. path('favicon.ico', RedirectView.as_view(url=settings.STATIC_URL + 'myapp/images/favicon.ico'))
  292. ]