[Answer]-Replacing words in a sentence from database (Python/Django)

1👍

If you’re willing to change the notation, you have two options:

  1. Use Python.

    >>> 'Starting in {Duration}, the {Noun} will be {Adjective}'.format(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    >>> 'Starting in %(Duration)s, the %(Noun)s will be %(Adjective)s' % dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    
  2. Use Django.

    >>> from django.template import Context, Template
    >>> t = Template('Starting in {{Duration}}, the {{Noun}} will be {{Adjective}}') 
    >>> c = Context(dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken'))
    >>> t.render(c)
    u'Starting in 24 hours, the Coffee Maker will be broken'
    

The rest is pulling from the database and selecting the words at random, which is the easy part.

Leave a comment