I'm deploying a python3 flask application in aws elasitc beanstalk (Amazon Linux 2 platform). The folder structure is as follows:
|-app/
|-templates/
|-static/
| |-css/
| |-js/
|-app.py
In the template files, importing of static resources are defiled as:
i.e. JS file:
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery.js') }}"></script>
In the EB configurations, I've defined the static resources as below
But the problem is, these resources are not loading and giving 404.
i.e.: https://example.com/static/js/jquery.js cannot be loaded. But if I try https://example.com/js/jquery.js, it works.
Have I done the configurations wrong??
Based on the comments.
The issue was due to overwriting /static path. In python EB environments, /static path is used by default to server the static content:
By default, the proxy server in a Python environment serves any files in a folder named static at the /static path. For example, if your application source contains a file named logo.png in a folder named static, the proxy server serves it to users at subdomain.elasticbeanstalk.com/static/logo.png. You can configure additional mappings as explained in this section.
The solution was to use the default settings and remove the overwrite.
Related
I deployed my site at the level of my host, but the imgaes are not displayed.
I did python manage.py collectstatic and it copied the files to my STATIC_ROOT
myprojet/settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
STATIC_URL = '/static/'
STATIC_ROOT='/home/www/mySite.com/static/'
index.html
{% extends 'layout/base.html' %}
{% load staticfiles %}
{% block content %}
<img src="{% static 'images/icone.png' %}" style="width:350px;height:100">
{% endblock content %}
I used {% load staticfiles %} as i am using django 2.2 but the images are not showing
I don't know where the problem is.
You need to configure your web server to serve the static files from the STATIC_ROOT directory. Django does not handle static files by itself in production, it only does so in development with the runserver command. Depending on your web server, you need to add some rules to tell it where to find the static files and how to serve them. For example, if you are using Apache, you can add something like this to your configuration file:
Alias /static/ /home/www/mySite.com/static/
<Directory /home/www/mySite.com/static>
Require all granted
</Directory>
This tells Apache to serve any request that starts with /static/ from the /home/www/mySite.com/static/ directory, and to allow access to it. You may need to restart or reload your web server for the changes to take effect.
Explanation:
Static files are files that do not change dynamically, such as images, CSS, JavaScript, etc. They are usually stored in a separate directory from the rest of the Django project, and they need to be collected and copied to a single location (the STATIC_ROOT) when deploying the project. This makes it easier to manage and optimize them, and to serve them efficiently with a web server.
Django provides some tools and settings to help with static files, such as the static template tag, the STATIC_URL, the STATICFILES_DIRS, and the collectstatic command. However, Django does not serve static files by itself in production, because it is not designed to be a web server and it would be inefficient and insecure to do so. Instead, it delegates the responsibility of serving static files to a web server, such as Apache, Nginx, etc. The web server is faster and more reliable at handling static files, and it can also apply some features such as compression, caching, etc.
To configure your web server to serve static files, you need to tell it where to find them and how to serve them. This depends on the web server you are using, and you may need to consult its documentation for the specific syntax and options. However, the general idea is to create an alias or a location that maps a URL path (such as /static/) to a directory on the file system (such as /home/www/mySite.com/static/). Then, you need to grant access and permissions to that directory, and optionally apply some settings to optimize the performance and security of serving static files.
Examples:
Here are some examples of how to configure different web servers to serve static files from Django:
Apache: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/#serving-files
Nginx: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/uwsgi/#serving-files
Gunicorn: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/gunicorn/#serving-static-files
Heroku: https://devcenter.heroku.com/articles/django-assets'
I am trying to run a python dash app on AWS Elastic Beanstalk service but it is missing the css, and the folder in which I have csv's as data to give results.
I could only declare one static folders but my directory looks like this.
My folder structure:
You can use a configuration file to configure static file paths and directory locations using configuration options. You can add a configuration file to your application's source bundle and deploy it during environment creation or a later deployment.
If your environment uses a platform branch based on Amazon Linux 2, use the aws:elasticbeanstalk:environment:proxy:staticfiles namespace.
The following example configuration file tells the proxy server to serve files in the statichtml folder at the path /html, and files in the staticimages folder at the path /images.
Example .ebextensions/static-files.config
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/html: statichtml
/images: staticimages
More information can be found here: Elastic BeanStalk - Serving static files.
I am new in Django, previously I had a test site in which my static files were placed in static folders. There was a static folder for every app. But now I want to make a site, which will be deployed to python anywhere and so I have to have only one static folder.
This is the project folder: https://github.com/martin-varbanov96/fmi-fall-2016/tree/master/django/click_bait/miranda
You can see that in the settings file I have:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
in my target html file I have
<link rel="stylesheet" href="{% static 'home/css/style.css' %}" type="text/css">
which returns 404.
What is wrong how should I arrage my folders so that they are best working for the pythonanywhere host?
If you're getting a 404 error you should ensure debugging mode is on. If I assume it is on (as it is in the settings.py you have on GitHub), then your web server just isn't serving the static files. I would ensure you've run python manage.py collectstatic which will push the static files for the project to the static folder you've defined in settings.py.
I would also ensure that you've configured Python Anywhere to serve these files. According to their website (source: https://help.pythonanywhere.com/pages/DjangoStaticFiles/) you should ensure you have that static folder added so their server knows to serve the files. The steps at that link are as follows:
Go to the Web tab on the PythonAnywhere dashboard
Go to the Static Files section
Enter the same URL as STATIC_URL in the url section (typically, /static/)
Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)
On my local PC I can do "python manage.py runserver" and the site runs perfectly, CSS and all. I just deployed the site to a public server and while most things work, CSS (and the images) are not loading into the templates.
I found some other questions with a similar issue, but my code did not appear to suffer from any of the same problems.
Within the Django project settings the same python function is being used to allow the app to see the templates and the static CSS / image files. The templates are being found by the views and are loading without issue.
Both from settings.py:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates/css').replace('\\','/'),
os.path.join(os.path.dirname(__file__), 'content').replace('\\','/'),
)
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)
In the base.html file which the rest of the templates all extend:
<head>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "style.css" %}" media="screen">
</head>
Directory structure:
|project_root/
|--manage.py
|--project/
| |--settings.py
| |--__init__.py
| |--content/
| | |--header.jpg
| |--templates/
| | |--base.html
| | |--css/
| | | |--style.css
My first thought when the CSS didn't load is that Django couldn't find the style.css file, but since I am using the same "os.path.dirname(file)" technique as with the templates, I am not sure this is the case.
What do I have wrong here?
Edit:
I neglected to mention that both the PC and server are running Python 2.7.5 and Django 1.5.5.
You never mentioned it in your post, so I am guessing:
You never ran ./manage.py collectstatic
collectstatic finds all static files (css, images, js) and puts them in a directory (Choose this directory with the django setting STATIC_ROOT) Then you point your webserver to that directory
You should deploy the static files using your server and not django.
The official documentation mentiones using collectstatic but unless your static files are messed up it's usually not a requirement. You just need to have some directory containing all your static files. Then you just push it with the server to the same place django will be looking.
Say you're STATIC_URL is '/static/', so you need to add an alias which would map '/static/' to the static directory. For example, using Apache, you should add this line to your http.conf:
Alias /static/ /path/to/mysite.com/static/
That's it! This thing goes true to media files as well, and it'd be wise to remove any serving of static files done by django for the development server (these kinds of urls). Finally, check out the documentation for even more information regarding other types of deployment
I'm using AppFog PaaS system for a few days, and I love it, It's probably the best PaaS system that I've tested (I've used other 3 ones previously), but didn't find information about how to serve static content with the Web server in frontend (Apache https or nginx) I'm not sure what server is being used.
My app is a Python WSGI with CherryPy and works perfectly in AppFog but I don't wan't CherryPy to serve static content, I think that Apache httpd or nginx is a better option for that.
With Ryan's support, I'm finally able to load static files! Here are the steps:
Created a 'static' directory in the project root - here all static files will be collected running the collectstatic command.
Edit the settings.py file:
STATIC_ROOT = os.path.join( os.path.abspath( os.path.dirname(file) ), '../static' ) # May change depending on where your settings.py file is!
STATIC_URL = '/static/'
Add following line in urlpatterns variable in urls.py file:
url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT} ) ,
Finally, run collectstatic command in your local machine. This will copy all static files from the apps you are using:
python manage.py collectstatic
That's it. Push in AF :)
Downside: Need to run collectstatic every time we have a new static file...
Edit your nginx.conf file. In the server section enter...
# serve static files
location ~ ^/(images|javascript|css)/ {
root /var/www/html/appname;
}
images, javascript and css would be folders in your document root folder. Update all your urls accordingly.