Django flatpages with markdown - a1k89/Blog GitHub Wiki

How to add django flatpages with markdown

  1. Add django.contrib.sites to INSTALLED_APPS
  2. Add django.contrib.flatpages' to INSTALLED_APPS
  3. Add django.contrib.flatpages.middleware.FlatpageFallbackMiddleware to INSTALLED_APPS
  4. Add path('pages/', include('django.contrib.flatpages.urls')) to your urls.py
  5. Install martor: django-martor
  6. Inside your any app add to admin.py:
from django.contrib import admin
from django.db import models
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin

admin.site.unregister(FlatPage)

@admin.register(FlatPage)
class CustomFlatPageAdmin(FlatPageAdmin):
    formfield_overrides = {models.TextField: {"widget": AdminMartorWidget}}
  1. Create templates folder: templates/flatpages/default.html
{% load martortags %}

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{{ flatpage.title|safe }}</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
  <div class="container">
    {{ flatpage.content|safe_markdown }}
  </div>
</body>
</html>
  1. Now you may to create simple hello-world page in admin and will see result in url path: /pages/hello-world/
⚠️ **GitHub.com Fallback** ⚠️