-1

I am using flask for building a dynamic website thats get its info from python dictionary.

I've made a dictionary for home page articles:

def homePageArticles():
    homepage_articles = [
        {
            'id': 1,
            'article_classes': 'img-R-art centerCloud-art BGcolored',
            'headline': 'Title 1',
            'art_text':'Hi there',
            'img_url': 'photo-1.png',
            'art_button_text': 'Go',
            'art_button_link': 'https://google.com',
            'art_button_classes': 'artPrimLink-curved artPrimLink-burder',
            'srtucture': """
            <h2 class="h1">  ['headline']  </h2>
            <p> ['art_text']</p>
            <a href="['art_button_link'] " class=" ['art_button_classes'] "> ['art_button_text'] </a>
            """
        },
    ]
    return homepage_articles

The whole thing is that I'm trying to call an item from dictionary (e.g: 'headline': 'Title 1') inside another item in same dictionary (e.g: 'srtucture': '<h2 class="h1"> ['headline'] </h2>' to get this result after print:

            'id': 1,
            'article_classes': 'img-R-art centerCloud-art BGcolored',
            'headline': 'Title 1',
            'art_text':'Hi there',
            'img_url': 'photo-1.png',
            'art_button_text': 'Go',
            'art_button_link': 'https://google.com',
            'art_button_classes': 'artPrimLink-curved artPrimLink-burder',
            'srtucture': """
            <h2 class="h1">  Title 1  </h2>
            <p> Hi there </p>
            <a href="https://google.com " class=" artPrimLink-curved artPrimLink-burder "> Go </a>

Ok, I've tried using : <h2 class="h1">{{ article['headline"] }}</h2> and call it in template with escaping function inside for loop; and it's appear as h2 but its value is: {{ article['headline"] }}. not Title 1 or Title 2.

So I am searching for calling dictionary value inside same dictionary.

If any one has another idea to make what i need to please but it here. thank you

Note: I want to use python dictionary, not sql or any another databases.

  • Please provide a [mcve]. This should be something that gives enough context that we can understand what you are adoing, but also avoids unnecessary details. For example, you show this `homePageArticles()` which returns a list of dictionaries. I think your question is about what happens when you use one of these dictionaries. So you don't need to give the entire list, but you do need to show the code that uses the dictionary. – Code-Apprentice Jul 26 '22 at 19:34
  • [this may be helpful](https://stackoverflow.com/questions/38254969/reference-a-dictionary-within-itself) – Nesi Jul 26 '22 at 19:37

1 Answers1

0

I'm pretty sure you can't do something like that. The dictionary would have to be already built while you are building it. But you can use string templates on structure like this <h2 class="h1">{headline}</h2>, and then formatting it. You format a string by calling .format with named parameters corresponding to the names on the templates. You can easily do that by unpacking the dictionary: homepage_article['structure'] = homepage_article['structure'].format(**homepage_article). As you have a list of dictionaries, you have to do this on a for loop:

articles = [{
    'id': 1,
    'headline': 'Title 1',
    'structure': """
    <h2 class = "h1" > {headline} < /h2>
    """
}, {
    'id': 2,
    'headline': 'Title 2',
    'structure': """
    <h2 class = "h1" > {headline} < /h2>
    """
}]

for article in articles:
    article['structure'] = article['structure'].format(**article)

Now articles has dictionaries with the information filled.