New-ish coder and first time stack overflow poster. I've made a django project with 3 pages. Sometimes no pages are found, sometimes the index page loads without 404 error but I can't link to other pages and the css files I've pointed it to doesn't render.
Errors I'm getting is:
404 error
I have slept on the problem, looked up file formats to make sure my project and app url.py is correct. added app to settings. imported correct classes. I have also tried clearing my browser data on chrome and using a different browser.
If anyone could take a look at my small project on github that would be amazing. https://github.com/Connell-L/portfolio_website is the link or if anyone wants me to post code files on codepen or something I'm more than happy to do it.
Edit: https://github.com/Connell-L/portfolio_website/tree/branch1 it's here sorry lol
You need to add STATICFILES_DIRS and STATIC_ROOT to your project settings.py
like this:
BASE_DIR = Path(__file__).resolve().parent.
the above line of code has to be at the bigining of your settings.py file
STATIC_URL = "static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Related
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.
I Build a WebApp and I am stuck on a Problem.
Images are not showing in Production (DEBUG = False)
settings.py
MEDIA_ROOT= '/home/new/gitrepo/main/media'
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '/home/new/gitrepo/static/'
What have i tried
I tried python manage.py collecstatic BUT images are still not showing in the Website.
I found many answers on this Topic like THIS. BUT it didn't worked for me.
I don't know what i am doing wrong.
Any help would be appreciated.
Thank You in Advance.
Was it working in development? I think yes
open Pythonanywhere.com => choose "Web" from navbar => scroll to static files
and here you let pythonanywhere know where are the URLs for showing static data from your server
Add those two
URL Directory Delete
/static/ /home/myWebsite/static
/media/ /home/myWebsite/media
The /home/myWebsite/media is a path to where you store the static files for the given URL
here is a screenshot of my configurations
If it's not showing make sure you have something in your main URLS.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have a working app and have downloaded the relevant django files locally (via git) and am trying to run the app locally. However, static files are not loading.
I receive the following console error when accessing, say, the home page (http://localhost:8000/home/) of the app:
GET http://localhost:8000/static/imported_JS/jquery/jquery.min.js net::ERR_ABORTED
or this error:
http://localhost:8000/static/globe.png 404 (NOT FOUND)
In my settings.py file, I can confirm:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)),'..')) # i.e. location of settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I can also confirm there is indeed a static_cdn/ file directory and it does contain the relevant static files.
The live dev and prod versions of the app work (which are hosted on separate web servers) but, for whatever reason, when running the app locally, static files are not served.
Any ideas on how to have the localhost server static files? Most of the answers on SO regarding this type of question relate to mistakes with setting up STATIC_URL vs. STATIC_ROOT but these appear to be correct (at least on dev and prod).
Let me know if you require more info. Thank you.
UPDATE
Well, I struggled with this problem for an hour or so, and 5mins after posting this SO question, I think I found a solution.
Changing from this:
STATICFILES_DIRS = []
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
to this made the difference:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_cdn/') # add STATIC_ROOT to DIRS
]
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I'm not sure why this works but I'll indicate your response as Answer if you explain why. Thanks.
I have installed Django file browser and added grappelli and filebrowser to my installed apps. And did every thing said in documention but when I browse http://127.0.0.1:8000/admin/filebrowser/ I got 404 page not found error. What's the problem? I configured my media settings like this:
# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
FILEBROWSER_DIRECTORY = '/media/'
The problem is your FILEBROWSER_DIRECTORY setting. It should have absolute path.
Unfortunately, the error is hidden by the nasty 400 Bad Request, as explained in this ticket.
Just solved the exact same problem, and it's the oldest one in the book: permissions
(facepalm)
make sure your media dir is chmod-ed to 755
create media/uploads dir and also chmod it to 755
In settings.py, I don't use the FILEBROWSER_DIRECTORY (I guess that uploads is the default)
I really think that throwing 400 Bad Request is utterly useless and anoying, but oh well...
Just a related note: I don't think there is anything at /admin/filebrowser/, it just gives me a 404. Please correct me it I'm missing something here :)
This should be super simple, but somehow its has had me stuck all morning. I'm developing locally, using the django debug server, and with this filestructure:
/project/ (django project)
/static/ (static files)
In the settings.py MEDIA_ROOT and MEDIA_URL are both set to '/static/' and I'm using this in my urls.py
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),
In my templates, the files that need to be server from the static directory are configured as such:
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">
That all works as it should - the javascript/css/images are all served properly from the hompage. However, when I go to a subdirectory, such as http://127.0.0.1:8000/news/ then all the links are broken.
I've tried using a variety of the os.import options to get it to do the relative links properly, but havent had any luck. Is there a way that I could force it to be relative to the base url, or perhaps hardcode it to my filesystem?
Any help would be amazing!
In this line in your urls.py file, the '../static' should be changed to an absolute directory. Try changing it and see what happens.
Your file:
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),
Should look more like:
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/full/path/to/static'}),
To give you an example, mine is set up a little differently, but I still use a full path.
Here's how mine is setup:
in settings.py
STATIC_DOC_ROOT = '/Users/kylewpppd/Projects/Django/kl2011/assets/'
and in urls.py:
(r'^assets/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes':True}),
I'm serving my static files from 'localhost:8000/assets/'.
I just had the same problem, and I solved it by removing first slash from STATIC_URL='/static/' and making it STATIC_URL = 'static/' just like philipbe wrote above.
Wierd thing is that '/media/' works fine for MEDIA_URL at the same time (while 'media/' breaks layout).
Anyway - just change STATIC_URL to be equal to 'static/' with no leading slash, and you'll solve it.
How do your links break when you go to a subdirectory? Can you explain that further please.
Does django 1.3 support some kind of strange relative url routing for static media?
If it can be served from the homepage but not others, doesn't that sound exactly like your STATIC_URL is a relative location?
What is your STATIC_URL? It should be absolute and start with a slash.