Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:02:57+00:00 2026-06-11T16:02:57+00:00

I have a function in my Django app that has a dictionary containing several

  • 0

I have a function in my Django app that has a dictionary containing several long strings. When that function is called, those strings are formatted and the dictionary returned.

For example:

def my_strings(foo, bar, baz):
    return = {
        'string1': 'a really long string! %s' % foo,
        'string2': 'another long one. %s %s' % (foo, bar),
        'string3': 'yet another! %s %s %s' % (foo, bar, baz),
    }

However, having all these long strings, stored in a Python file is ugly and it seems there should be a cleaner way to do it.

I’d toyed with putting them in a template file and doing some rendering, like so:

mytemplate.txt

{% if string1 %}
a really long string! {{ foo }}
{% endif %}

{% if string2 %}
another long one. {{ foo }} {{ bar }}
{% endif %}

{% if string3 %}
yet another! {{ foo }} {{ bar }} {{ baz }}
{% endif %}

Python

def my_strings(foo, bar, baz):
    arg_dict = {
        'foo': foo,
        'bar': bar,
        'baz': baz,
    }

    my_strings = {}
    string_names = ['string1', 'string2', 'string3']

    for s in string_names:
        arg_dict[s] = True
        my_strings[s] = render_to_string('mytemplate.txt', arg_dict).strip()
        del arg_dict[s]

    return my_strings

But that seems a little too roundabout, and most likely less performant.

Is there a preferred way in Python, or Django specifically, to handle storing and formatting long string assets?

Some extra context: the string assets are either HTML or plaintext. The dictionary is eventually iterated over and all instances of each key in yet another string are replaced with its string value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:02:59+00:00Added an answer on June 11, 2026 at 4:02 pm

    I would treat this as something similar to the way many i8n compilation code does it.

    Store the long strings in a dictionary in a separate file. Import that dictionary and then format the desired string in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the view function in django that written like a dispatcher calling other
I have a Django app with an image field (a custom ThumbnailImageField type) that
On my django app I have a report (a csv download) that can take
I have a page in my Django app that needs to do one of
I have a django app containing a model with a file upload field. the
I have a django app that loads a lot of data into a sqlite3
Other posters have previously said in this forum that when your Django app starts
This one has me scratching my head. I have an app with views that
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.