Using Wagtail as an API layer - python

My company is evaluating Wagtail as a CMS for parts of our website. Currently we're running Python 2.7 and Django 1.5 (don't ask...). We have the ability to run Wagtail on a separate instance which can include the most current versions of Python/Django but we will not be able to run Wagtail out of the box within our main application.
We're looking at using Wagtail strictly as a CMS, then proxying requests from our main website to the Wagtail instance and returning just the generated markup.
Is there anyone who has done something like this, of could offer insight into the process we might take? Does Wagtail offer functionality like this out of the box? What potential pitfalls might we encounter, or things we should watch out for?
This could mean that instead of "entire pages" stored in Wagtail, we treat it as a way to store distinct content fragments: A paragraph of text which would be loaded into our homepage, or the outer wrapper of a dynamic search results page.

Yes, Wagtail does offer functionality like this, via its API:
http://docs.wagtail.io/en/v2.1/advanced_topics/api/
You could consume the API from the front-end of your main website, using JavaScript (React and Vue are popular options for this approach, but they aren't necessary if you don't need a complex Single Page Application with routing etc), or from the back-end, making HTTP requests from the views of your Django 1.5 app.
As for potential pitfalls, the main issue is that Wagtail previews won't work out of the box, since Wagtail doesn't know about how the content will be rendered. If you have a predictable URL structure on the site that's rendering Wagtail API content, the preview mechanism can be configured to handle this.
If the API approach isn't what you have in mind, you could also consider ways of embedding rendered fragments in the main site. For example, if you use Varnish in front of the main site, you could take advantage of Edge Side Includes:
https://varnish-cache.org/docs/3.0/tutorial/esi.html
Finally, you may find this recent talk on Wagtail as a 'headless' CMS useful:
https://www.youtube.com/watch?v=HZT14u6WwdY (video)
https://docs.google.com/presentation/d/1ZYMogOeXKCCmr7hDZnzx0euw2pD5VCwtv3a7zHB4FW0/ (slides)
https://github.com/openstax/openstax-cms (code)

Related

Django is rendering template from the database efficient

This is not a HOWTO question, as it, as such, has been answered before here
I am trying to integrate Django with modern frontend framework, and I found that it is possible to store and render Django templates from models. Since it is not a standard, I am wondering what are the advantages (or disadvantages if that's the case) of file based templates.
Reading though the documentation, I have seen that it is recommended to actually cache templates and models as much as possible for best performance, so why would it not be recommended to store templates in the database? It seems very convenient to me that in doing so pages can be edited from the admin panel (where you can add a code editor), which, along with the rest framework and a front end framework synergize very well.
From my research, the template tags and template language seem to work and the context can be passed in a view as well. the only thing I cannot figure out is the {include .. } tag, as it does not seem to point to a view. (although a custom tag cam be made to have this function)
Can such a setup be used in production? or are there security/performance/other concerns?
It's not recommended because templates are the developer's responsibility, not the site admin's.
Otherwise, there's not much performance difference between reading files directly from filesystem and from database.
One drawback of this approach is you don't get revision history (using git, mercurial etc). Sure, you can implement something similar to save the revisions in the database, but then why not use the better tools which already exist?
There's a Django flatpages app which lets you save HTML content in database. But the purpose of the app is to allow site admins to edit HTML content, such as information for an "About Us" page, because writing this info is not a developer's responsibility.

Good Implementation : Isomorphic react app + Python backend

What is the best way to implement isomorphic react app using webpack with python as backend to serve
Following features are required to be implemented
1. React router
2. Redux store
By Isomorphic, I understand you mean both rendered on the server & on the client - so the first access of the site will return static HTML and a javascript bundle which then replaces the static HTML with the react-rendered version.
Usually this would mean running javascript on the server too with Node.js. This means you can render your JSX / React templates / components on the server and send them as static HTML to the user. If you're working with Python on the server, you cannot use your JSX / Javascript templating & logic on the server, so you'll need to duplicate it in Python.
So since you have to do it all twice - you first need to work out which one you want to do first. Either in Python first, or Javascript.
You can either start by building the whole application server-side-rendered, and then have javascript take over, or build a fully javascript application, and render the first views with Python.
Personally, for content-driven sites, I prefer building a server-side rendered application with Django (the most used Python Web Framework), and then adding Javascript on top once it's all fully working as a javascriptless site. This has the advantage of working even when javascript is disabled, and ensures you have good URLs, etc.
Of course, if it's a really complex application in terms of user interaction, you'll need to do it the other way.
I recomend looking into Django first, Here is a great tutorial from django-girls. However, if you want to go the mainly-JS route, here's some ideas:
Javascript First
Probably the best way is to first design your data structure(s), and make a REST api for your data. (e.g. /api/1.0/cars/rustbucket_94 which sends JSON data.)
Next, figure out your user-facing URL schema, and work out which REST endpoints need to be pulled in to get those pages. (How the URLs should look to the end users. e.g. /cars/rustbucket_94.html)
Design your React app as normal, using those REST / JSON endpoints, and the react router to show it correctly.
Once you have your whole react app working, you can now build a server-side rendered version of the whole thing, which you'll need to re-implement from scratch, and can either access the data through the JSON endpoints (slow) or by making the queries itself inside the pages.
The Python side
Which Python Framework to use on the backend?
I'd recommend Django to start with. It's very capable and can do ANYTHING you want it too. You probably need the Django REST framework too. There are other options available, but this will be the quickest way to get something secure and sane running.
Getting Django to work nicely with your webpack / react workflow is not too complex. There are projects like Django-Webpack-Loader and various tutorials about it online showing how to use it.
Good luck.

What Are The Advantages of Building a Web-Page with Django as Opposed to Solely Using HTML5/CSS3?

Essentially, my questions are as stated above in the title. What I'm really seeking to know is why it would be privy of me to build a web-page utilizing the Django framework as opposed to solely building it out with HTML5 and CSS3.
I do know that Django utilizes bootstrapping of HTML5 and CSS and this is where my questions are raised over the efficiency of using the Django framework as opposed to solely using HTML5/CSS3.
1.) What are the advantages of Django?
2.) What does utilizing the Django framework offer me that HTML5/CSS3 do not?
3.) HTML5 can also build dynamic web-pages as can Django. Why would Django be better for a dynamic web-page?
I am looking for a very valid answer here as I am about to start building my web-page. The responses I get to these questions will be the nail in the coffin for which method I will be using to build the web-page. Thanks ladies and gentleman and I hope you find this question to be worth your while in answering.
Django is a server side framework. So it has little to do with HTML.
Django will give you easier/standardized ways to handle HTTP requests, and to manipulate entries in the database, among other things.
HTML5 alone doesn't enable dynamic web-pages. You can have interactive web pages, but they will always be the same, for every user, whenever you access it.
Django is a python web application framework that allows you to send requests from your page to a server that will in turn provide a response back to your web page.
Advantages: The power of Django is the ability to quickly get both the client ( your page ) and the backend ( the server-side logic ) setup. The backend can include writing to a database, processing information, retrieving information which is subsequently a response delivered to your web page.
HTML5/CSS3 is markup languages for your web page. You can use a editors like sublime or even notepad ++ if you are building a static web page. Django, like most web app frameworks, are used because of what I've described in #1 ( and many other unlisted reasons ).
HTML5 provides the ability to make dynamic web pages ( using a client side library like JQuery as an embedded script ), Django helps you build web apps. You can write a web page using only HTML5 and JQuery to display list of tv shows that are currently on ABC by listing what is currently playing today, but what about for tomorrow? You need server-side help by creating response that will fetch all shows for tomorrow by calling the ABC API. Take a look at server-side logic and web applications.
In short, there are web pages and web applications. Sounds like to me you are building the former, so Django might be overkill.
1.) What are the advantages of Django?
Server-side scripting without the necessity to use PHP. If you already worked with Python, you don't need to learn another language for you server-side.
2.) What does utilizing the Django framework offer me that HTML5/CSS3 do not?
Hm, deployment to a server, handling user requests and dynamically generated webpages. You mentioned making an intricate website in a comment. I don't know what you mean by that, but a framework will let you do this way faster then without. Especially, if you only rely on client-side JS with static HTML5 and CSS3, I'm fairly certain you will have a hard time achieving your goal.
3.) HTML5 can also build dynamic web-pages as can Django. Why would Django be better for a dynamic web-page?
I'm not really sure you understand what dynamic means. Dynamic means generated from code, as opposed to static, which means served directly from an .html file. Django let's you do both, it's a framework and offers lots of flexibility.
If you want to serve same dish for all visitors to your site, HTML is fine. But if you want to server different dish to different user then you'll need ingredients and a way to churn them. Ingredients can be users, their profile and preferences, location, and other entities users are dealing with. Django is one way to churn all of these together and present (in HTML for example) to users.

Django REST browser interface

I'm writing a set of REST services for a Django project. I've been using django-rest-framework for a while. Because of its limited functionality I had to switch to django-piston which I quite enjoy.
However, django-rest-framework had one really nice feature - it was able to display an admin-like interface for testing the created services from the browser. It's just terrific for debugging purposes. It's very simple: one form is displayed for each HTTP method like "GET", "POST", etc. Along with that a drop-down list of available content types and a text field for putting in the data to be sent.
As I view it, this isn't really a feature in any way directly connected with a particular REST framework. It isn't even necessarily about Django. It could all be achieved just using HTML + JS, or an external website.
My question is: What do you use for manual testing / debugging web services? Could you point me to some HTML snippet or a Django app that would do the described thing?
This may seem obvious, but:
Why not just use Django's testing client (django.test.client.Client)? then instead of manually 'debugging' in your browser, you can write unit tests with expectations and get leverage out of those further down the track.
e.g.
from django.test.client import Client
client = Client()
resp = client.put('/employee/2/', data={'email': 'here#there.com'}, follow=True)
#... etc
As the author of django-rest-framework it'd be great to pick your brains about which bits of functionality could do with fleshing out. :) (obv i've got some thoughts of my own and areas I'm planning to work on, but be really good to get some user perspective)
Your absolutely right about the API browser not being limited to any particular framework. To me that's the big deal with DRF and I'd love to see more API frameworks take a similar approach. One of the supposed benefits of RESTful APIs is that they should be self-describing, and it seems counter-intuitive to me that so many of the Web APIs we build today are not Web browseable.
Oh, and totally agree with jsw re. testing Web APIs in django, I wouldn't use the framework's browsable API to replace automated tests.
I had the same problem and that was eaily solved by logging out of admin page in that project.

customizing Django look and feel in Python

I am learning Django and got it to work with wsgi. I'm following the tutorial here:
http://docs.djangoproject.com/en/1.1/intro/tutorial01/
My question is: how can I customize the look and feel of Django? Is there a repository of templates that "look good", kind of like there are for Wordpress, that I can start from? I find the tutorial counterintuitive in that it goes immediately toward customizing the admin page of Django, rather than the main pages visible to users of the site. Is there an example of a "typical" Django site, with a decent template, that I can look at and built on/modify? The polls application is again not very representative since it's so specialized.
any references on this would be greatly appreciated. thanks.
Search for generic CSS/HTML templates, and add in the Django template language where you need it. Because unless you are trying to skin a particular app (such as the admin system), there is nothing Django-specific about any of your HTML.
The fact that you're thinking in terms of Wordpress templates, and that you think the tutorial's poll application is highly specialised, are hints that you haven't really grasped what Django is. It isn't a content management system or a blog engine, although it can be used to build those things.
There's no such thing as a typical Django site, and it simply doesn't make sense to have pre-packaged templates, because the front end could be absolutely anything at all - like a poll.
You write the template like you would write any standalone HTML+CSS page, perhaps with placeholders for the content, then turn those placeholders into actual Django template tags. If you know how to do write HTML, then you know how to make a Django template.
Actually Django does not have a "look and feel". You are probably referring to the built in Django Admin application. That app comes with its own templates.
There are third party applications that can change the Admin interface, Django Grapelli is a great example.
For any other application you want to build yourself, or download. Most likely you'll have to do the templates yourself. In order to come up with something pretty you need to learn about CSS/HTML/JS and design principles as the Django Templates will quite likely be out of your way.
I always recommend HTML Dog for learning the basics of HTML, CSS and JS.

Categories