Skip to content

Instantly share code, notes, and snippets.

@tombola
Last active June 9, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tombola/86c20413aacb4f09989a6fa7934a79ea to your computer and use it in GitHub Desktop.
Save tombola/86c20413aacb4f09989a6fa7934a79ea to your computer and use it in GitHub Desktop.

How do wagtail and django relate?

Django

is a web framework

Used by Instagram, Spotify, The Washington Post, loads of other big and small sites.

There are lots of different ways you can use python. Your end goal might be to use python to process data from spreadsheets, bulk edit a load of images, or analyse genetic code, anything really.

You might write a simple script that you run from the command line, or distribute a more sophisticated command line applications (essentially more general purpose scripts with different commands you can use). You might even package this up into a graphical interface that can be used from the computer desktop.

One of those things you might choose to do is to build a web application. This is a program that basically sits and listens for requests that come to it when people access its address on the web. Typically they will do this from their computer or phone via a browser (eg chrome/safari). When it gets a request, it works out what to send back and generally supplies an html document that will be rendered in the browser (a web page).

https://tutorial.djangogirls.org/en/how_the_internet_works/

You could just put a plain html document up on a web server and people would access that directly. But that document would be the same every time you access it, and the same to every person that accessed it. Also it might be a bit difficult to re-use stuff between pages, like menus, links etc. You would have to edit and re-upload each file every time you changed something.

You could write a python program that writes that html for you, and then upload the result (see static site generators). That would make it easy to re-use stuff between pages, and probably simpler to edit just your content and not all the other HTML around it. Every visitor would still see the same content, and you would still have to generate and upload each time you changed something. In some cases this is still a really good idea, because it is simple HTML that we are uploading and that will be fast to view (and simple/cheap to host).

If you want to do something a bit more complex on your website, like only allow certain people to view content, allow other people to contribute, or fetch information from somewhere else, then you will normally need it to be dynamic, and respond intelligently to each request separately.

It would be complicated to do this from scratch, and there would be a lot to do before you got to do anything interesting with your actual website.

If you are working in python this is where Django (or another web framework like flask) comes in. It listens for those requests, does some work to interpet what is being requested, and then passes that information on to your code. When you have determined what you want to send back, you can hand that back to django and it sends a response back to the browser.

Django is a big box of tools to help you interpret a request and then build a response. It provides some conventions about to structure your project, so that it can find what it needs, and that also helps when you want to work with other people, or get support, because you are using those shared ways of doing things.

It's an open source project, so as people have run up against things they want to do, or could do better, they add solutions to django itself. It has been around a long time, but its used so heavily it has been continuously brought up to date. That also means some greally clever developers have thought about possible security holes which would allow people to hack your site and tried to make sure they are less likely by default.

Some of the important things Django does for you:

  • Connects to a database where you can store your content
  • Returns content from the database as simple objects that you can use easily
  • Updates and saves those objects back into the database when needed
  • Works out what a change to your code means for how the database should be structured, and writes migrations to script those changes
  • Allows you to configure what happens at specific paths on your site, eg show a blog post at /blog/first-post, or delete a comment at /comments/123/delete (arbitrary examples)
  • Provides a template system to make it simpler to write html and insert bits of dynamic content into it
  • Simplifies making forms to collect user input, validate that input and acting on it
  • Settings variables (with sensible defaults) that allow you to configure custom apps to install, or how to handle cookies and time zones etc
  • Sending email from your website
  • Uploading and storing images

This is in no way an exhaustive list!

Wagtail

is a CMS, built using Django

Variously used by a lot of people, including nasa, mozilla, Hilary clinton/bernie sanders campaign websites, and many others

Django lets you do almost anything that responds to a web request.

But there are whole sets of websites that aim to do similar things when they receive a request. One of these categories is websites that allow some authorised users to edit content, but others to only view that content.

Other common features in this category are allowing an editor to save drafts of new content, and only publish them when ready, or provide a hierarchical menu that allows you to access content at a path according to its type and id or title. Attaching images to content, and resizing images to work on different sized displays is another example.

Wagtail also has a killer feature called streamfields which allows editors to make a flow of content out of a set of building blocks you as a developer have defined. Things like image blocks and embeds come with wagtail, but you can also put together your own, like a slideshow block, or a 'description of a cat block' with image, specific fields etc. The editor could then add slideshows and cats to the page to their heart's content, re-order, update and remove them. This really helps with giving editors freedom whilst allowing you to maintain consistency by say insisting that a cat always has a name, and that the image can always be styled with rounded images and margins.

You could do all of this yourself with a web framework like django, but quite a lot of the specifics have already been resolved by an open source CMS like Wagtail, with thousands of hours spent on the details.

A Wagtail site is still a Django site, in fact you can add wagtail to an existing django site to get the advanced content editing interface. Because of that a wagtail site is in no way limited to just being a CMS, you can add anything onto it, you will just be starting with a solid admin experience for creating pages.

Summary

If the basic description of a CMS fits what you are trying to achieve, it is much simpler and less frustrating and time consuming to start from there and build the bits that are specific to your project than to write all the content management features yourself yourself with django.

As a general rule of thumb if your web project has pages, and editors, then you could consider using Wagtail to build your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment