Show HTML tables in django-rest-framework instead raw data












2















is it possible to show HTML code like tables with css style instead of json/csv/text/whatever?
I tried to send html as string but it just inserts html like raw text



Thanks in advance!










share|improve this question



























    2















    is it possible to show HTML code like tables with css style instead of json/csv/text/whatever?
    I tried to send html as string but it just inserts html like raw text



    Thanks in advance!










    share|improve this question

























      2












      2








      2








      is it possible to show HTML code like tables with css style instead of json/csv/text/whatever?
      I tried to send html as string but it just inserts html like raw text



      Thanks in advance!










      share|improve this question














      is it possible to show HTML code like tables with css style instead of json/csv/text/whatever?
      I tried to send html as string but it just inserts html like raw text



      Thanks in advance!







      python django django-rest-framework






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 23 '14 at 9:56









      Aleksander KorovinAleksander Korovin

      174213




      174213
























          3 Answers
          3






          active

          oldest

          votes


















          0














          Have you installed the markdown and django-filter packages? These were required to get our HTML browsing capability working on a recent project.



          sudo pip install markdown
          sudo pip install django-filter





          share|improve this answer
























          • I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

            – rabin utam
            Dec 13 '15 at 22:15



















          0














          This is an old question, but I just went through this and wanted to share my solution in case anyone else is going through it as well.



          What I ended up having to do is shim the rest_framework.compat.apply_markdown function to enable the tables extension. At the end of my settings.py, I added the following:



          # Monkey patch rest_framework's markdown rendering function, to enable the
          # tables extension.

          import markdown
          import rest_framework.compat

          def apply_markdown(text):
          """
          Simple wrapper around :func:`markdown.markdown` to set the base level
          of '#' style headers to <h2>.
          """
          extensions = ['markdown.extensions.toc', 'markdown.extensions.tables']
          extension_configs = {
          'markdown.extensions.toc': {
          'baselevel': '2'
          }
          }
          md = markdown.Markdown(
          extensions=extensions, extension_configs=extension_configs
          )
          return md.convert(text)

          rest_framework.compat.apply_markdown = apply_markdown


          In this case I'm using DRF 3.6.4 and markdown 2.6.9. In the original rest_framework.compat.apply_markdown function there's some code that sets different options based on the version of markdown, but I omitted that in the shim.



          Also note that the default tables extension may not give you tables styled the way you want. I ended up copying markdown/extensions/tables.py into a new module and adding class="table" to the table element. The source for that change is in a gist. For more information about the limited table syntax in markdown see this thread.






          share|improve this answer































            0














            For djangorestframework>=3.7 update the default view description function.



            https://www.django-rest-framework.org/api-guide/settings/#view_description_function



            REST_FRAMEWORK = {
            # Module path to a callable which should have a signature (self, html=False)
            'VIEW_DESCRIPTION_FUNCTION': 'app_name.view_description.get_view_description',
            }


            view_description.py



            import markdown
            from django.utils.encoding import smart_text
            from django.utils.html import escape
            from django.utils.safestring import mark_safe
            from rest_framework.compat import (
            HEADERID_EXT_PATH, LEVEL_PARAM, md_filter_add_syntax_highlight
            )
            from rest_framework.utils import formatting


            TABLE_EXTENSION_PATH = 'markdown.extensions.tables'

            def _apply_markdown(text):
            extensions = [HEADERID_EXT_PATH, TABLE_EXTENSION_PATH]
            extension_configs = {
            HEADERID_EXT_PATH: {
            LEVEL_PARAM: '2'
            }
            }
            md = markdown.Markdown(
            extensions=extensions, extension_configs=extension_configs
            )
            md_filter_add_syntax_highlight(md)
            return md.convert(text)


            def get_view_description(view_cls, html=False):
            description = view_cls.__doc__ or ''
            description = formatting.dedent(smart_text(description))

            if html:
            return mark_safe(_apply_markdown(description))
            return description





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23826165%2fshow-html-tables-in-django-rest-framework-instead-raw-data%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Have you installed the markdown and django-filter packages? These were required to get our HTML browsing capability working on a recent project.



              sudo pip install markdown
              sudo pip install django-filter





              share|improve this answer
























              • I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

                – rabin utam
                Dec 13 '15 at 22:15
















              0














              Have you installed the markdown and django-filter packages? These were required to get our HTML browsing capability working on a recent project.



              sudo pip install markdown
              sudo pip install django-filter





              share|improve this answer
























              • I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

                – rabin utam
                Dec 13 '15 at 22:15














              0












              0








              0







              Have you installed the markdown and django-filter packages? These were required to get our HTML browsing capability working on a recent project.



              sudo pip install markdown
              sudo pip install django-filter





              share|improve this answer













              Have you installed the markdown and django-filter packages? These were required to get our HTML browsing capability working on a recent project.



              sudo pip install markdown
              sudo pip install django-filter






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 23 '14 at 13:24









              FlipperPAFlipperPA

              7,17122143




              7,17122143













              • I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

                – rabin utam
                Dec 13 '15 at 22:15



















              • I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

                – rabin utam
                Dec 13 '15 at 22:15

















              I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

              – rabin utam
              Dec 13 '15 at 22:15





              I installed both markdown and django-filter. Only raw json extraction of comments show up. and show/hide, list operations, expand operation show nothing on cliicking them.

              – rabin utam
              Dec 13 '15 at 22:15













              0














              This is an old question, but I just went through this and wanted to share my solution in case anyone else is going through it as well.



              What I ended up having to do is shim the rest_framework.compat.apply_markdown function to enable the tables extension. At the end of my settings.py, I added the following:



              # Monkey patch rest_framework's markdown rendering function, to enable the
              # tables extension.

              import markdown
              import rest_framework.compat

              def apply_markdown(text):
              """
              Simple wrapper around :func:`markdown.markdown` to set the base level
              of '#' style headers to <h2>.
              """
              extensions = ['markdown.extensions.toc', 'markdown.extensions.tables']
              extension_configs = {
              'markdown.extensions.toc': {
              'baselevel': '2'
              }
              }
              md = markdown.Markdown(
              extensions=extensions, extension_configs=extension_configs
              )
              return md.convert(text)

              rest_framework.compat.apply_markdown = apply_markdown


              In this case I'm using DRF 3.6.4 and markdown 2.6.9. In the original rest_framework.compat.apply_markdown function there's some code that sets different options based on the version of markdown, but I omitted that in the shim.



              Also note that the default tables extension may not give you tables styled the way you want. I ended up copying markdown/extensions/tables.py into a new module and adding class="table" to the table element. The source for that change is in a gist. For more information about the limited table syntax in markdown see this thread.






              share|improve this answer




























                0














                This is an old question, but I just went through this and wanted to share my solution in case anyone else is going through it as well.



                What I ended up having to do is shim the rest_framework.compat.apply_markdown function to enable the tables extension. At the end of my settings.py, I added the following:



                # Monkey patch rest_framework's markdown rendering function, to enable the
                # tables extension.

                import markdown
                import rest_framework.compat

                def apply_markdown(text):
                """
                Simple wrapper around :func:`markdown.markdown` to set the base level
                of '#' style headers to <h2>.
                """
                extensions = ['markdown.extensions.toc', 'markdown.extensions.tables']
                extension_configs = {
                'markdown.extensions.toc': {
                'baselevel': '2'
                }
                }
                md = markdown.Markdown(
                extensions=extensions, extension_configs=extension_configs
                )
                return md.convert(text)

                rest_framework.compat.apply_markdown = apply_markdown


                In this case I'm using DRF 3.6.4 and markdown 2.6.9. In the original rest_framework.compat.apply_markdown function there's some code that sets different options based on the version of markdown, but I omitted that in the shim.



                Also note that the default tables extension may not give you tables styled the way you want. I ended up copying markdown/extensions/tables.py into a new module and adding class="table" to the table element. The source for that change is in a gist. For more information about the limited table syntax in markdown see this thread.






                share|improve this answer


























                  0












                  0








                  0







                  This is an old question, but I just went through this and wanted to share my solution in case anyone else is going through it as well.



                  What I ended up having to do is shim the rest_framework.compat.apply_markdown function to enable the tables extension. At the end of my settings.py, I added the following:



                  # Monkey patch rest_framework's markdown rendering function, to enable the
                  # tables extension.

                  import markdown
                  import rest_framework.compat

                  def apply_markdown(text):
                  """
                  Simple wrapper around :func:`markdown.markdown` to set the base level
                  of '#' style headers to <h2>.
                  """
                  extensions = ['markdown.extensions.toc', 'markdown.extensions.tables']
                  extension_configs = {
                  'markdown.extensions.toc': {
                  'baselevel': '2'
                  }
                  }
                  md = markdown.Markdown(
                  extensions=extensions, extension_configs=extension_configs
                  )
                  return md.convert(text)

                  rest_framework.compat.apply_markdown = apply_markdown


                  In this case I'm using DRF 3.6.4 and markdown 2.6.9. In the original rest_framework.compat.apply_markdown function there's some code that sets different options based on the version of markdown, but I omitted that in the shim.



                  Also note that the default tables extension may not give you tables styled the way you want. I ended up copying markdown/extensions/tables.py into a new module and adding class="table" to the table element. The source for that change is in a gist. For more information about the limited table syntax in markdown see this thread.






                  share|improve this answer













                  This is an old question, but I just went through this and wanted to share my solution in case anyone else is going through it as well.



                  What I ended up having to do is shim the rest_framework.compat.apply_markdown function to enable the tables extension. At the end of my settings.py, I added the following:



                  # Monkey patch rest_framework's markdown rendering function, to enable the
                  # tables extension.

                  import markdown
                  import rest_framework.compat

                  def apply_markdown(text):
                  """
                  Simple wrapper around :func:`markdown.markdown` to set the base level
                  of '#' style headers to <h2>.
                  """
                  extensions = ['markdown.extensions.toc', 'markdown.extensions.tables']
                  extension_configs = {
                  'markdown.extensions.toc': {
                  'baselevel': '2'
                  }
                  }
                  md = markdown.Markdown(
                  extensions=extensions, extension_configs=extension_configs
                  )
                  return md.convert(text)

                  rest_framework.compat.apply_markdown = apply_markdown


                  In this case I'm using DRF 3.6.4 and markdown 2.6.9. In the original rest_framework.compat.apply_markdown function there's some code that sets different options based on the version of markdown, but I omitted that in the shim.



                  Also note that the default tables extension may not give you tables styled the way you want. I ended up copying markdown/extensions/tables.py into a new module and adding class="table" to the table element. The source for that change is in a gist. For more information about the limited table syntax in markdown see this thread.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 24 '17 at 13:37









                  mjumbewumjumbewu

                  5193927




                  5193927























                      0














                      For djangorestframework>=3.7 update the default view description function.



                      https://www.django-rest-framework.org/api-guide/settings/#view_description_function



                      REST_FRAMEWORK = {
                      # Module path to a callable which should have a signature (self, html=False)
                      'VIEW_DESCRIPTION_FUNCTION': 'app_name.view_description.get_view_description',
                      }


                      view_description.py



                      import markdown
                      from django.utils.encoding import smart_text
                      from django.utils.html import escape
                      from django.utils.safestring import mark_safe
                      from rest_framework.compat import (
                      HEADERID_EXT_PATH, LEVEL_PARAM, md_filter_add_syntax_highlight
                      )
                      from rest_framework.utils import formatting


                      TABLE_EXTENSION_PATH = 'markdown.extensions.tables'

                      def _apply_markdown(text):
                      extensions = [HEADERID_EXT_PATH, TABLE_EXTENSION_PATH]
                      extension_configs = {
                      HEADERID_EXT_PATH: {
                      LEVEL_PARAM: '2'
                      }
                      }
                      md = markdown.Markdown(
                      extensions=extensions, extension_configs=extension_configs
                      )
                      md_filter_add_syntax_highlight(md)
                      return md.convert(text)


                      def get_view_description(view_cls, html=False):
                      description = view_cls.__doc__ or ''
                      description = formatting.dedent(smart_text(description))

                      if html:
                      return mark_safe(_apply_markdown(description))
                      return description





                      share|improve this answer




























                        0














                        For djangorestframework>=3.7 update the default view description function.



                        https://www.django-rest-framework.org/api-guide/settings/#view_description_function



                        REST_FRAMEWORK = {
                        # Module path to a callable which should have a signature (self, html=False)
                        'VIEW_DESCRIPTION_FUNCTION': 'app_name.view_description.get_view_description',
                        }


                        view_description.py



                        import markdown
                        from django.utils.encoding import smart_text
                        from django.utils.html import escape
                        from django.utils.safestring import mark_safe
                        from rest_framework.compat import (
                        HEADERID_EXT_PATH, LEVEL_PARAM, md_filter_add_syntax_highlight
                        )
                        from rest_framework.utils import formatting


                        TABLE_EXTENSION_PATH = 'markdown.extensions.tables'

                        def _apply_markdown(text):
                        extensions = [HEADERID_EXT_PATH, TABLE_EXTENSION_PATH]
                        extension_configs = {
                        HEADERID_EXT_PATH: {
                        LEVEL_PARAM: '2'
                        }
                        }
                        md = markdown.Markdown(
                        extensions=extensions, extension_configs=extension_configs
                        )
                        md_filter_add_syntax_highlight(md)
                        return md.convert(text)


                        def get_view_description(view_cls, html=False):
                        description = view_cls.__doc__ or ''
                        description = formatting.dedent(smart_text(description))

                        if html:
                        return mark_safe(_apply_markdown(description))
                        return description





                        share|improve this answer


























                          0












                          0








                          0







                          For djangorestframework>=3.7 update the default view description function.



                          https://www.django-rest-framework.org/api-guide/settings/#view_description_function



                          REST_FRAMEWORK = {
                          # Module path to a callable which should have a signature (self, html=False)
                          'VIEW_DESCRIPTION_FUNCTION': 'app_name.view_description.get_view_description',
                          }


                          view_description.py



                          import markdown
                          from django.utils.encoding import smart_text
                          from django.utils.html import escape
                          from django.utils.safestring import mark_safe
                          from rest_framework.compat import (
                          HEADERID_EXT_PATH, LEVEL_PARAM, md_filter_add_syntax_highlight
                          )
                          from rest_framework.utils import formatting


                          TABLE_EXTENSION_PATH = 'markdown.extensions.tables'

                          def _apply_markdown(text):
                          extensions = [HEADERID_EXT_PATH, TABLE_EXTENSION_PATH]
                          extension_configs = {
                          HEADERID_EXT_PATH: {
                          LEVEL_PARAM: '2'
                          }
                          }
                          md = markdown.Markdown(
                          extensions=extensions, extension_configs=extension_configs
                          )
                          md_filter_add_syntax_highlight(md)
                          return md.convert(text)


                          def get_view_description(view_cls, html=False):
                          description = view_cls.__doc__ or ''
                          description = formatting.dedent(smart_text(description))

                          if html:
                          return mark_safe(_apply_markdown(description))
                          return description





                          share|improve this answer













                          For djangorestframework>=3.7 update the default view description function.



                          https://www.django-rest-framework.org/api-guide/settings/#view_description_function



                          REST_FRAMEWORK = {
                          # Module path to a callable which should have a signature (self, html=False)
                          'VIEW_DESCRIPTION_FUNCTION': 'app_name.view_description.get_view_description',
                          }


                          view_description.py



                          import markdown
                          from django.utils.encoding import smart_text
                          from django.utils.html import escape
                          from django.utils.safestring import mark_safe
                          from rest_framework.compat import (
                          HEADERID_EXT_PATH, LEVEL_PARAM, md_filter_add_syntax_highlight
                          )
                          from rest_framework.utils import formatting


                          TABLE_EXTENSION_PATH = 'markdown.extensions.tables'

                          def _apply_markdown(text):
                          extensions = [HEADERID_EXT_PATH, TABLE_EXTENSION_PATH]
                          extension_configs = {
                          HEADERID_EXT_PATH: {
                          LEVEL_PARAM: '2'
                          }
                          }
                          md = markdown.Markdown(
                          extensions=extensions, extension_configs=extension_configs
                          )
                          md_filter_add_syntax_highlight(md)
                          return md.convert(text)


                          def get_view_description(view_cls, html=False):
                          description = view_cls.__doc__ or ''
                          description = formatting.dedent(smart_text(description))

                          if html:
                          return mark_safe(_apply_markdown(description))
                          return description






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 1:36









                          jackotonyejackotonye

                          1,4401221




                          1,4401221






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23826165%2fshow-html-tables-in-django-rest-framework-instead-raw-data%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga