Brilliant, thanks for that Karen, works great :)

On Nov 29, 2007 4:18 PM, Karen Tracey <[EMAIL PROTECTED]> wrote:
> You need to turn off Django's HTML autoescaping when you want to render raw
> HTML.  See:
>
> http://www.djangoproject.com/documentation/templates/#automatic-html-escaping
>
> Karen
>
>
>
> On 11/29/07, Tane Piper <[EMAIL PROTECTED]> wrote:
> >
> > I was actually just about to reply to say I had found this problem :)
> > Silly me.  However, I now have an issue (which is probably out of
> > scope here, but I'll ask here just incase anyone can help).
> >
> > Now I get the formatted code back, however when I render it in my
> > template, rather than actually coming out as markup it comes out as
> > marked-up text, like so:
> >
> > <table class="sourcetable"><tr><td class="linenos"><pre> 1 2 3 4 5 6 7
> > 8 9 10 11 12</pre></td><td class="code"><div class="source"><pre><span
> > class="k">from</span> <span
> > class="nn">django.conf.urls.defaults </span> <span
> > class="k">import</span> <span class="o">*</span> <span
> > class="k">from</span> <span
> > class="nn">pastemonkey2.pastes.models </span> <span
> > class="k">import</span> <span class="n">Paste</span><span
> > class="p">,</span> <span class="n">Language</span> <span
> > class="k">from</span> <span
> > class="nn">pastemonkey2.pastes.views</span> <span
> > class="k">import</span> <span class="o">*</span> <span
> > class="n">info_dict</span> <span class="o">=</span> <span
> > class="p">{</span> <span class="s">&#39;queryset&#39;</span><span
> > class="p">:</span> <span class="n">Paste</span><span
> > class="o">.</span><span class="n">objects</span><span
> > class="o">.</span><span class="n">all</span><span class="p">(),</span>
> > <span class="p">}</span> <span class="n">urlpatterns</span> <span
> > class="o">=</span> <span class="n">patterns</span><span
> > class="p">(</span><span class="s">&#39;&#39;</span><span
> > class="p">,</span> <span class="p">(</span><span
> > class="s">r&#39;^$&#39;</span><span class="p">,</span> <span
> >
> class="s">&#39;django.views.generic.list_detail.object_list&#39;</span><span
> > class="p">,</span> <span class="n">info_dict</span><span
> > class="p">),</span> <span class="p">(</span><span
> > class="s">r&#39;^(?P&lt;object_id&gt;\d+)/$&#39;</span><span
> > class="p">,</span> <span
> > class="s">&#39;pastemonkey2.pastes.views.show_paste&#39;</span><span
> > class="p">),</span> <span class="p">)</span> </pre></div>
> > </td></tr></table>
> >
> > I'm not sure if this is something with Django, as the formatting seems
> > to be fine, it just doesn't seem to render correctly :/
> >
> > On Nov 29, 2007 3:47 PM, Karen Tracey <[EMAIL PROTECTED]> wrote:
> > >
> > > On 11/29/07, Tane Piper < [EMAIL PROTECTED]> wrote:
> > >
> > >
> > > >
> > > > Hi Folks,
> > > >
> > > > I have only been using Python since the weekend, and Django since
> > > > Tuesday so I'm quite new to the API, and I've hit a snag with what I'm
> > > > trying to do.  I'm currently working on porting over my CakePHP
> > > > application which used the GeSHi library for syntax highlighting and I
> > > > came across Pygments as the Python alternative.  What I am trying to
> > > > do is take a paste from the database which is stored as plain text and
> > > > display it highlighted.
> > > >
> > > > So far, here is my view:
> > > >
> > > > from django.shortcuts import render_to_response, get_object_or_404
> > > > from django.http import HttpResponseRedirect
> > > > from django.core.urlresolvers import reverse
> > > > from pastemonkey2.pastes.models import Paste, Language
> > > >
> > > > from pygments import highlight
> > > > from pygments.lexers import get_lexer_by_name
> > > > from pygments.formatters import HtmlFormatter
> > > >
> > > > def highlight(source, language):
> > > >     lexer = get_lexer_by_name(language, stripall=True)
> > > >     formatter = HtmlFormatter(linenos=True, cssclass="source")
> > > >     result = (source, lexer, formatter)
> > > >     return result
> > > >
> > > > def show_paste(request, object_id):
> > > >     p = get_object_or_404(Paste, pk=object_id)
> > > >     p.formatted = highlight(str(p.paste), str(p.language ))
> > > >     return render_to_response('pastes/paste_detail.html', {'object':
> p})
> > >
> > > You are first importing the pygments highlight function, then defining
> your
> > > own, then you never actually call the pygments highlight.  I believe
> what
> > > you want is something more like:
> > >
> > > def format(source, language):
> > >
> > >     lexer = get_lexer_by_name(language, stripall=True)
> > >     formatter = HtmlFormatter(linenos=True, cssclass="source")
> > >     result = highlight(source, lexer, formatter)
> > >
> > >     return result
> > >
> > > def show_paste(request, object_id):
> > >     p = get_object_or_404(Paste, pk=object_id)
> > >     p.formatted = format(str(p.paste), str(p.language))
> > >
> > >     return render_to_response('pastes/paste_detail.html', {'object': p})
> > >
> > > That is, call your function something other than highlight, and within
> it
> > > return the result of calling the pygments highlight function.
> > >
> > > Karen
> > >
> > >
> > > > What I am trying to do in show_paste is when the paste is received
> > > > from the database, I pass the paste and language from the model into
> > > > the highlight function, which should generate the marked up code and
> > > > pass back, creating a new p.formatted object, which I display in my
> > > > template.  However, what I get back is this:
> > > >
> > > > ("from django.conf.urls.defaults import *\r\nfrom
> > > > pastemonkey2.pastes.models import Paste, Language\r\nfrom
> > > > pastemonkey2.pastes.views import *\r\n\r\ninfo_dict = {\r\n
> > > > 'queryset': Paste.objects.all(),\r\n}\r\n\r\nurlpatterns =
> > > > patterns('',\r\n (r'^$',
> > > > 'django.views.generic.list_detail.object_list', info_dict),\r\n
> > > > (r'^(?P<object_id>\\d+)/$',
> > > > 'pastemonkey2.pastes.views.show_paste'),\r\n)",
> > > > <pygments.lexers.PythonLexer with {'stripall': True}>,
> > > > < pygments.formatters.html.HtmlFormatter object at 0x88ca1ec>)
> > > >
> > > > Instead of nice marked up code.  I'm wondering if anyone can suggest
> > > > where I am going wrong?
> > > >
> > > > Thanks
> > > >
> > > >
> > > > Tane Piper
> > > > Blog - http://digitalspaghetti.me.uk
> > > > AJAX Pastebin - http://pastemonkey.org
> > > >
> > > > This email is: [ ] blogable [ x ] ask first [ ] private
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > >  >
> > >
> >
> >
> >
> > --
> > Tane Piper
> > Blog - http://digitalspaghetti.me.uk
> > AJAX Pastebin - http://pastemonkey.org
> >
> > This email is: [ ] blogable [ x ] ask first [ ] private
> > > >
> >
>



-- 
Tane Piper
Blog - http://digitalspaghetti.me.uk
AJAX Pastebin - http://pastemonkey.org

This email is: [ ] blogable [ x ] ask first [ ] private

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to