add_to_django_project.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.core.middleware.SiteMiddleware',
  32. 'wagtail.contrib.redirects.middleware.RedirectMiddleware',
  33. ]
  34. Wagtail requires several common Django middleware modules to work and cover basic security. Wagtail provides its own middleware to cover these tasks:
  35. ``SiteMiddleware``
  36. Wagtail routes pre-defined hosts to pages within the Wagtail tree using this middleware.
  37. ``RedirectMiddleware``
  38. Wagtail provides a simple interface for adding arbitrary redirects to your site and this module makes it happen.
  39. Apps (``settings.py``)
  40. ~~~~~~~~~~~~~~~~~~~~~~
  41. .. code-block:: python
  42. INSTALLED_APPS = [
  43. 'myapp', # your own app
  44. 'wagtail.contrib.forms',
  45. 'wagtail.contrib.redirects',
  46. 'wagtail.embeds',
  47. 'wagtail.sites',
  48. 'wagtail.users',
  49. 'wagtail.snippets',
  50. 'wagtail.documents',
  51. 'wagtail.images',
  52. 'wagtail.search',
  53. 'wagtail.admin',
  54. 'wagtail.core',
  55. 'taggit',
  56. 'modelcluster',
  57. 'django.contrib.auth',
  58. 'django.contrib.contenttypes',
  59. 'django.contrib.sessions',
  60. 'django.contrib.messages',
  61. 'django.contrib.staticfiles',
  62. ]
  63. 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.
  64. Wagtail Apps
  65. ------------
  66. ``wagtailcore``
  67. The core functionality of Wagtail, such as the ``Page`` class, the Wagtail tree, and model fields.
  68. ``wagtailadmin``
  69. The administration interface for Wagtail, including page edit handlers.
  70. ``wagtaildocs``
  71. The Wagtail document content type.
  72. ``wagtailsnippets``
  73. Editing interface for non-Page models and objects. See :ref:`Snippets`.
  74. ``wagtailusers``
  75. User editing interface.
  76. ``wagtailimages``
  77. The Wagtail image content type.
  78. ``wagtailembeds``
  79. Module governing oEmbed and Embedly content in Wagtail rich text fields. See :ref:`inserting_videos`.
  80. ``wagtailsearch``
  81. Search framework for Page content. See :ref:`wagtailsearch`.
  82. ``wagtailredirects``
  83. Admin interface for creating arbitrary redirects on your site.
  84. ``wagtailforms``
  85. Models for creating forms on your pages and viewing submissions. See :ref:`form_builder`.
  86. Third-Party Apps
  87. ----------------
  88. ``taggit``
  89. 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`_.
  90. .. _Taggit Documentation: https://django-taggit.readthedocs.org/en/latest/index.html
  91. ``modelcluster``
  92. 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`_.
  93. .. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster
  94. URL Patterns
  95. ~~~~~~~~~~~~
  96. .. code-block:: python
  97. from django.contrib import admin
  98. from wagtail.core import urls as wagtail_urls
  99. from wagtail.admin import urls as wagtailadmin_urls
  100. from wagtail.documents import urls as wagtaildocs_urls
  101. urlpatterns = [
  102. re_path(r'^django-admin/', include(admin.site.urls)),
  103. re_path(r'^admin/', include(wagtailadmin_urls)),
  104. re_path(r'^documents/', include(wagtaildocs_urls)),
  105. # Optional URL for including your own vanilla Django urls/views
  106. re_path(r'', include('myapp.urls')),
  107. # For anything not caught by a more specific rule above, hand over to
  108. # Wagtail's serving mechanism
  109. re_path(r'', include(wagtail_urls)),
  110. ]
  111. This block of code for your project's ``urls.py`` does a few things:
  112. * Load the vanilla Django admin interface to ``/django-admin/``
  113. * Load the Wagtail admin and its various apps
  114. * 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)
  115. * Lets Wagtail handle any further URL dispatching.
  116. 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.
  117. .. _complete_example_config:
  118. Ready to Use Example Configuration Files
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. These two files should reside in your project directory (``myproject/myproject/``).
  121. ``settings.py``
  122. ---------------
  123. .. code-block:: python
  124. import os
  125. PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  126. BASE_DIR = os.path.dirname(PROJECT_DIR)
  127. DEBUG = True
  128. # Application definition
  129. INSTALLED_APPS = [
  130. 'myapp',
  131. 'wagtail.contrib.forms',
  132. 'wagtail.contrib.redirects',
  133. 'wagtail.embeds',
  134. 'wagtail.sites',
  135. 'wagtail.users',
  136. 'wagtail.snippets',
  137. 'wagtail.documents',
  138. 'wagtail.images',
  139. 'wagtail.search',
  140. 'wagtail.admin',
  141. 'wagtail.core',
  142. 'taggit',
  143. 'modelcluster',
  144. 'django.contrib.auth',
  145. 'django.contrib.contenttypes',
  146. 'django.contrib.sessions',
  147. 'django.contrib.messages',
  148. 'django.contrib.staticfiles',
  149. ]
  150. MIDDLEWARE = [
  151. 'django.contrib.sessions.middleware.SessionMiddleware',
  152. 'django.middleware.common.CommonMiddleware',
  153. 'django.middleware.csrf.CsrfViewMiddleware',
  154. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  155. 'django.contrib.messages.middleware.MessageMiddleware',
  156. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  157. 'django.middleware.security.SecurityMiddleware',
  158. 'wagtail.core.middleware.SiteMiddleware',
  159. 'wagtail.contrib.redirects.middleware.RedirectMiddleware',
  160. ]
  161. ROOT_URLCONF = 'myproject.urls'
  162. TEMPLATES = [
  163. {
  164. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  165. 'DIRS': [
  166. os.path.join(PROJECT_DIR, 'templates'),
  167. ],
  168. 'APP_DIRS': True,
  169. 'OPTIONS': {
  170. 'context_processors': [
  171. 'django.template.context_processors.debug',
  172. 'django.template.context_processors.request',
  173. 'django.contrib.auth.context_processors.auth',
  174. 'django.contrib.messages.context_processors.messages',
  175. ],
  176. },
  177. },
  178. ]
  179. WSGI_APPLICATION = 'myproject.wsgi.application'
  180. # Database
  181. DATABASES = {
  182. 'default': {
  183. 'ENGINE': 'django.db.backends.postgresql',
  184. 'NAME': 'myprojectdb',
  185. 'USER': 'postgres',
  186. 'PASSWORD': '',
  187. 'HOST': '', # Set to empty string for localhost.
  188. 'PORT': '', # Set to empty string for default.
  189. 'CONN_MAX_AGE': 600, # number of seconds database connections should persist for
  190. }
  191. }
  192. # Internationalization
  193. LANGUAGE_CODE = 'en-us'
  194. TIME_ZONE = 'UTC'
  195. USE_I18N = True
  196. USE_L10N = True
  197. USE_TZ = True
  198. # Static files (CSS, JavaScript, Images)
  199. STATICFILES_FINDERS = [
  200. 'django.contrib.staticfiles.finders.FileSystemFinder',
  201. 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  202. ]
  203. STATICFILES_DIRS = [
  204. os.path.join(PROJECT_DIR, 'static'),
  205. ]
  206. STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  207. STATIC_URL = '/static/'
  208. MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  209. MEDIA_URL = '/media/'
  210. ADMINS = [
  211. # ('Your Name', 'your_email@example.com'),
  212. ]
  213. MANAGERS = ADMINS
  214. # Default to dummy email backend. Configure dev/production/local backend
  215. # as per https://docs.djangoproject.com/en/stable/topics/email/#email-backends
  216. EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'
  217. # Hosts/domain names that are valid for this site; required if DEBUG is False
  218. ALLOWED_HOSTS = []
  219. # Make this unique, and don't share it with anybody.
  220. SECRET_KEY = 'change-me'
  221. EMAIL_SUBJECT_PREFIX = '[Wagtail] '
  222. INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
  223. # A sample logging configuration. The only tangible logging
  224. # performed by this configuration is to send an email to
  225. # the site admins on every HTTP 500 error when DEBUG=False.
  226. # See https://docs.djangoproject.com/en/stable/topics/logging for
  227. # more details on how to customize your logging configuration.
  228. LOGGING = {
  229. 'version': 1,
  230. 'disable_existing_loggers': False,
  231. 'filters': {
  232. 'require_debug_false': {
  233. '()': 'django.utils.log.RequireDebugFalse'
  234. }
  235. },
  236. 'handlers': {
  237. 'mail_admins': {
  238. 'level': 'ERROR',
  239. 'filters': ['require_debug_false'],
  240. 'class': 'django.utils.log.AdminEmailHandler'
  241. }
  242. },
  243. 'loggers': {
  244. 'django.request': {
  245. 'handlers': ['mail_admins'],
  246. 'level': 'ERROR',
  247. 'propagate': True,
  248. },
  249. }
  250. }
  251. # WAGTAIL SETTINGS
  252. # This is the human-readable name of your Wagtail install
  253. # which welcomes users upon login to the Wagtail admin.
  254. WAGTAIL_SITE_NAME = 'My Project'
  255. # Override the search results template for wagtailsearch
  256. # WAGTAILSEARCH_RESULTS_TEMPLATE = 'myapp/search_results.html'
  257. # WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX = 'myapp/includes/search_listing.html'
  258. # Replace the search backend
  259. #WAGTAILSEARCH_BACKENDS = {
  260. # 'default': {
  261. # 'BACKEND': 'wagtail.search.backends.elasticsearch2',
  262. # 'INDEX': 'myapp'
  263. # }
  264. #}
  265. # Wagtail email notifications from address
  266. # WAGTAILADMIN_NOTIFICATION_FROM_EMAIL = 'wagtail@myhost.io'
  267. # Wagtail email notification format
  268. # WAGTAILADMIN_NOTIFICATION_USE_HTML = True
  269. # Reverse the default case-sensitive handling of tags
  270. TAGGIT_CASE_INSENSITIVE = True
  271. ``urls.py``
  272. -----------
  273. .. code-block:: python
  274. from django.conf.urls import include, re_path
  275. from django.conf.urls.static import static
  276. from django.views.generic.base import RedirectView
  277. from django.contrib import admin
  278. from django.conf import settings
  279. import os.path
  280. from wagtail.core import urls as wagtail_urls
  281. from wagtail.admin import urls as wagtailadmin_urls
  282. from wagtail.documents import urls as wagtaildocs_urls
  283. urlpatterns = [
  284. re_path(r'^django-admin/', include(admin.site.urls)),
  285. re_path(r'^admin/', include(wagtailadmin_urls)),
  286. re_path(r'^documents/', include(wagtaildocs_urls)),
  287. # For anything not caught by a more specific rule above, hand over to
  288. # Wagtail's serving mechanism
  289. re_path(r'', include(wagtail_urls)),
  290. ]
  291. if settings.DEBUG:
  292. from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  293. urlpatterns += staticfiles_urlpatterns() # tell gunicorn where static files are in dev mode
  294. urlpatterns += static(settings.MEDIA_URL + 'images/', document_root=os.path.join(settings.MEDIA_ROOT, 'images'))
  295. urlpatterns += [
  296. re_path(r'^favicon\.ico$', RedirectView.as_view(url=settings.STATIC_URL + 'myapp/images/favicon.ico'))
  297. ]