Install the package

pip install buttercms-python

The source code is available on Github.

Set your API token:

from butter_cms import ButterCMS
client = ButterCMS('your_api_token')

Run this:

print client.posts.all({'page_size': 10, 'page': 1})

This API request fetches your blog posts. Your account comes with one example post which you'll see in the response.

Next, run:

print client.content_fields.get(['homepage_headline'])

This API request fetches homepage headline content. You can setup your own custom content fields to manage any content kind of content you need.

ButterCMS lets you manage content using our dashboard and integrate it into your front-end of choice with our API. You can use ButterCMS for new projects as well as add it to existing codebases.

ButterCMS comes with a blogging interface and APIs for blog posts, categories, authors, and XML feeds. For other use cases you can setup custom content fields based on your needs.

For example, if you wanted to enable a non-technical person to edit some copy on your homepage, you might create two custom content fields called "Homepage Headline" and "Homepage Main Paragraph". The non-technical person would be able to edit the values of the fields and the JSON API output would look something like this:

{
  "homepage_headline": "OMG I can edit this!",
  "homepage_paragraph": "I love ButterCMS"
}

Custom content fields can be used for more complex cases like creating dynamic pages and managing knowledgebases, which we will cover in this guide.

Table of Contents

Introduction

Learn how to quickly build a Django blog with great SEO.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Display posts

To display posts, add a blog app to your Django project. This will serve as the basis of your blog home and individual post pages. The home page will display a list of 10 most recent posts.

python manage.py startapp blog

Set up a new url route to this blog app in our project's global urls.py file (i.e. myproject/myproject/urls.py).

urlpatterns = [
    ...
    url(r'^blog/', include('blog.urls'))
    ]

Define the blog home route in blog/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='blog'),
    url(r'^page/(?P<page>\d+)$', views.home, name='archive'),
]

Note there's also an archive named route for letting users paginate through older blog posts. It points to home view and passes in page as a param.

Then set up our home view in blog/views.py and fetch blog posts from the Butter API. The response also includes some metadata we'll use for pagination.

from django.http import Http404
from django.shortcuts import render
from butter_cms import ButterCMS

client = ButterCMS('your_api_token')

def home(request, page=1):
    response = client.posts.all({'page_size': 10, 'page': page})

    try:
        recent_posts = response['data']
    except:
        # In the event we request an invalid page number, no data key will exist in response.
        raise Http404('Page not found')

    next_page = response['meta']['next_page']
    previous_page = response['meta']['previous_page']

    return render(request, 'blog_base.html', {
        'recent_posts': recent_posts,
        'next_page': next_page,
        'previous_page': previous_page
    })

Next we'll create the blog_base.html template that displays our posts and pagination links:

<h2>Posts</h2>

<!-- List of posts -->
{% for post in recent_posts %}
    <a href="{% url 'blog_post' post.slug %}">{{ post.title }}</a>
{% endfor %}

<!-- Pagination links -->
<div>
  {% if previous_page %}
  <a href="{% url "archive" previous_page %}">Prev</a>
  {% endif %}

  {% if next_page %}
  <a href="{% url "archive" next_page %}">Next</a>
  {% endif %}
</div>

We'll also create an additional route + view for displaying individual posts:

# in blog/urls.py

urlpatterns = [
    url(r'^$', views.home, name='blog'),
    url(r'^page/(?P<page>\d+)$', views.home, name='archive'),

    url(r'^(?P<slug>.*)$', views.post, name='blog_post'),
]
# in blog/views.py

def post(request, slug):
    try:
        response = client.posts.get(slug)
    except:
        raise Http404('Post not found')

    post = response['data']
    return render(request, 'blog_post.html', {
        'post': post
    })

The view for displaying a full post includes information such as author, publish date, and categories. See a full list of available post properties in our API reference.

<title>{{ post.seo_title }}</title>
<meta name="description" content="{{ post.meta_description }}">

<!-- Post title -->
<h2>{{ post.title }}</h2>

<!-- Post author + Publish date -->
Posted by <a href="{% url 'blog_author' author.slug %}">{{ post.author.first_name }} {{ post.author.last_name }}</a> on {{ post.published }}

<!-- Post categories -->
{% for category in post.categories %}
<a href="{% url 'blog_category' category.slug %}">{{ category.name }}</a>
{% endfor %}

<!-- Post body -->
{{ post.body|safe }}

Categories, Tags, and Authors

Use Butter's APIs for categories, tags, and authors to feature and filter content on your blog:

# In blog/views.py

client = ButterCMS('your_api_token')

def home(request, page=1):
    ...
    # Query categories and tags to display in sidebar
    categories = client.categories.all()
    tags = client.tags.all()
    ...

    return render(request, 'blog_base.html', {...})

def author(request, slug):
    response = client.posts.all({'author_slug': slug})
    recent_posts = response['data']

    return render(request, 'author.html', {
        'recent_posts': recent_posts
    })

def category(request, slug):
    response = client.posts.all({'category_slug': slug})
    recent_posts = response['data']

    return render(request, 'category.html', {
        'recent_posts': recent_posts
    })

See our API reference for more information about these objects:

RSS, Atom, and Sitemap

Butter generates RSS, Atom, and sitemap XML markup. To use these on your blog, return the generated XML from the Butter API with the proper content type headers.

# in blog/views.py
...
def rss(request):
    response = client.feeds.get('rss')
    return HttpResponse(response['data'], content_type='application/rss+xml')


def atom(request):
    response = client.feeds.get('atom')
    return HttpResponse(response['data'], content_type='application/rss+xml')

Comments

Butter doesn't provide an API for comments due to the excellent existing options that integrate easily. Two popular servies we recommend are:

Both products are free, include moderation capabilities, and give your audience a familiar commenting experience. They can also provide some additional distribution for your content since users in their networks can see when people comment on your posts. For a minimalist alternative to Disqus, check out RemarkBox or for an open-source option, Isso.

CSS

Butter integrates into your front-end so you have complete control over the design of your blog. Use the following CSS as boilerplate for post content styling.

.post-container {
  h1, h2, h3, h4, h5 {
    font-weight: 600;
    margin-bottom: 1em;
    margin-top: 1.5em;
  }

  ul, ol {
    margin-bottom: 1.25em;

    li {
      margin-bottom: 0.25em;
    }
  }

  p {
    font-family: Georgia, Cambria, "Times New Roman", Times, serif;
    font-size: 1.25em;
    line-height: 1.58;
    margin-bottom: 1.25em;
    font-weight: 400;
    letter-spacing: -.003em;
  }

  /* Responsive default image width */
  img {
    max-width: 100%;
    height: auto;
  }

  /* Responsive floating */
  @media only screen and (min-width: 720px)  {
    .butter-float-left {
      float: left;
      margin: 0px 10px 10px 0px;
    }

    .butter-float-right {
      float: right;
      margin: 0px 0px 10px 10px;
    }
  }

  /* Image caption */
  figcaption {
    font-style: italic;
    text-align: center;
    color: #ccc;
  }

  p code {
    padding: 2px 4px;
    font-size: 90%;
    color: #c7254e;
    background-color: #f9f2f4;
    border-radius: 4px;
    font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
  }

  pre {
    display: block;
    padding: 1em;
    margin: 0 0 2em;
    font-size: 1em;
    line-height: 1.4;
    word-break: break-all;
    word-wrap: break-word;
    color: #333333;
    background-color: #f5f5f5;
    font-family: Menlo, Monaco,Consolas, "Courier New", monospace;
  }
}

Migration

To import content from another platform like WordPress or Medium, send us an email.

Table of Contents

Introduction

Learn how to make content on a static page editable using Butter.

Making your content dynamic with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Setup content fields

Let's suppose we want to add a CMS to a static FAQ page with a title and a list of questions with answers. Here's the initial view and template code for a static FAQ page:

faq/views.py:

from butter_cms import ButterCMS
client = ButterCMS('your_api_token')

def faq(request):
    return render(request, 'main.html', {})

faq/templates/main.html:

<h1>FAQ</h1>

<ul>
  <li>
    <h4>When was this company started?</h4>
    <p>2014</p>
  </li>
  <li>
    <h4>What forms of payment do you accept?</h4>
    <p>Credit cards and checks.</p>
  </li>
</ul>

Making your content dynamic with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

To setup custom content fields, first sign in to the Butter dashboard.

Create a new workspace or click on an existing one. Workspaces let you organize content fields in a friendly way for content editors and have no effect on development or the API. For example, a real-estate website might have a workspace called "Properties" and another called "About Page".

Once you're in a workspace click the button to create a new content field. Choose the "Object" type and name the field "FAQ Headline":

After saving, add another field but this time choose the "Collection" type and name the field FAQ Items:

On the next screen setup two properties for items in the collection:

Now go back to your workspace and update your heading and FAQ items.

Integrate your app

To display this dynamic content in our app, we fetch the fields with an API call and then reference them in our view. Here's what the code looks like:

faq/views.py:

from butter_cms import ButterCMS
client = ButterCMS('your_api_token')

def faq(request):
    response =  client.content_fields.get(['faq_heading', 'faq_items'])
    content = response['data']

    return render(request, 'main.html', {'content': content})

faq/templates/main.html:

<h1>{{ content.faq_heading %}</h1>
<ul>
    {% for item in content.faq_items %}
    <li>
      <h4>{{ item.question %}</h4>
      <p>{{ item.answer %}</p>
    </li>
    {% endfor %}
</ul>

That's it! The values entered in the Butter dashboard will immediately update the content in our app.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Table of Contents

Introduction

Learn how to create dynamic pages using Butter.

Creating dynamic pages with Butter is a two-step process:

1) Setup custom content fields in Butter
2) Integrate the fields into your application

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Setup content fields

Let's suppose we want to be able to create pages for retail-chain locations using ButterCMS and we're starting out with a basic static page template for a single location:

locations/views.py:

from butter_cms import ButterCMS
client = ButterCMS('your_api_token')

def location(request):
    return render(request, 'location.html', {})

locations/templates/location.html:

<h1>Widget Store - Chicago</h1>
<img width="100%" src="/images/chicago.jpg">
<p>Our Chicago location is located at 566 Wacker Drive.</p>

Enabling new pages to be created with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

To setup custom content fields, first sign in to the Butter dashboard.

Create a new workspace or click on an existing one. Workspaces let you organize content fields in a friendly way for content editors and have no effect on development or the API. For example, a real-estate website might have a workspace called "Properties" and another called "About Page".

Once you're in a workspace click the button to create a new content field. Choose the "Collection" type and name the field "Location Pages":

On the next screen, we'll setup the properties for each page in the collection.

Now go back to the work space and create a location:

Integrating your app

To create these pages in our app, we create a dynamic route that fetches content for the page by using a URL parameter. Here's what the code looks like:

locations/views.py:

from butter_cms import ButterCMS
client = ButterCMS('your_api_token')

def location(request, slug):
    response =  client.content_fields.get(['location_pages[slug=%s]' % slug])
    result = response['data']

    # location_pages is a collection (array) so get the only item in the array
    location = result['location_pages'][0]

    return render(request, 'location.html', {'location': location})

locations/templates/location.html:

<h1>Widget Store - {{ location.name %}</h1>
<img width="100%" src="{{ location.feature_image %}">
<p>{{ location.description %}</p>

That's it! If you browse to /locations/chicago, you'll see the content we just entered into Butter.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Get started now

Sign up with Google Sign up with Github
or