I was going through serving media in django and the I encountered a project with this line of code
href="{{STATIC_URL}}/custom/js/list.js"
in settings, STATIC_URL= /xexo/static/. That means the url will become
127.0.0.1:8000/xexo/static//custom/js/list.js
notice the //. Surprisingly even if I do this
127.0.0.1:8000/xexo/static///////custom/js/list.js
or
127.0.0.1:8000/xexo/static/////custom/////js/list.js
in my browser, the list.js will still load.
My qn is, how is this possible?
EDIT
Here is another link I have found
Link 1
Link 2
This questions url is:
http://stackoverflow.com/questions/15260904/serving-media-in-django
I can add a few slashes in and it works fine...
http://stackoverflow.com/questions/15260904///////serving-media-in-django
My point being this isn't a thing associated with serving media in django, this seems to be typical browser behaviour.
Although it doesn't seem to cause any problems in the browser to have multiple slashes it might be best to avoid it and change that line to:
href="{{STATIC_URL}}custom/js/list.js"
so the double slash is avoided.
Related
I have set up a basic Django system and am testing out the admin app. The admin screen displays with raw styles and not the pretty ones shown in the tutorials and specified in the style-sheets.
Following several other questions, I found this is a common problem caused because Django doesn't normally serve static files. However, it does if DEBUG is switched on and I do have debug switched on. I followed all of the suggestions in the answers anyway to collect static files, etc.
If I enter the static file URLs directly they get downloaded and I can see the files in the developer mode of the browser (both Chrome and Edge). So I think the static files are being served.
Furthermore, if I save the page using the browser, it saves all of the static files and then if I open the main page it is shown with the correct styles. So the styles are working.
So it would seem to me that the files are being served and the browser is getting them and it can render them (from a saved file) but it somehow isn't working when served from Django.
Can anyone tell me what is going on?
EDIT:
Here is a strange thing: if, using the Chrome developer tool, I select the base.css file, click somewhere in the text of the CSS (say at the end of a line) and then add a space, suddenly the page appears correctly.
If I then do refresh the page it goes back to unstyled again.
EDIT 2:
I saw a recommendation to install the Whitenoise app to serve static file so I went ahead and did it. I turned off debug and presto! the styles appear. Turning on debug (so I presume django serves the files) and the styles go away. I saved both sites to the file system and compared the directories using a compare tool. There was no difference.
I'm not calling this an answer as the question is:
Why?
Also, I can't have debug on and get styles.
The project is hosted (support say to solve the problem yourself and therefore my friends hope for your help) and you can find it at: https://caparolcenterspb.ru
All styles, pictures and js appear on the main page, but not on other pages. You can see the errors directly in the browser, but just in case I give a screen:
It seems you are adding the path to the static file not from the STATIC_ROOT, but from the current URL. I'll explain with the example.
On the main page, you got the link:
https://caparolcenterspb.ru/
And looking for the static logo at:
https://caparolcenterspb.ru/static/img/Logo.svg
And all works fine. But if you will go to
https://caparolcenterspb.ru/services/
The URL for the logo request will change to:
https://caparolcenterspb.ru/services/static/img/Logo.svg
Which is wrong and there are obviously no files on this link.
So, the solution to your problem will be fixing STATIC_URL in Django settings. You need to use the same fixed STATIC_ROOT for all static requests and everything will be fine.
It is pretty simple, just check everything is done as it described here:
https://docs.djangoproject.com/en/3.0/howto/static-files/
The shown error explains your MIME is text/html not text/css. It's linked to your browser or OS.
Try to add this in your settings.py:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
I stumbled across on a very strange issue. The dev env is Django 1.10 & django-cms 3.4.4. When I try to load localhost in Chrome & Firefox everything works fine, but if I try to open it in Safari, it fails with NoReverseMatch at / and it fails with random urls. I also tried to
curl http://localhost:8000/
and it works fine.
Also tried on Windows and different browsers and it fails for some browsers with the same error.
Does someone have any idea what could be the issue?
Thanks!
P.S.
Reverse for 'model' with arguments '(u'bmw',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
It seems that it doesn't load any urls at all. 0 patterns tried means there is nothing loaded I guess?
I think I found it. Long story short
The default cms is configured to use English as primary language, but I want my primary language to be Bulgarian. I configured the CMS to support both languages and I've made Bulgarian to be default and English is disabled. Then I got rid of the i18n_patterns() wrapper in urls.py to get rid of the /en/ /bg/ etc prefixes. So far so good. The admin site works great. There is only one language - Bulgarian, there are pages and custom apps attached to them. When I load the front end with wrong url on Safari just to see all the supported URLs, I can't see any applications attached and urls for them. If I do the same with Chrome - it shows urls for all attached applications. It turns out that Chrome loads the Bulgarian page configuration and all the configured applications, but Safari loads the English page which is disabled and there are no attached applications to it.
My first guess was the Accept-Language header. Safari uses Accept-Language: en, but other browsers send bg. I tried to tamper the data, but nothing happened. Then I noticed there is a django_language cookie being used by the LocaleMiddleware that actually determines the language. When I disabled the middleware, everything worked like a charm.
Thanks!
When using django, compressor, and clevercss, I set my css url to an absolute path. Clevercss is then passed the path of the .ccss file without the COMPRESS_ROOT prefixed (the absolute path). When I set my css url to a relative path, clevercss processes the ccss files, but the browser then correctly looks for relatively placed css files (e.g. mywebsite.com/profile/user/1/css/stylesheet.css)
Compressor, however, does use the MEDIA_ROOT when the css link is a relative url, but not when an absolute url is used. This has the unfortunate effect of my css either being rendered by clevercss and not accessible by the browser (unless on the home page), or clevercss not having access to the files (due to an absolute url being used). Ironically, the examples offered on http://github.com/mintchaos/django_compressor use absolute urls for the css paths.
I think I'm doing something wrong here, but I'm not sure where it could be and have spent quite a few hours looking. I'm also currently running this locally through ./manage.py runserver and serving some static files (images) through django. (this is fine for my local development).
I can't speak to django-compressor specifically; but I have been dealing with finding a good automatic compression solution for the CSS and JS files of my Django-powered web applications. I'm currently using django-static. It's been really easy to set up and use, IMO. I was running into some issues running django-compress (different from django-compressor) when I decided to give django-static a try. So far it's been great. Might be worth checking out. It can be found here: http://github.com/peterbe/django-static.
I am working on a Django project for a company. This project worked very well before today.
Today I found a page can not show images (and their corrsponding links). I checked source code of THAT PAGE, I found there are images and links, I just can not find them on the page.
I checked the auth of the server and I am sure I can write things to the database. In fact, I think it is not database mistake because I can find what I want in the page source code, I just can not find them on the page.
Oh my Gosh, I am going to be crazy...
Has anyone suffered similar problem? What kind of problem could it be?
Please help me! Thank you very much!
PS: I can not provide any source code of the project because some business limit...I am really sorry...
Try exploring the site_media directory. If you're images are being served up as static content it could be related to the permissions of that folder on local disk or based on the settings.
Within your urls you may have something similar to:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/path/to/media'}),
OR it may reference the path in the settings file something similar to:
STATIC_DOC_ROOT = '/path/to/media'
If the link looks correct for the image, but the side is not rendering the image even directly your issue will be somewhere in these areas.
For more information check the django docs:
http://docs.djangoproject.com/en/dev/howto/static-files/