How to Use Markdown in Django?

Published On: 03/01/2023 | Category: Django


Hi Dev,

This simple article demonstrates of how to use markdown in django. In this article, we will implement a how to convert markdown to html in django. Here you will learn django markdown example. you will learn django markdown editor example. Follow bellow tutorial step of how to use markdown in python django.

For web authors, Markdown is a well-liked text-to-HTML conversion tool. Compared to standard HTML, it is much simpler to use. Markdown support is incorporated into many/most static site generators, but adding it to a Django website—such as a blog—requires an additional step or two. I'll show you how to rapidly add Markdown capabilities to any Django website in this article.

Here i will give you we will help you to give example of django search example. So let's see the bellow example:

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject myproject
Step 2: Create a App
cd myproject
django-admin startapp base

Step 3: Installing required library

In this section first of all we need to install You can install the library using the pip installer as library through below following command:

pipenv install markdown==3.2.1

Step 4: Update settings.py

Next, you need to add it in the settings.py file as follows:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'base', #new
] 
Step 5: Create a Model

Now go for the models we will We'll call our single model Post and it will have just two fields: title and body. And finally set __str__ to display the name of the post in admin interface.

base/models.py
from django.db import models

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()

    def __str__(self):
        return self.title

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python manage.py makemigrations
python manage.py migrate

Step 6: Creating Template Filters

In this step we need to create a custom a markdown template tag in our base app , so now create the tag.

base/templatetags/markdown_extras.py
from django import template
from django.template.defaultfilters import stringfilter

import markdown as md

register = template.Library()


@register.filter()
@stringfilter
def markdown(value):
    return md.markdown(value, extensions=['markdown.extensions.fenced_code'])

Step 7: Update admin.py file

In this step, we need to configure admin file. open the base/admin.py file and add:

base/views.py
from django.contrib import admin
from .models import Post

# Register your models here.
admin.site.register(Post)
Output

So, add the record in our lead model manually..



Step 8: Creating the Views

In this step, we need to configure views. open the base/views.py file and add:

base/views.py
from django.shortcuts import render
from .models import Post
from django.views.generic import ListView

# Create your views here.
class BlogListView(ListView):
    model = Post
    template_name = 'post_list.html'

Step 9: Creating the Templates

Here, in this step we need to create a new folder named core in the templates folder of core.

base/templates/post_list.html
{% load markdown_extras %}

<div class="container" style="text-align: center;margin-top: 100px;">
    <div class="row">
        {% for post in object_list %}
        <div>
          <h2>{{ post.title }}</h2>
          <p>{{ post.body | markdown | safe }}</p>
        </div>
    </div>
</div>
<hr>
{% endfor %}

Step 10: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create base/urls.py with your text editor and paste below code.

base/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('post', views.BlogListView.as_view(), name='blog_list'),
]

Next, we require to add a URL path for our myproject app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

I Hope It will help you....