Login

Snippets by epicserve

Snippet List

HTML 5 Firefox 2 Hack

This is a hack to get HTML 5 to work for Firefox 2. It Sends the xhtml content-type to all Gecko based browsers where version is less than 1.9, or "rv:1.9pre" or "rv:1.9a" or "rv:1.9bx" where x is less than 5. I created this hack based on the information I found on this site, http://html5doctor.com/how-to-get-html5-working-in-ie-and-firefox-2/. Just put this code in one of your middleware files (i.e. mysite/middleware.py) and then add it in your MIDDLEWARE_CLASSES in your settings.py. Example: MIDDLEWARE_CLASSES = ( ... 'mysite.middleware.HTML5Firefox2Hack', ... )

  • middleware
  • html5
Read More

Dynamically change a form select widget to a hidden widget

This is an example of how you change a ChoiceField select widget into a hidden field if the right GET variable is passed. In this example code it would change the select widget into something like the following if something like "?d=3" was passed. `<p><label for="id_designation">Designation</label>Designation Option<input type="hidden" name="designation" value="3" id="id_designation" /></p>`

  • dynamic
  • forms
  • select
  • hidden
  • widget
Read More

Hide Emails

Example Usage in the template: <p>{{ email|hide_email }}<br /> {{ email|hide_email:"Contact Me" }}<br /> {% hide_email "[email protected]" %}<br /> {% hide_email "[email protected]" "John Smith" %}</p> {{ text_block|hide_all_emails|safe }} All hidden emails are rendered as a hyperlink that is protected by javascript and an email and name that are encoded randomly using a hex digit or a decimal digit for each character. Example of how a protected email is rendered: <noscript>(Javascript must be enabled to see this e-mail address)</noscript> <script type="text/javascript">// <![CDATA[ document.write('<a href="mai'+'lto:&#106;&#x6f;&#x68;&#x6e;&#x40;&#101;&#120;&#97;&#109;&#x70;&#108;&#x65;&#46;&#x63;&#111;&#109;">&#74;&#111;&#104;&#110;&#x20;&#83;&#x6d;&#x69;&#x74;&#104;</a>') // ]]></script>

  • email
  • hide
  • protect
Read More

Add URL Segments to Templates

Add this code to you your context_processors.py in your project and then install it in your settings.py TEMPLATE_CONTEXT_PROCESSORS. In your template you can print out a segment of a url by using {{ segment_1 }}. For example if you're on the page "/mysite/section1/section2/" and you used {{ segment_2 }} it would print section1. This idea was taken from Expression Engines URL Segments, http://expressionengine.com/docs/templates/globals/url_segments.html. This comes in handy if you only want to do something in your template if the page your on has a particular segment. FYI, I haven't used this in a production setting yet so it could be buggy still.

  • template
  • url
  • path
  • segment
Read More

List all Form Errors

You can place this code above your form and it will list out all errors in your form if there are errors. Sometimes I like listing the errors at the top of the form because I think it looks cleaner. Make sure you add error_messages={'required': 'My detailed error here'} in your view.py for your form.

  • error
  • form
Read More

epicserve has posted 5 snippets.