Losing user input data due to heroku databse refresh (django backend) - python

So I made a django app and deployed it using Heroku - with s3 for file uploads and whitenoise for static files. I use the admin page to add updates to the displayed sections, like new blog entries (for example or comments).
The problem is that any database update using the admin page stays for a day at most and then refreshes, going back to the previous state and making me lose all the new data.
I have tried some stuff, but I am totally out of my depth here. I even tried pulling the changes made back into my local copy and pushing again to repository but it shows no changes. Is it something to do with how Django takes in user input? If anyone could help me that would be amazing. If you guys need any code please tell me.

Related

Django project is corrupted

I have a new project for a little online shop. I use the PyCharm code editor. It saves the progress whenever I switch to a new window and the server autorestarts. After a while of working on the project the server stopped doing that and now it needs to be done manually. When I got to making forms, I noticed that they don't show on the page. It's the same logic as in my previews projects. I made exactly the same forms and models from the old project in the current one and they do not work. Then I did the opposite: I added the models and the form from the current project into the old one and they do render on page.
What could be the problem and where shoul I start looking for it?

How to display user inputted blogs in flask production server

I'm using gunicorn and nginx to serve a flask application, my website has a blogging feature where users can write blogs, once they do, their input is saved as an html file (I use tinyMCE to generate it) and a flask view is added to a views.py file. Now, in deployment, I just used
use_reloader=True
This ensured that every time a new file was added, it was detected, now in production, I don't know how to implement it, gunicorn has --reload option but the docs say that it's only for development. Can someone please provide an insight on how to implement this feature? Thanks!
You don't need a new view for every html file. You could use a generic view that renders the appropriate html according to the url requested.

Best approach for loading settings that can be modified in Flask

Hello all fellow StackOverFlow'ers,
I'm making an app in Flask that runs depending on settings that can be changed by administrator via a POST request in their admin-panel,
Actually, the only two things I came up with for doing this is using os.environ.get (Environment variables) [which i'm using now] or insert it to a PostreSQL Database config table and load it up
Anyway I will be storing settings such as a couple of API_URLs and their API_KEY, and some Conditions of checking like a success value where if condition in text .. else is applied to ... where admin can change them via the panel
I'm looking for the best performant approach for doing such thing.
Best regards.
If you're looking for the changes to only apply on a per-user basis (changes made by the admin only affect the admin), check out Flask's sessions. It works like a dictionary, but stores information in a cookie in the user's browser that can be programmatically accessed by Flask. Be warned that this data is stored in plain-text in the user's browser, so don't store anything sensitive here.
On the other hand, if you're looking for changes made by the admin to affect everybody visiting the website, you may just be able to store the settings in a variable, update them when the admin makes changes, and read them when responding to a request. If you want these settings to persist through a server restart, however, you'll need to write them to disk and then load them on server restart and save them to disk when they're changed. If this is a production-grade app and needs to be able to scale, I personally recommend using an SQLite file to store settings (or a SQL database if it really needs to scale), but this is a personal preference of mine. If this is just a personal app, storing settings in text files would be just fine.
Hope this helps!

Django : how to delete file record after user close his page?

I'm making a simple file resizer with Django, there are 3 fields, email, size, image.
After the user arrives to a the last view where he can download what he resized, I'd like Django to erase the image of the ImageField to save space on my Django project whenever an user close his window with the final view page.
I do want to keep the email and the size as it is, so these are the only things that shouldn't be deleted.
I have no idea where to search, I looked into StackOverflows posts and in Django google groups articles but I could not find something to help me, any suggestion on how this can be done ?
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload - you can try using this method to handle browser close tab with js and send request to django.
But in your place I would prefer timeout after which all images resized certain time ago would be removed (it can be done with simple cron script, possibly with some management command).

Huge Django project

I have a new job and a huge django project (15 apps, more than 30 loc). It's pretty hard to understand it's architecture from scratch. Are there any techniques to simplify my work in the beginning? sometimes it's even hard to understand where to find a form or a view that I need... thnx in advance.
When I come to this kind of problem I open up a notebook and answer the following:
1. Infrastructure
Server configuration, OS etc
Check out the database type (mysql, postgres, nosql)
External APIS (e.g Facebook Connect)
2. Backend
Write a simple description
Write its input/output from user (try to be thorough; which fields are required and which aren't)
Write its FK and its relation to any other apps (and why)
List down each plugin the app is using. And for what purpose. For example in rails I'd write: 'gem will_paginate - To display guestbook app results on several pages'
3. Frontend
Check out the JS framework
Check the main stylesheet files (for the template)
The main html/haml (etc) files for creating a new template based page.
When you are done doing that. I think you are much more prepared and able go deeper developing/debugging the app. Good luck.
Use this http://packages.python.org/django-extensions/graph_models.html
to generate the Relationship diagrams from the models so that you can visually see how the models are related to each other. This will give you nice idea about the app
1) Try to install the site from scratch. You will find what external apps are needed for the site to run.
2) Reverse engineer. Browse through the site and try to find out what you have to do to change something to that page. Start with the url, look up in urls.py, read the view, check the model. Are there any hints to other processes?
3) Try to write down everything you don't understand, and document the answers for future reference.
I would clone the project so you can mess up endlessly.
Then I would start to reduce the code. "What happens if if just remove this function here?
Also get django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
A good terminal debugger is also golden, there are many out there, here is an example:
https://github.com/tomchristie/django-pdb
This allow you to halt the code and even inject and mutate parameters in runtime. Just like GDB in C.
If you use FireFox you can install FireBug on it and when you for example submit ajax form you can see at which url send you request after what you can easily find controller which work with this form data. At chrome this utility embedded by default and call by F12 key.

Categories