Flask doesn't seem to recognize file changes - python

A little background:
I've been working on this project for about six months now and it's been running on Flask the whole time. Everything has been fine, multiple versions of the backend have been deployed live to support an app that's been in production for months now.
The development cycle involves writing everything locally and using Flask-Script's runserver command to test everything locally on localhost:8080 before deploying to a dev server and then finally to the live server.
The Problem: The other day my local flask instance, which runs on localhost:8080 apparently stopped respecting my local files.
I tried adding a new view (with a new template) and I got a 404 error when trying to view it in my browser.
I then tried making some test changes to one of the existing pages by adding a few extra words to the title. I restarted flask and none of those changes appeared.
I then went as far as deleting the entire views.py file. After restarting flask again, much to my dismay, I could still view the pages that were there originally (i.e. before this behavior started).
Finally, I made some changes to the manage.py file, which is where I put all of the Flask-Script commands, and they weren't recognized either. It's as if flask started reading from a cached version of the filesystem that won't update (which very well might be the case but I have no idea why it started doing this or how to fix the issue).
FYI: Browser caching shouldn't be an issue b/c I have the dev tools open with caching disabled. Plus the fact that changes to manage.py aren't being noticed shouldn't have anything to do with the browser.

You've most likely used flask in the DEBUG mode in which it auto reloads templates the app whenever a file changes.
Try using
export FLASK_DEBUG=True
before running
flask run
For more information see http://flask.pocoo.org/docs/1.0/config/#DEBUG

I was having a similar issue and deleting the .pyc files solved it for me.

Related

Flask and auto-reloading in new CLI

I have a web application in flask that uses rest-plus for some CRUD operations. I'm using Swagger to test endpoints.
Recently i migrated my project to use new Flask CLI instead of Flask-Script (using Manager instance to add commands).
However there is a big change I've witnessed w.r.t. reloader...
Previously every time I made a change in code, after server was already running there was information about reloading, and traceback in case of error in terminal. I think whole application was restarted each time?
Now, after migrating to CLI there is no such thing (at least not visible in terminal) and in order to see error I have to open up my server in browser or refresh browser, and the traceback is visible both in browser and terminal afterwards also there is no information about reloading in terminal.
I'd like to know whether it's caused by this new CLI or maybe I messed something else up in the meantime.
Which behavior is the correct one and is there any way I can get back auto-reloading in terminal without having to refresh web browser each time?
Thanks in advance,
Cheers
Make sure you run your flask server in development mode. For example:
export FLASK_ENV=development
export FLASK_APP=app.py
flask run
The log should say something like:
* Environment: development
* Debugger is active!

Django: Push app from local server to production server via FTP

This is a bit embarassing, but I'm a Django noob and I couldn't find a simple solution to this:
I have written a Django app in a local VM that I now want to deploy to a "production" server. App works like a charm locally.
Now my IT colleague has set up the server with Django and that also works fine. I can open it via the Web and I get the usual "Congratulations on your first Django-powered page". I can also log into the admin interface. The project has been created.
This is a very low-key mini project and I'm not too familiar with git, so we've decided to just push files via FTP. (And I want to stick with that if at all possible.) So I uploaded the app folder into the project folder and also adjusted the project's settings.py and urls.py.
However, nothing seems to be happening on the server's end. The welcome page is the same, the app does not show up in the admin interface and the URLs won't be resolved as hoped.
Any suggestions what I should have done / done differently?
You need to restart apache or whatever is running your django project. Your changes to py files are cached when you first load your server config (settings).
Any suggestions what I should have done / done differently?
You should be using git/jenkins/deployment techniques, I know you said you've decided not to use it but you're going to be missing out on important things like being able to keep track of changes and unit testing

Download and setup a Angular/Django site from server to localhost?

Hi I am new to Django and have been googling for a whole day without any success.
Basically there is a live/working website built with AngularJS, Django and PostgreSQL (Ubuntu 14.04) and I am trying to download all the files and clone the site into my localhost (Ubuntu 14.04).
After I downloaded the folder and finished install the required packages on my localhost, I run:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
The server runs without reporting any errors.
However, some of the links are directed to a 404 page, such as this:
http://127.0.0.1:8000/city/chicago
Whereas in the live site, it would direct to the correct working page.
Can someone tell me what may have gone wrong in the process?
Thanks.
Try editing the settings.py file set the DEBUG value to True. Instead of a 404 you should get a full debug of the actual request and see what exactly is missing (I suspect missing data as #NightShadeQueen pointed).
Also you might have a look at django debug toolbar which prints the executed sql queries as well and run them against your local database to see what exactly they return.

Herkou site looks different at launch then django local server site

My issue is that when I view my site using python manage.py runserver or foreman start, I can see my site perfectly.
However, when I git push heroku master on the surface everything appears fine as no errors are given. But when I view my site with the Heroku given site link, I do not see my updated site as I see when I view my site using python manage.py runserver or foreman start.
I am building my site using 'pinax-theme-bootstrap` and my virtualenv is on my desktop directory.
Does anyone have a solution as to why this may be the case?
Here is a list of suggestions on how I would approach this issue with Heroku.
You should try heroku restart. This restarts your application and can help pick up new changes.
I would clear my browser cache as often I do not see changes on my web page if the browser has cached them.
I would check that the git repository on Heroku matches my local one in that it has all the newest changes made on my local server.

Django project using wrong (old) database settings

recently I started a small Django project that I developed on a local machine using a SQLite3 database and the integrated development server. I now copied the whole project to a server running Debian.
Everything worked well as long as I kept using the SQLite3 database. Now I wanted to switch to a local MySQL database, so I changed the settings.py file in my project's root folder, created the database and added a user. I then ran syncdb and it created the needed tables without any problems.
Now I wanted to use the app, but I keep getting errors, because Django can not find the tables - neither the 'standard tables' like django_sessions nor my own tables - even though they are there (I checked the database).
The one thing that seems awkward about the DatabaseError pages I get is the following line:
Exception Location: /usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py in execute, line 234
It seems like Django is still using the SQLite3 backend even though I set it to use the MySQL backend.
The other thing that nearly makes me freak out is this: I deleted the data.sqlite file in my app's root folder using rm. But when I use my app, the file is being recreated!
Can anyone tell me where I went wrong?
When running in production with mod_wsgi or mod_python, new code isn't incorporated until you reload/restart the webserver.
If you are using apache with mod_wsgi, you can also touch the wsgi.py file to restart the python code only.
If running on apache run "sudo /etc/init.d/apache2 reload" everytime you make a change.

Categories

Resources