I'm working on my first real django project, and was fiddling around with the css, but the changes I made did not have any effect on the site. I tried changing things on all 3 of the css files but nothing changed. I then deleted the files (from the project I still have them), and still nothing changed. My site is getting styled, I checked all of my html to make sure there was no inline style in there. All the html files are working, when I change them the site changes. Is this some weird caching thing? I've tried rerunning the server several times and still my site is getting style, seemingly from nowhere. Anyone have any ideas?
Try opening your site from now on in incognito mode. I think it is some weird caching thing so maybe clear your cache too. When developing I just use igcognito and static updates just fine.
Django copies static files (like CSS) in a separate directory when you're running with DEBUG=False. If you update any of the static files in your project, you'll have to run the collectstatic command to update them.
From the root of your project, try:
./manage.py collectstatic
Related
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.
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
I have experience with PHP but now have to manage a python application. It is already developed and running on live server. On live server we have beta.domain.com as well. which has its own copy of same source, other than the main domain application.
Now, when we print something within template it effects but before that, following MVC, if we try to print or use sys.exit() in manage.py or later imported "settings.py" or then views.py, nothing effects these files at all. After any change in just mentioned files, Website still renders everything and display related template.
Another thing that if even we remove
return render_to_response('home.html', RequestContext(request, context))
inside the views.py for testing, nothing effects and website still gets rendered with template.
Do I need to think that if website code is already hosted then in order to have changes to reflect new one in the code in any file (.py), needs to be reinitialized by executing any project related file?
I am not sure how python code needs to be updated at all, any quick help is much appreciated.
Anytime you change your code, you need to restart Apache server.
<path to apache>/bin/apachectl restart
The python interpreter of the process has already loaded your python modules in previous web requests. And once the module is loaded, it is stored in memory. Next time when a request comes, the Python interpreter will simply use the version of the module that is already loaded in memory. So your changed code will not be picked up.
Please, please, please don't try and edit files on your live server. You're only going to get yourself into terrible difficulties. Use a local copy for development, preferably cloned via a version control system, and deploy at regular intervals rather than on every change.
To answer your question, you usually need to restart the WSGI process in order to see Python code changes. An easy way to do this is to touch the .wsgi file, at which point mod_wsgi will detect that it has changed, and reload everything. Otherwise you can simply reload Apache.
Unfortunately I don't understand your references to manage.py or sys.exit.
The solution is excatly restarting the apache server as answered by Sudipta .Each time the py files are interpretted .pvc files are created for each corresponding .py files.When the Apache server is restarted all the python source files are interprretted again and new .pvc files are generated and only these files will run till the apache restarts again.
I have javascript files in my static folder. Django finds and loads them perfectly fine, so I don't think there is anything wrong with my configuration of the static options. However, sometimes when I make a change to a .js file and save it, the Django template that uses it does NOT reflect those changes -- inspecting the javascript with the browser reveals the javascript BEFORE the last save. Restarting the server does nothing, though restarting my computer has sometimes solved the issue. I do not have any code that explicitly deals with caching. Has anyone ever experienced anything like this?
I believe your browser is caching your js
you could power refresh your browser, or clear browser cache?
on chrome control+f5 or shift + f5
i believe on firefox it is control + shift + r
Since you are editing JavaScript files and watching for the changes in the browser I assume you are actively developing your Django app and probably using Django's development runserver. There is a better solution than clearing the browser cache and refreshing. If you run a watcher utility that supports the livereload protocol then your browser will automatically refresh whenever you change any static file.
The django-livereload-server python package provides a spiffy solution. Install it:
$ pip install django-livereload-server
Add 'livereload.middleware.LiveReloadScript' to MIDDLEWARE_CLASSES in settings.py.
Then run
$ ./manage.py livereload
before starting the runserver.
More documentation can be found at the django-livereload-server github site
For me, opening Incognito Mode in Chrome let the browser show the recent changes in my .js static files.
To anyone who is using Firefox:
If you don't want to clean your browser cache or it doesn't solve your issue, you can try doing a hard refresh (hold Shift and press the refresh button).
In my projects settings.py I have set
ADMIN_MEDIA_PREFIX='/static/admin/'
The admin media is being served in the correct location, browsing to http://127.0.0.1:8000/static/admin/css/base.css gives me the base.css for the admin page. But when I inspect the admin pages they are still attempting to find the admin media at '/media/admin/'. I'm not sure what is wrong here. This is what I get when I attempt to find the setting in a manage.py shell.
>>> from django.conf import settings
>>> settings.ADMIN_MEDIA_PREFIX
'/media/admin/'
This should be '/static/admin/'.
James was right. I had a second settings.py defined for my development environment that was overriding my main settings. I've deleted ADMIN_MEDIA_PREFIX from that file and now everything works as expected. Thank you!!
I sometimes get a similar problem where I've clearly made changes but the code still behaves a la pre-change. Sometimes making sure I've saved with fix it, but other times the compiled python files (.pyc) don't seem to update. Deleting the .pyc file sometimes fixes my issue. Note that when you ./manage.py runserver again, the .pyc will be generated with the updated code.
Maybe try deleting your settings.pyc?