Serving static in flask-admin project - flask

I added flask-admin admin panel for my FastAPI project. For the first view, it works, but it has no static. I found 404 in terminal:
"GET /static/admin/bootstrap/bootstrap4/swatch/Cosmo/bootstrap.min.css?v=4.2.1 HTTP/1.1" 404 -
So I can add this file in virtual environment but as for me it is not the best option and I decided to serve static from my project:
app = Flask(__name__, static_folder='static', static_url_path='/static')
I added bootstrap.min.css and bootstrap.min.css.map to
static/admin/bootstrap/bootstrap4/swatch/Cosmo
But it does not work. What's wrong?

Related

Using a readymade Template for django frontend

I tried using a readymade HTML5 template for my Django Project and updated the static links and all but it fails to load the CSS and js and other static files too.
My folder structure :
**MyProject
apps
templates
MyProject
static
MyProject
css
js
etc**
My Settings for static :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
TEMPLATES = [
......
'DIRS': [os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR,'static') .
..... ]
I have tried
using the collectstatic command.
Adding STATICFILES_DIRS.
Adding this code snippet to urls.py
Trace from terminal:
[04/Apr/2020 13:04:51] "GET /static/css/owl.theme.default.min.css HTTP/1.1" 404 1814
[04/Apr/2020 13:04:51] "GET /static/css/animate.css HTTP/1.1" 404 1772
[04/Apr/2020 13:04:51] "GET /static/css/magnific-popup.css HTTP/1.1" 404 1793
[04/Apr/2020 13:04:51] "GET /static/css/aos.css HTTP/1.1" 404 1760
[04/Apr/2020 13:04:51] "GET /static/css/owl.carousel.min.css HTTP/1.1" 404 1799
collectstatic will collect static resources that you have placed in your individual apps and copy them to an app directory within the project static directory. So, if you have an app static directory like this:
my_app
static
my_app
css
js
etc
your project static dir will look like this after collectstatic:
static
my_app
css
js
etc
You can also manually place static resources in the project static directory. This is a good place for your project wide css, js, and image files.
static
css
images
js
From the 404 errors you posted, the server is looking for your css files in the static/css/ directory and you have your css directory inside the MyProject directory. Move your css directory out one level and place your css files there and it should work.
static
MyProject
css
js
Your Error:
"GET /static/css/owl.theme.default.min.css HTTP/1.1" 404 1814
according to your error server is looking your file in static folder and then in css while your files is in static/MyProject/css
so, update your code in your html file
{% static 'MyProject/css/owl.theme.default.min.css' %}

Some static files in django serve 404 when other dont- any ideas why?

Using django 1.5
I got static files configured like this:
STATIC_ROOT = '/home/<user>/Projects/<name>/static'
STATIC_URL = '/static/'
i just run manage.py collectstatic
directory listing:
static/css
static/css/bootstrap.css
static/css/addressbook.css
static/css/bootstrap-responsive.css
static/css/rewrite.css
static/css/login.css
when I type
localhost:8000/static/css/addressbook.css i got 404
but:
localhost:8000/static/css/bootstrap.css
gives me a proper css content
WTF? they are in the same folder and have same user/rights/groups
part from menage.py runserver output:
[24/Jul/2013 12:18:19] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:19:16] "GET /static/css/login.css HTTP/1.1" 200 533
[24/Jul/2013 12:20:12] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:32:51] "GET /static/css/bootstrap.css HTTP/1.1" 304 0
UPDATE:
It's serving files not from "project/static" but from static folder under application folder
I figure that out by deleting static forder under one app - files start to give 404. Same if I disable AppDirectoriesFinder.
But it still not consistent some applications don't serve files even from "static" under application folder.
My ideal situation will be: AppDirectoriesFinder commented out and all files served from myProject/static/
You can't just drop files into the /static folder of your project. Django doesn't "see" files in that directory unless they were collected from the apps via collectstatic. You will get a 404 trying to access files that you drop in there manually.
You need to put static files into the /static directory of an app (if it doesn't belong in a specific app, just create a new one, for example "main" or "website").
Alternatively you can set the STATICFILES_DIRS for Django to search additional directories when you call collectstatic.
I had the same problem. Here's my solution:
Add this to your settings.py
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
#"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

GET / HTTP/1.1" 404 2032

i'm trying to install Fang of Mongo, i did all the steps required but whe i try to go to the http page i have got this error message:
muratalina#muratalina-To-be-filled-by-O-E-M:~/Documents/Fang-of-Mongo/fangofmongo$ python ./manage.py runserver
Validating models...
0 errors found
Django version 1.4.3, using settings 'fangofmongo.settings'
Development server is running at "http://127.0.0.1:8000/"
Quit the server with CONTROL-C.
[29/Jan/2013 12:36:52] "GET / HTTP/1.1" 404 2032
when i go to "http://127.0.0.1:8000/fangofmongo/" page i have got this error
The base URL for Fang-of-Mongo is:
(r'^fangofmongo/', include('fangofmongo.fom.urls')),
This means that you will need to navigate to http://127.0.0.1:8000/fangofmongo.
There is no base URL http://127.0.0.1:8000/ defined - so it will 404.
EDIT: Also Fang-of-Mongo appears to have gone 3 years, at least on Github, without an update - so you will most likely encounter other issues with the project running on Django 1.4.3.
Make sure you imported the include function in the urls.py, else add "from django.urls import include"
Here is how to Include another URLconf (in urls.py file)
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))

How do I serve static admin files in Django 1.4?

I have a Django project that was started with Django 1.2. Now I'm trying to run it under Django 1.4, in a development environment, using the built-in webserver. One thing I cannot get working is the static files for the admin interface. In my django server's window, I see:
Django version 1.4, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Jul/2012 11:38:04] "GET /admin/ HTTP/1.1" 200 6452
[18/Jul/2012 11:38:05] "GET /admin/admin/css/base.css HTTP/1.1" 404 4249
[18/Jul/2012 11:38:05] "GET /admin/admin/css/dashboard.css HTTP/1.1" 404 4264
I see that ADMIN_MEDIA_PREFIX has been deprecated, but I'm clearly missing the HOWTO that tells me what I have to do to get the admin pages working in development.
Have you tried running collectstatic to gather your static files?
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#collectstatic
I finally started another project with "django-admin startproject", and started picking through the settings file. I found that the new settings file had:
added 'django.contrib.staticfiles' to INSTALLED_APPS
added a few STATIC_ settings. I copied these four:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = ()
And now things appear to be working.
In django 1.4 you can use the static tag
{% load static %}
<a href="{% static 'img/foo.png' %}">

Django admin media not loading

I'm trying to deploy this application with nginx/gunicorn but I don't know why the admin media files are missing.
settings.py:
ADMIN_MEDIA_PREFIX = '/srv/www/antingprojects.com.ar/gobras/static/admin/'
I also tried:
ADMIN_MEDIA_PREFIX = '/static/admin/'
project folder:
/srv/www/antingprojects.com.ar/gobras/static/admin/css|js|img
urls.py:
(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve')
nginx access.log:
"GET /admin/ HTTP/1.1" 200 1556 "-"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/base.css HTTP/1.1" 404 1136 "http://antingprojects.com.ar/admin/"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/dashboard.css HTTP/1.1" 404 1141 "http://antingprojects.com.ar/admin/"
"GET /admin_media/img/admin/nav-bg.gif HTTP/1.1" 404 1114
You need to set up a symbolic link from the directory where the admin media files are to the static media directory you are using to serve your static files.
The admin media files should be at something like:
path/to/django/contrib/admin/media/
Once your symlink is set up you would set your ADMIN_MEDIA_PREFIX to the symbolic directory within the directory where you serve your static files, so something like this should work:
ADMIN_MEDIA_PREFIX = '/static/admin/'
Check to see where your admin is trying to load its media files from, and this should help you get started if you can't figure out which directory to use.
Here's a quick tutorial on symlinks:
http://ubuntuforums.org/showthread.php?t=255573
Your ADMIN_MEDIA_PREFIX is correct, you just need to remove /admin portion from your static view URL, because what you have now specified translates to a URI beginning with /static/admin/admin for accessing your admin resources through the static view.
Thank you for your help, the problem was in the nginx.conf, I wasn't locating /static folder.
location /static {
root /srv/www/antingprojects.com.ar/gobras;
}