Missing bootstrap resources in Django-Rest-Framework - python

I'm using the new django-rest-framework 2.0 and have been following the tutorial for creating a rest based API. The API is now complete, however I am having trouble getting the bootstrap resources to load, all return with a 404 Not Found from Django.
I feel like the resources should be loaded from django-rest-framework module's static directory, And when I do a listing on 'python2.7/dist-packages/rest_framework/static/rest_framework' I see the css, js, and img directories I need with but I have been unable to find any place in the documentation that shows how to link the CSS from the module to my project.
What is the best course of action here? Should I download the source and copy the folder into my /static directory? Symlinking is out of the question because I need to check the project into a central repo.. Ideas?

First up, I'm assuming that you mean the bootstrap static resources aren't loading for the browsable API? (Although I guess it could be that you're trying to use them elsewhere in your project?)
If you're running with DEBUG=True they should be served automatically, but once you're running with DEBUG=False you need to make sure to run manage.py collectstatic and ensure your STATIC_ROOT and STATIC_URL settings are correct.
Django's static files documentation should help: https://docs.djangoproject.com/en/dev/howto/static-files/
If you're still not having any luck I'd suggest you double check your Django version (1.3 and upwards is supported), and REST framework version (Anything from version 2 onwards), and make sure you step through the tutorial step-by-step, taking care particularly with the project setup.

If you're using Heroku you'll want to make sure you are using the configurations in the getting started documentation (https://devcenter.heroku.com/articles/getting-started-with-django). See the section "settings.py" and "wsgi.py". I was having the same problem and this solved it.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
wsgi.py
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

CSS is not found. The fix would be to make the missing css files (404 for files seen in the browser console) available. That's it.
You can make the css files accessible in backend(need some tweaks) or in front end(comparatively easy).
This solution works perfect if you have a seperate frontend & backend(restful) setup such as Django-Django rest framework and AngularJS..
Let us say if django backend is running at 8000, and front end is running at 9000.
frontend.example.com loads front end JS app running at 9000
backend.example.com loads django app running at 8000
in settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = 'http://frontend.example.com/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
then in terminal
$ python manage.py collectstatic
Then go to dir where you have the folder static
$ cp -rf static/* /frontend-code-directory/static/
DONE.
What I have done basically, is copying all css of django apps to the frontend. Frontend serves this easy as frontend is already a collection of static files.

Related

why django admin template doesn't load

Since I had started Django, I'm tackling some ridiculous problems. Recently, when I start a new project and I run it on the server, Django's admin CSS is not loaded. The last project I ran on the server, after a while it was okay, and the real Django admin template was there and the CSS was loaded and it was working. But this time again the same problem happened and I don't know how to solve it cause the project here is the photo is raw, with no special code.
I don't know if it's Chrome problem or not, but I have tried it on other browsers and it was the same thing.
I would be glad if you can help me
the answer I found was to change STATIC_URL = 'static/' in settings.py to STATIC_URL = '/static/'. Only one '/' may change your whole admin appearance. This problem may not happens to everyone but running code in Pycharm, I had been tackling it for such a long time.
If this problem is happening locally, then just change your settings.py
DEBUG = True
#...
#...
STATIC_ROOT = YOUR_STATIC_ROOT_DIRECTORY
Then run collectstatic, make sure you have a proper STATIC_ROOT directory which is also in your settings.py file
python manage.py collectstatic
If you have deployed your app on a production server then, you have to follow certain things in order to get stylesheet and javascript files.
Firstly
In your urls.py you need to add this code
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then you have to use a third-party media file server(CDN) Like AWS S3 or you can serve your css, js or media files from your Django server
If you don't want to use AWS S3, then you can either send css/js using your django app (which is not ideal to do), or you can use nginx or apachee to send your css/js
For Nginx to send js/css
You have to add a Static and Media File Root that can be accessed by your django application, I usually use /var/www/ to serve static and media from any Linux server
STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = 'var/www/media'
Then if you are using nginx
server {
server_name domainname.com;
access_log off;
location /static/ {
root /var/www/static;
}
location /media/ {
root /var/www/media;
}
If it is still not working, then your django app might not be able to use the given static_root and media directory, make sure your app has access to them
If you want to send your js/css from your django app (Better not to do in production)
Then
To install whitenoise
pip install whitenoise
In your settings file
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]

Python Django Admin not showing CSS

I was working with Django latest version by watching a tutorial on YT, but for some reason, my admin page isn't coming how it has to. It does not have a style or CSS.
[enter image description here][1]
[1]: https://i.stack.imgur.com/MrASY.pngenter code here
Are you running in debug or production mode?
Django delivers static files like CSS, JS etc in debug though it's own development server.
When you run in production mode / through a web server you have to configure your web server to deliver the static files.
Cheers
First of all I think you should first try this in the command line:
python manage.py collectstatic
After that go to settings file and check STATIC_URL and STATIC_ROOT
It should look like this(if you didn't change anything):
STATIC_URL = '/static/'
STATIC_ROOT = "/var/www/example.com/static/"
Static Url: URL to use when referring to static files located in STATIC_ROOT.
Static Root:
The absolute path to the directory where collectstatic will collect static files for deployment.
and also you can check the documentation.

Django not serving media files when DEBUG is turned to False

Django is not serving my media files when DEBUG = False.
Here is my code:
DEBUG = False
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
I've tried several things but none have worked.
Please help me if you know the answer to my problem!
I recently had a similar issue.
In development, the Django development server serves the static files as documented in the Django docs. If settings.DEBUG is False then static file serving by the development server stops. See this Stack Overflow post: Why does DEBUG=False setting make my django Static Files Access fail?
In addition to these configuration steps, you’ll also need to actually serve the static files.
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
For production, there are options for serving static files as documented here.
I solved the issue of serving static files in production on an instance of Dokku—the smallest PaaS implementation you've ever seen—that is similar to Heroku.
I used a package called Whitenoise that is discussed in this blog post.
I found Whitenoise to be the easiest solution to serving static files for a small Django application without having to configure a separate static file server.
if you want to deploy your django project best practices please set DEBUG=False and ALLOWED_HOSTS=['your_host'] and than collect your static and media files using command ./manage.py collectstatic
As i can see your question is regarding media files but the answers are given about static files which are different if we see them at deployment level. For static files, you can simply use whitenoise. That will work perfectly. It will also work with media files but only when DEBUG = True and for shorter time(e.g, if you deploy your app on some platform which uses dynos like heroku, dynos refresh after a particular time. In heroku it is 30 min and after that media files will be deleted no matter whether the debug is set to true or false.) Which is not secure during deployment.So to make your media files working, you need some third party support to serve your media files to your site like Amazon S3 buckets which will store your media files and will serve them to your website.
Hope you got the answer!

Django collect the static files fails

While I run python manage.py collectstatic
It says You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
This is my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]
How can I solve this issue?
You need to add STATIC_ROOT as a parameter to your settings.py.
Here is how it works with those static files:
In STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to server static files, this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them... - exactly! - into the STATIC_ROOT folder.
At this point 2 things should become clear:
1. You need to provide STATIC_ROOT in your settings.py.
2. The path provided in STATIC_ROOT cannot be part of STATICFILES_DIRS, as this would mean django would need to put the folder into itself.
Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that. Nevertheless, the STATIC_ROOT parameter is always required.
Hope that helped to clarify things. Happy coding! :)
STATICFILES_DIRS tells django where to look for files to collect when using the collectstatic command
STATIC_ROOT="../my_real_static_folder" will tell django to put the static files it collects there and the static_url will get files from there.
... however be warned serving static files through django only works if debug is True (in settings.py) and is strongly discourages in production environments
Set STATIC_ROOT = 'path to ur static root directory' in settings.py.
Note that when you run collectstatic all files from ur static dirs are collected to the static root directory provided in static root path above. U can find more info about this in documentation here

Django - Static pictures not found

Helo all,
I am running a Django application in development mode. I have collected static files into a /static/images/ directory in my project.
In my template I try to link an example image:
<img src="{{ STATIC_URL }}items/no_image.jpeg"/>
{{ STATIC_URL }}items/no_image.jpeg
(The bottom line is printing for debug purposes)
The picture shows with a broken link, and the bottom line prints out the correct directory:
/static/items/no_image.jpeg
Inside my project, I do have the /static/items/no_image.jpeg file.
In my settings.py I have:
STATIC_ROOT = os.path.dirname(__file__)+'/static/'
STATIC_URL = '/static/'
Can anybody help?
Thank you !
Found out the problem. The problem was I was using the wrong directory. STATIC_URL was named /site_media/ when it should be /static/. Changed it and everything now works clear as water...
I think it's slash issue. Try
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
Maybe you've misunderstood the collecting of static files. There's no need to collect them into a target directory with the collectstatic command during development. That's meant to be done for deployment.
During dev, you only add the django.contrib.staticfiles app, then specify the STATIC_ROOT, STATIC_URL and STATICFILES_DIRS as described here.
Then, in your base urlconf you need to add the url rules for the staticfiles as described here. That's it for "development mode".
In production mode, you run the collectstatic command first, then shove the resulting directory over to the server where your apache (or nginx or whatever) is running, and let him serve that directory unter www.yoururl.com/static/
The whole story about handling staticfiles in django almost drove me nuts ... and even today I have to think for some minutes when trying to remember it or explain it. :-/ Don't worry if it confuses you sometimes.
I think I recently had a similar issue. Try placing your images directly in /static/ instead of /static/items/. If you want to be able to directly link to /static/items/sample.jpg you need to add /static/items/ to your STATIC_ROOT in SETTINGS

Categories