Django at PythonAnywhere doesn't recognize static url - python

The problem looks similiar to the problem here:
Pythonanywhere, how to use static files? url? ,but I cannot comment there.
I've started learning Django and when everything worked on localhost that on PythonAnywhere it does not.
At projectname/settings.py I've set:
STATIC_ROOT = "/home/*username*/*projectname*/Static/"
STATIC_URL = "/s/"
and even URL's from static folders in apps.
After trying to run
python3 manage.py collectstatic
every file *.js, *.css and images were coppied to the projectname/Static folder.
But... none of them were recognized after launch of the app.
I've set
{% load static %}
used tags
{% static "assets/css/theme.css" %}
At the source code I can see the proper link to css file:
<script src="/s/assets/js/seen.min.js"></script>
And everything would be fine, but the "/s/" isn't recognized by django and it tries to find the view in urls.py.
After opening the link to: username.pythonanywhere.com/s/assets/js/seen.min.js I've got standard, debug 404 page with the path of urls.py tries.
How to solve this annoying problem?

You need to add a static file mapping on the web app. Look for the "Static files" heading on the web app tab.
From what I can tell of your setup, you'd need to put "/s/" for the URL and "/home/*username*/*projectname*/Static/" for the directory.

Related

cannot read static files after deploying my project on pythonanywhere PYTHON DJANGO

I dont know if I did this right but during development phase I placed all my static files on my project's main directory, it looks like this:
myproject
myapp
manage.py
myproject
- settings.py
- urls.py
...
static
- css
- somecssfile.css
- js
- somejsfile.js
...
templates
now my settings.py file looks like this:
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_URL = '/static/'
after I deployed my project into pythonanywhere I added STATIC_ROOT:
STATIC_ROOT = '/home/jeremiramos/cvs/staticfiles/'
it does copy my static files to staticfiles folder when I do collectstatic on pythonanywhere console but it cannot read/load the static files I used on each and every page including admin page.
and my staticfiles settings on pythonanywhere looks like this:
I checked my console and I got some errors:
Refused to apply style from 'http://jeremiramos.pythonanywhere.com/static/css/index.css/' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
I've tried checking the forums of pythonanywhere.com but it doesn't seem to help.
Can anyone please explain what did I do wrong and what should I do? Thank you!
Since http://jeremiramos.pythonanywhere.com/static/css/index.css loads the actual file, it means that static file mappings are OK. Trying the url with trailing slash (as in the error message you've shown) gives 404 and MIME type error message might be a symptom of it. Remove trailing slash from the url and check if that solves the issue.

Django admin wont load certain static files

I have a strange issue that's occurred on several django projects and I'm trying to figure out a fix for it. For some reason, all the static files for the admin area load properly including the js, css, and images but 2 files for the side nav bar (which are in my static directory along with everything else) wont load. The files are the nav_sidebar.css and nav_sidebar.js.
I've figured out a work around and added these inline in the admins base.html template and deleted the links to these files. This works but it's kind of ridiculous that it manages to load all other static assets fine but not these particular files. I have my static root and directories set up properly, have nginx pointing to the correct static directory, and have done collect static and restarted the server. Everything I could possibly think of but it doesn't work.
Considering this has happened on 3 straight projects, I think this is some kind of bug rather than an error on my end.
So I was able to answer my own question in the end and for anyone else that comes across this problem here are the steps I took.
For the admin static files, before you run collect static are being sourced directly from the wherever your python(versionNumber)/site-packages/django/contrib/admin/static is located. Unless you run collect static, or manually copy and paste them into your static files directory, these admin files wont be there.
Now I'm sure this is basic knowledge for any django user and I even did this and the error still persisted. What I found, is that I set my static urls up like:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
And for some reason, django was still sourcing the admin files from that same directory the django source is located. So I simply changed the url path and static root to:
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And ran manage.py collectstatic again and that fixed my admin area and everything is working properly now.
These two files are introduced in django 3.1 for nav_sidebar feature. In admin/base.html, it says:
{% if not is_popup and is_nav_sidebar_enabled %}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/nav_sidebar.css" %}">
<script src="{% static 'admin/js/nav_sidebar.js' %}" defer></script>
{% endif %}
is_nav_sidebar_enabled by default is enabled. Did you put something in the root urls.py to disable that?
something like:
admin.site.enable_nav_sidebar = False in your root urls.py?
The doc about the new nav_sidebar feature is here

Using CSS Templates with Flask: Overriding "static system"

I'm just starting out with Flask, and I was wondering what the best method for
dealing with how flask deals with static files when trying to use a premade CSS template.
Basically, I have downloaded a CSS template that I liked off the internet, but when if I simply drag the files into my flask application folder the CSS, JS, and image files do not work since they are not located in the static folder.
But if I move all the static files into the static folder, then I have to go through all the code and change the link urls, which is very time consuming.
The CSS Template I am using has an index.html that uses links like
<link rel = "stylesheet" href = "css/style.css" >
I have set both the static_folder = ""
and the static_url_path = "" in my flask app and I have moved the css, js, and image folders from the downloaded template into the base folder for the application, but the links are still not working.
Is there a better way to deal with using premade CSS templates with flask? Can I override the need to put css and js and image files in the static folder somehow? Thanks for your help!
(Sorry for opening this old post, but I'm on a badge hunt :])
There are several possible solutions, but the one I would recommend is to move the file style.css to folder <server_root>/static/css/.
Then create the flask app like app = Flask(__name__, static_url_path=''), what means that it still serves static files from the static/ folder, but on path / (so <server_root>/static/css/style.css is served on /css/style.css).
With this setup, your links href="/css/style.css" will work.
However, it's strongly recommended to use flask.url_for('endpoint', param='value') instead of /endpoint/url/value both in code and templates (surrounded with {{ ... }}) for all URLs - static files ('static', filename='css/style.css') and your own endpoints. So if your endpoint looks like this,
#app.route('/some/path/<variable>')
def some_endpoint(variable):
# do something and return response...
... you can use url_for('some_endpoint, variable='something') no matter what the actual URL (/some/path/something/ in this case) is. (Tested python 3.6.7; flask 1.0.2)

STATICFILES_DIR does not work on Apache web-server

I'm testing one and the same application both on the default Django server and on Apache and I see a lot of big differences. I managed to resolve some of them, but at this moment I'm unable to resolve a major difference. So, in project settings.py file I have this code:
MODULES_DIR = BASE_DIR + '/system/modules/'
for item in os.listdir(MODULES_DIR):
stat = os.path.join(MODULES_DIR, item + '/static')
if os.path.isdir(os.path.join(MODULES_DIR, item)):
INSTALLED_APPS += ('system.modules.%s' % item, )
STATICFILES_DIR += (stat, )
APPS_DIR = true
This code is supposed to populate INSTALLED_APPS dynamically, based on the contents of BASE_DIR + '/system/modules/' folder. In other words, if there is a folder inside /modules, this folder becomes an application. Likewise, I build dynamically STATICFILES_DIR - in this case it is supposed, that every single folder/application (which is inside /modules folder) has a /static folder with static contents - js, css etc. For example, it may be such a construct:
\modules
\DefaultModule
__init__.py
urls.py
views.py
\static
test.js
\templates
DefaultModule.html
And DefaultModule.html in this example loads static files like this:
<html>
<head>
{% load static from staticfiles %}
<script type="text/javascript" src="{% static "test.js" %}"></script>
It is rather interesting, but on default Django server this logic works perfectly, so that when I go in my browser to localhost/DefaultModule/, I see a template DefaultModule.html loaded and I see test.js file loaded from http://localhost/DefaultModule/static/. However, on Apache the template is rendered too, but the test.js file is loaded from http://localhost/static/ what eventually results in a 404 NOT FOUND error. So, for some reason Apache server does not take into account STATICFILES_DIR. And yes I checked its (I mean STATICFILES_DIR) contents and it is the same. In both cases STATICFILES_DIR contains modules/DefaultModule/static/, but on Apache it is ignored for some reason. Hope someone can help. Thanks!
I think you should read the Django docs on static files. Looks like you're falling into the simple and old Django Static File Hosting an Apache
Check it out and let us know.

How can i serve html files from diff directory in django

I need to make an app where i have the ready made standalone html templates avaialbe in my
app/ready_templates/template1
Now i have made an view where i display the name , thumbnail of that template to show to users
But there is demo link there which i want tthem to see the whole template.
basically if they click on demo then i want to redirect them to
template1/index.html so they can see all the templates images , js etc without linking anything to django views
So your requirement seems pretty straightforward. You can do this using the static files feature in Django. Basically whenever you have a bunch of static content like html, css, js images you put it in a folder in your app called static. For example the admin app that ships with Django has all it's static content in django/contrib/admin/static. Then you could place something simple like the below into your django template to link to the static content. You of course don't have to hard code each name like template1 you could have it dynamically generated in your django template.
{% load static from staticfiles %}
<a href="{% static "template1/index.html" %}" />template1</a>

Categories