How to save or access plt svg in volume using Django?

My Django app is working perfectly and I've already integrated volume in my repo, users can change their profile picture and display them through my app views.py. It is setup like this. settings.py: STATIC_URL = '/static/' MEDIA_URL = '/images/' MEDIA_ROOT = os.path.join(BASE_DIR, 'images') STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ] app url: urlpatterns += [ re_path(r'^static/images/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})] urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) volume mount path: /app/static/images When a user uploads a new profile pic in my volume through my views, I can access it through database image in my html {{user.image.url}} the path will be like this. img src="/static/images/doctors/profilepic.png" Now I have a separate python file called forecasting.py inside the doctors app and I don't know how to save the plt and its path without going through the database and accessing it through plt.url. I tried: plt.savefig(f'forecasting/Forecast{Dataset.columns[0]}.svg') and access it through: src="/static/images/doctors/forecasting/Forecast_Hepatitis%20A.svg" but it's not working even if I modified the src to another path I'm aware of.
8 Replies
Percy
Percy16mo ago
Project ID: fc5fd2a0-ec61-4a05-9b07-25bbbe640789
WearBullish
WearBullish16mo ago
fc5fd2a0-ec61-4a05-9b07-25bbbe640789
Brody
Brody16mo ago
@Vin this ones for you, when you have some time
MantisInABox
MantisInABox16mo ago
Hmmm… I’d have to look at it when I get back on my computer. Unfortunately that won’t be tonight. I’m in the middle of relocating my office. Are you able to share your repo @WearBullish?
WearBullish
WearBullish16mo ago
my repo is actually in private because I have not figure out how to not expose my django secret key, my railway postgresql keys and email backend (im currently using my extra gmail since I'm rushing to deploy my website. So if you want I can make you a collaborator
MantisInABox
MantisInABox16mo ago
To not expose your keys, use os.getenv(“<key_name>”) wherever you need to store secret info. Like your Django Secret Key, and database variables
WearBullish
WearBullish16mo ago
I'll explore it in a sec So, in my railway github repo, I will create new variables containing secret info and access it through os.getenv(“<VARIABLE_NAME>”) right? or nah?
MantisInABox
MantisInABox16mo ago
In your Django service on railway, you would add the references to your database, then, you would create an environment variable for your Django secret. Then in your code update your settings.py to use the os.getenv() function for each of the variables you created. Then deploy and it should work as it does now
Want results from more Discord servers?
Add your server