Jump to content

Hello,

 

I am building a Django app. I would like to create a global file where I will store all text and links on my website.

 

For example:

 

homeSiteTitle = 'My Title',

 

homeButtonText = 'My Button',

 

homeButtonUrl = 'https://example.com',

 

 

Then I want to use these variables in my .html files.

 

<a href="{{homeButtonUrl"}}><button>{{homeButtonText}}</button>

 

Is there any way I can achieve this. I hope you understand me.

Link to comment
https://linustechtips.com/topic/1400476-global-file-for-strings-in-django/
Share on other sites

Link to post
Share on other sites

5 minutes ago, mich991 said:

Is there any way I can achieve this.

There are probably a few dozen.

 

This isn't really django specific, you can easily read a json file into a dict:

https://realpython.com/python-json/

and copy the relevant values into the corresponding variables.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

I believe you can use the Django settings file for this.

 

In settings.py add:

HOME_SITE_TITLE = "My Title"
HOME_BUTTON_TEXT = "My Button"
HOME_BUTTON_URL = "https://example.com"

 

You can then access it in other files like this:

from django.conf import settings

print(settings.HOME_SITE_TITLE)

 

I can understand setting a global title for the site but I don't understand why you want to store the text/link for a button in a global variable? You can also remove the need to store a global site title if you use a template to create your pages.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×