Code

Changes between Version 5 and Version 6 of UsingMarkup


Ignore:
Timestamp:
09/30/06 12:18:11 (8 years ago)
Author:
HolgerSchurig
Comment:

removed pre-magic code

Legend:

Unmodified
Added
Removed
Modified
  • UsingMarkup

    v5 v6  
    11= CookBook - Using Markup Filters in Models = 
    22 
    3 The markup application (django.contrib.markup) is pretty handy for using filters like Textile or Markdown in your templates, but has a slight downside: every time a page is generated, you have to run the markup filter over the text, which may or may not be an acceptable performance hit. You could, if you wanted, just define a _pre_save hook which applies the filter to incoming text so it gets saved to your DB as HTML, but then you're in a pickle if you want to edit it again later. What to do, then? 
     3The markup application (django.contrib.markup) is pretty handy for using filters like Textile or Markdown in your templates, but has a slight downside: every time a page is generated, you have to run the markup filter over the text, which may or may not be an acceptable performance hit. You could, if you wanted, just define a save() hook which applies the filter to incoming text so it gets saved to your DB as HTML, but then you're in a pickle if you want to edit it again later. What to do, then? 
    44 
    55One solution is found in the Textpattern CMS, which incorporates Textile. Textpattern accepts input in Textile syntax, and then saves two copies: one is the raw input, which is stored for later use when an item is being edited, and the other is the XHTML Textile produces for that input, which is used for actual page display. The idea here is that database columns are cheap, but processing time might not be. And this is easy to do in Django; for example, let's say you have a weblog where you'd like to use Markdown on the entries. You might define your Entry model like so: 
     
    2121        ) 
    2222 
    23     def _pre_save(self): 
    24         import markdown 
    25         self.body_markdown = markdown.markdown(self.body) 
    26 }}} 
    27  
    28 Now you've got the same effect: when creating or editing entries in the admin, you'll only see the field which accepts Markdown syntax, but when saving, the body_markdown field will be auto-filled with the corresponding XHTML. Then in your template, you can pull in entry.body_markdown, and save yourself a little processing time. 
    29  
    30  
    31 ---- 
    32  
    33  
    34 '''Update:''' 
    35  
    36 Due to magic-removal, the ''_pre_save()'' function is not required, instead use : 
    37  
    38 {{{ 
    39 #!python 
    40  
    4123    def save(self): 
    4224        import markdown 
     
    4527}}} 
    4628 
    47 More information on http://code.djangoproject.com/wiki/RemovingTheMagic#Overridingsaveanddeletemodelmethods 
     29Now you've got the same effect: when creating or editing entries in the admin, you'll only see the field which accepts Markdown syntax, but when saving, the body_markdown field will be auto-filled with the corresponding XHTML. Then in your template, you can pull in entry.body_markdown, and save yourself a little processing time.