Encoding Unicode data for XML and HTML « Python recipes « ActiveState Code
original source : http://code.activestate.com/recipes/303668-encoding-unicode-data-for-xml-and-html/
The “xmlcharrefreplace” encoding error handler was introduced in Python 2.3. It is very useful when encoding Unicode data for HTML and other XML applications with limited (but popular) encodings like ASCII or ISO-Latin-1. The following code contains a trivial function “encode_for_xml” that illustrates the “xmlcharrefreplace” error handler, and a function “_xmlcharref_encode” which emulates “xmlcharrefreplace” for Python pre-2.3.
An HTML demonstration is included. Put the code into a file, run it with Python, and redirect the output to a .html file. Open the output file in a browser to see the results.
A variation of this code is used in the Docutils project. The original idea for backporting “xmlcharrefreplace” to pre-2.3 Python was from Felix Wiemann.
Python, 55 lines
Copy to clipboard
def encode_for_xml(unicode_data, encoding='ascii'): """ Encode unicode_data for use as XML or HTML, with characters outside of the encoding converted to XML numeric character references. """ try: return unicode_data.encode(encoding, 'xmlcharrefreplace') except ValueError: # ValueError is raised if there are unencodable chars in the # data and the 'xmlcharrefreplace' error handler is not found. # Pre-2.3 Python doesn't support the 'xmlcharrefreplace' error # handler, so we'll emulate it. return _xmlcharref_encode(unicode_data, encoding) def _xmlcharref_encode(unicode_data, encoding): """Emulate Python 2.3's 'xmlcharrefreplace' encoding error handler.""" chars = [] # Step through the unicode_data string one character at a time in # order to catch unencodable characters: for char in unicode_data: try: chars.append(char.encode(encoding, 'strict')) except UnicodeError: chars.append('&#%i;' % ord(char)) return ''.join(chars) if __name__ == '__main__': # demo data = u''' <html> <head> <title>Encoding Test</title> </head> <body> <p>accented characters:</p> <ul> <li>xe0 (a + grave) <li>xe7 (c + cedilla) <li>xe9 (e + acute) <li>xee (i + circumflex) <li>xf1 (n + tilde) <li>xfc (u + umlaut) </ul> <p>symbols:</p> <ul> <li>xa3 (British pound) <li>xa2 (cent) <li>u20ac (Euro) <li>u221e (infinity) <li>xb0 (degree) </ul> </body></html> ''' print encode_for_xml(data, 'ascii')
Unicode data must be encoded before being printed or written out to a file. UTF-8 is an ideal encoding, since it can handle any Unicode character. But for many users and applications, ASCII or ISO-Latin-1 are often preferred over UTF-8. When the Unicode data contains characters that are outside of the given encoding (for example, accented characters and most symbols are not encodable in ASCII, and the “infinity” symbol is not encodable in Latin-1), these encodings cannot handle the data on their own. Python 2.3 introduced an encoding error handler called “xmlcharrefreplace” which replaces unencodable characters with XML numeric character references, like “∞” for the infinity symbol.
Please note that “xmlcharrefreplace” cannot be used for non-XML/HTML output. TeX and other non-XML markup languages do not recognize XML numeric character references.
Once comfortable with Unicode and encodings, there is a more concise and direct interface in the “codecs” module:
Create a file-like object, outfile, that will transparentlyhandle its own text encoding:
outfile = codecs.open(‘out.html’, mode=’w’, encoding=’ascii’, errors=’xmlcharrefreplace’)
output = u’… some HTML containing non-ASCII characters …’
No need to encode the output here:
outfile.write(output) outfile.close()
Django forms ChoiceField with dynamic values… | ilian.io
original source: http://www.ilian.io/django-forms-choicefield-with-dynamic-values/
The problem: Lets say that you need a form with a drop-down list that have dynamic values. With Django this can be done simple and fast, but if you are new you may get yourself into a trap. In a standard form(with static values in the drop-down) your code will be something like this:
MY_CHOICES = ( ('1', 'Option 1'), ('2', 'Option 2'), ('3', 'Option 3'), ) class MyForm(forms.Form): my_choice_field = forms.ChoiceField(choices=MY_CHOICES)
So if you want the values to be dynamic(or dependent of some logic) you can simply modify your code to something like this:
def get_my_choices(): # you place some logic here return choices_list class MyForm(forms.Form): my_choice_field = forms.ChoiceField(choices=get_my_choices())
and here you will fail(not absolutely). This is a common mistake and sooner or later you will see what you messed but it my be too late. Speciality: the trick is that in this case my_choice_field choices are initialized on server (re)start. Or in other words once you run the server the choices are loaded(calculated) and they will not change until next (re)start. This mean that your logic will be executed only once. This will be OK if your logic is server specific(depends from server settings) or “semi-dynamic” in any other way. But you need this list to be updated on every form load you will need to use something the form __init__ method. Solution: fortunately the form`s class has an __init__ method that is called on every form load. Most of the times you skipped it in the form definition but now you will have to use it.
def get_my_choices(): # you place some logic here return choices_list class MyForm(forms.Form): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['my_choice_field'] = forms.ChoiceField( choices=get_my_choices() )
You first call the __init__ method of the parent class(Form) using the super keyword and then declare your dynamic fields(in this case my_choice_field). With this code get_my_choices is called on every form load and you will get your dynamic drop-down.
Final words: have in mind that this method also have one strong drawback. If your application have a heavy load, executing a logic on every form load may(will) bring you troubles. So some caching of the form or the logic result may be useful.
And remember If you have other approach, idea or question I am always ready to hear it.
original source : https://djangosnippets.org/snippets/3023/
{% if is_paginated %} <ul class="pagination pagination-centered"> {% if page_obj.has_previous %} <li><a href="?page=1"><<</a></li> <li><a href="?page={{ page_obj.previous_page_number }}"><</a></li> {% endif %} {% for i in paginator.page_range %} <li {% if page_obj.number == i %} class="active" {% endif %}><a href="?page={{i}}">{{i}}</a></li> {% endfor %} {% if page_obj.has_next %} <li><a href="?page={{ page_obj.next_page_number }}">></a></li> <li><a href="?page={{ page_obj.paginator.num_pages }}">>></a></li> {% endif %} </ul> {% endif %}
Programmer blog: Django: ‘ManyRelatedManager’ object is not iterable
original source : http://garmoncheg.blogspot.com/2011/10/django-manyrelatedmanager-object-is-not.html
Django: ‘ManyRelatedManager’ object is not iterableUpon development proces you often meet ManyToMany relations that throw errors in templates. I prefer to feed everything to context and play with it there.
This kind of error appears when you iterate through a model’s ManyToMany field. To fix it just add a related ‘all’ manager through dot syntax. Like so:
Model:
class BannerImage(models.Model): image = models.ImageField(upload_to="somedir")
def get_image_url(self):
return 'Image url here' class Banner(models.Model): name = models.CharField(max_length=250, null=True) images = models.ManyToManyField(BannerImage)
View/Tag or else, that creates context:
banner = get_object_or_404(Banner, pk=1) return {'banner': banner,}
Template causing the error:
{% for image in banner.images %} <img src="{{ image.get_image_url }}" /> {% endfor %}
Template without the error:
{% for image in banner.images.all %} <img src="{{ image.get_image_url }}" /> {% endfor %}
I wrote this article for myself for remembering. I always forget this little thing. But if this method works for your case fill free to drop a “thankyou” comment or +1.