[{"data":1,"prerenderedAt":58},["Reactive",2],{"blog-post-django-markdown":3},{"slug":4,"title":5,"description":6,"excerpt":7,"date":8,"category":9,"author":10,"keywords":11,"readingMinutes":12,"html":13,"headings":14,"tools":42,"related":43},"django-markdown","Integrate Markdown in Django: A Step-by-Step Guide","Learn to integrate Markdown into your Django projects with our straightforward tutorial using the Markdown-deux library.","In this tutorial, we are going to learn how to integrate and use Markdown in Django. Very often, we need to process text in Markdown format to output HTML in our Django applications. This can be…","2024-07-22","Tutorial","Matthew Rathbone","Django markdown",2,"\u003Ch2 id=\"introduction\">Introduction\u003C/h2>\n\u003Cp>In this tutorial, we are going to learn how to integrate and use Markdown in Django. Very often, we need to process text in Markdown format to output HTML in our Django applications. This can be easily accomplished with the help of a few libraries.\u003C/p>\n\u003Ch2 id=\"prerequisites\">Prerequisites\u003C/h2>\n\u003Cp>Before starting this tutorial, make sure that you have Django installed in your development environment. If not, you can install it with pip:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-bash\">pip install django\n\u003C/code>\u003C/pre>\u003Ch2 id=\"setting-up-a-new-django-project\">Setting up a new Django project\u003C/h2>\n\u003Cp>Start by creating a new Django project. Run the following command in your Terminal:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-bash\">django-admin startproject markdown_project\n\u003C/code>\u003C/pre>\u003Cp>After creating the project, navigate into the project directory:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-bash\">\u003Cspan class=\"hljs-built_in\">cd\u003C/span> markdown_project\n\u003C/code>\u003C/pre>\u003Ch2 id=\"installing-the-required-library\">Installing the required library\u003C/h2>\n\u003Cp>To convert Markdown into HTML in Django, we will use the \u003Ccode>Markdown-deux\u003C/code> library. This library will convert our markdown into HTML that can be used in our Django templates. To install \u003Ccode>Markdown-deux\u003C/code>, run the following command in your Terminal:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-bash\">pip install markdown-deux\n\u003C/code>\u003C/pre>\u003Ch2 id=\"integrating-markdown-deux-in-django\">Integrating Markdown-deux in Django\u003C/h2>\n\u003Cp>After installing \u003Ccode>Markdown-deux\u003C/code>, we need to add it to the list of installed apps in our Django settings. Open the \u003Ccode>settings.py\u003C/code> file and add \u003Ccode>&#39;markdown_deux&#39;\u003C/code> to \u003Ccode>INSTALLED_APPS\u003C/code>:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-python\">INSTALLED_APPS = [\n    \u003Cspan class=\"hljs-comment\"># ... other apps\u003C/span>\n    \u003Cspan class=\"hljs-string\">&#x27;markdown_deux&#x27;\u003C/span>,\n]\n\u003C/code>\u003C/pre>\u003Ch2 id=\"using-markdown-in-django-views\">Using Markdown in Django views\u003C/h2>\n\u003Cp>Now, we are all set to use Markdown in our Django views. Here&#39;s a basic example:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-python\">\u003Cspan class=\"hljs-keyword\">from\u003C/span> django.shortcuts \u003Cspan class=\"hljs-keyword\">import\u003C/span> render\n\u003Cspan class=\"hljs-keyword\">from\u003C/span> markdown_deux.templatetags.markdown_deux_tags \u003Cspan class=\"hljs-keyword\">import\u003C/span> markdown_allowed\n\n\u003Cspan class=\"hljs-keyword\">def\u003C/span> \u003Cspan class=\"hljs-title function_\">post_detail\u003C/span>(\u003Cspan class=\"hljs-params\">request, \u003Cspan class=\"hljs-built_in\">id\u003C/span>\u003C/span>):\n    post = get_object_or_404(Post, \u003Cspan class=\"hljs-built_in\">id\u003C/span>=\u003Cspan class=\"hljs-built_in\">id\u003C/span>)\n    content = markdown_allowed(post.content)\n    \u003Cspan class=\"hljs-keyword\">return\u003C/span> render(request, \u003Cspan class=\"hljs-string\">&#x27;blog/post_detail.html&#x27;\u003C/span>, {\u003Cspan class=\"hljs-string\">&#x27;content&#x27;\u003C/span>: content})\n\u003C/code>\u003C/pre>\u003Cp>In the example above, we are getting a \u003Ccode>post\u003C/code> by its \u003Ccode>id\u003C/code>, converting the \u003Ccode>content\u003C/code> into HTML with the \u003Ccode>markdown_allowed\u003C/code> function, and passing the converted \u003Ccode>content\u003C/code> into our \u003Ccode>post_detail.html\u003C/code> template.\u003C/p>\n\u003Ch2 id=\"using-markdown-in-django-templates\">Using Markdown in Django templates\u003C/h2>\n\u003Cp>After converting markdown into HTML in our view, we can use it in our Django template. Open your \u003Ccode>post_detail.html\u003C/code> template and add the following:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-html\">\u003Cspan class=\"hljs-tag\">&lt;\u003Cspan class=\"hljs-name\">div\u003C/span> \u003Cspan class=\"hljs-attr\">class\u003C/span>=\u003Cspan class=\"hljs-string\">&quot;post-content&quot;\u003C/span>&gt;\u003C/span>\n    {{ content|safe }}\n\u003Cspan class=\"hljs-tag\">&lt;/\u003Cspan class=\"hljs-name\">div\u003C/span>&gt;\u003C/span>\n\u003C/code>\u003C/pre>\u003Cp>We are outputting the \u003Ccode>content\u003C/code> with Django&#39;s \u003Ccode>safe\u003C/code> filter because it&#39;s being treated as safe HTML.\u003C/p>\n\u003Ch2 id=\"expected-output\">Expected output\u003C/h2>\n\u003Cp>If you run your Django server and navigate to your post detail view, you should see your post content rendered as HTML.\u003C/p>\n\u003Ch2 id=\"conclusion\">Conclusion\u003C/h2>\n\u003Cp>That&#39;s how you can use Markdown in Django. Remember, this tutorial only scratches the surface of what you can do with Markdown in Django. For more advanced uses, I recommend checking out the \u003Ca href=\"https://github.com/trentm/django-markdown-deux\">Markdown-deux documentation\u003C/a>.\u003C/p>\n\u003Cp>For more information on the Markdown syntax, check out this \u003Ca href=\"/blog/guide-to-blockquotes-in-markdown\">guide to blockquotes in markdown\u003C/a>.\u003C/p>\n\u003Cp>\u003Cem>Django is a trademark of the Django Software Foundation.\u003C/em>\u003C/p>\n",[15,18,21,24,27,30,33,36,39],{"id":16,"text":17,"level":12},"introduction","Introduction",{"id":19,"text":20,"level":12},"prerequisites","Prerequisites",{"id":22,"text":23,"level":12},"setting-up-a-new-django-project","Setting up a new Django project",{"id":25,"text":26,"level":12},"installing-the-required-library","Installing the required library",{"id":28,"text":29,"level":12},"integrating-markdown-deux-in-django","Integrating Markdown-deux in Django",{"id":31,"text":32,"level":12},"using-markdown-in-django-views","Using Markdown in Django views",{"id":34,"text":35,"level":12},"using-markdown-in-django-templates","Using Markdown in Django templates",{"id":37,"text":38,"level":12},"expected-output","Expected output",{"id":40,"text":41,"level":12},"conclusion","Conclusion",[],[44,51],{"slug":45,"title":46,"description":47,"excerpt":48,"date":49,"category":9,"author":10,"keywords":50,"readingMinutes":12},"markdown-cross-out-text","How to Cross Out Text in Markdown - Step-by-Step Guide","Learn how to effectively cross out text in Markdown using the double tilde syntax. Practical examples and use cases included.","Utilizing text effects in Markdown can improve the readability and emphasize key points of your written content. In this tutorial, we will be focusing on how to cross out text in Markdown.…","2024-07-12","Markdown cross out text",{"slug":52,"title":53,"description":54,"excerpt":55,"date":56,"category":9,"author":10,"keywords":57,"readingMinutes":12},"ruby-on-rails-markdown-with-active-record","Integrate Markdown in Rails Apps with ActiveRecord","Learn how to add Markdown support to Ruby on Rails applications using ActiveRecord and Redcarpet for blog functionalities.","In this tutorial we're going to demonstrate how you can integrate Markdown in your Ruby on Rails application, especially when the context involves ActiveRecord models.","2024-07-01","Ruby on rails markdown with active record",1792184940727]