Search code examples
encodingcharacter-encodingflaskflask-flatpages

How to pass config settings with utf-8 characters in flask?


I created a static site generator in Flask, running Python 2.7.

There are some common settings for the whole site, declared in app.py, such as a global sitename SITENAME that shell fill in part of <title> on every page.

It works with average ASCII signs, but as soon as I enter a German Umlaut (or any non-ASCII code as such), it breaks.

from flask import Flask, render_template, url_for
from flask_flatpages import FlatPages

DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
SITENAME = "Übungstitel"

app = Flask(__name__)
app.config.from_object(__name__)
pages = FlatPages(app)

I tried to declare the encoding in a preamble:

#!/usr/bin/python
# -*- coding: utf-8 -*-

But this did not solve it either, as flask would not start at all.

How can I pass a non-ASCII title as a global setting to my flask app?


Solution

  • If you are using Python 2.x, you will need to pass it has a unicode object.

    SITENAME = u"Übungstitel"
    

    The code you have would work with Python 3.x. You'd also be able to remove the coding declaration as utf-8 is the default for Python 3.