TinyMCE popup windows not working in Django development server - python

TinyMCE is working just fine, all except for the popup windows. They come up blank, and after a little bit of Google searching, apparently it has something to do with cross domain errors with Firefox and Django. I tried using document.domain, but I have a feeling that it doesn't work when you're using the Django development server (http://127.0.0.1:8000).

There's nothing wrong with 127.0.0.1 as a domain. The problem is that it's different to your media domain localhost, although they both point to the same thing.
TinyMCE doesn't like different domains for the media, which is why having a relative MEDIA_URL would work. Using the URL http://localhost:8000/ to access your server would also work, because the domain would be the same as the MEDIA_URL, so too would setting your MEDIA_URL to 127.0.0.1:8000/m/ and accessing it with http://127.0.0.1:8000/.

Instead of using 127.0.0.1:8000, if I use localhost:8000, everything seems to work perfectly. I'm not sure why. Maybe it's because localhost:8000 is a domain and 127.0.0.1:800 is not.

Related

Django CMS - link to uploaded image returns 404

I have a Django 1.8.4 site with Django CMS 3.1.3. Via the djangocms-picture plugin I want the editors to be able to upload images to the site. If I upload an image on a page using this plugin, the image ends up in the following folder on the server:
[PATH_TO_APP_ROOT]/media/cms_page_media/8/
Where ‘8’ is the id of the page. When I browse the page, the link to the image is given as
https://[MY_SITE]/media/cms_page_media/8/my_image.png
But this link is not correct and I get a 404. I am having a hard time figuring out what I am missing but it seems that there is some URL setting that should be changed.
In settings.py I have:
MEDIA_ROOT = os.path.join([PATH_TO_APP_ROOT], "media")
MEDIA_URL = "/media/"
Where [PATH_TO_APP_ROOT] is the same as given above. I have not changed any of the media-settings given in the Django CMS docs so they should all be default. I am confused that the server looks for the image in /media/cms_page_media/8/my_image.png as the cms_page_media-part of this URL comes from CMS_PAGE_MEDIA_PATH which is not a URL setting(?). What setting do I need to look for to correct this?
Note that everything works perfectly for me in DEV (locally) - the problem is only on the production server.
Finally had time to look into this: The problem was that the web server (pythonanywhere) was not configured correctly. In their admin tool, I had to specify the path to media folder. When I did this, it worked, without having to change any Django settings.
more info here: https://help.pythonanywhere.com/pages/DjangoStaticFiles

Session is shared between two Flask apps on localhost

So, I have two Flask apps running on localhost, one on the port 5001, and the other one on the port 5003, and apparently both are using the same session. If I log in on one app, it logs out on the other. And for example, recently, if I logged in using my email on one app, it would also log in my account on the other app, since I have users using that email on both apps, and I was using the email as an user identifier, but that stop happening when I used another id for the users.
I'm using Flask-Login, and Google Chrome (the same thing happens in Edge).
I'm not really sure if this could also happen in production, we'll probably use the same host for both apps, so that would be a problem. If this is something related only to localhost, then it's ok, but I don't think that's the case.
Any idea of what could be happening here?
Thanks in advance.
I had a similar problem, and I think the cause is that both instances would use the same "session cookie" in the browser.
The solution that fixed it for me was renaming the SESSION_COOKIE_NAME which is session by default.
app.config.update(SESSION_COOKIE_NAME=<new_session_name>)
I found the solution via: https://stackoverflow.com/a/45497948/380038

Viewing Django and webpack built site on LAN

I am trying to view my locally served site on other devices such as my phone or another laptop. On my current laptop the site works fine, I see everything (frontend) and I get 200s when I visit the site.
However, when I try to access the site with my iphone and second laptop, I do not see any frontend but I do get 200s when I try to access the site. The terminal on my working machine also tells me that there are requests coming in.
I use Django as a backend and I bundle/build my Javascript and CSS with webpack and serve it with webpack-dev-server.
When I run webpack I see this message:
http://0.0.0.0:3000/
webpack result is served from http://localhost:3000/public/bundle/
content is served from ./public
When I load my webpage on my working machine, the developer tools shows this:
and everything is working.
I run django with this command.
$ ./manage.py runserver 0.0.0.0:8000
My ifconfig gives me:
inet 192.168.1.102
With my second laptop, I visit 192.168.1.102:8000 and I see nothing on my page. I get a 200 on my machine with the site working meaning the requests has gone through. On my second laptop I see this in the developer tools:
Notice it doesnt have /public/ in the src and href
On my second machine if I visit 192.168.1.102:3000 I see an Interface and I am able to click around to 192.168.1.102:3000/bundle/main.js and see my webpack built javascript.
On my second machine, I tried to change the src and href in my developer tools to 192.168.1.102:3000/bundle/main.js. However nothing would change and I still see a blank screen
Here is a gist of my webpack config:
https://gist.github.com/liondancer/7685b53dffa50d7d102d
I believe your page is empty because the whole "app" is generated by javascript (it seems so in your two screenshots at least, assuming that the content of <div id="app"></div> is not generated by a django view) but the javascript file is not accessible to clients that are different from your development machine.
It's hard to guess why this is happening without your settings.py, urls.py and the code/template of the view generating your home page but we have some candidates that may be generating this issues: CORS, localhost "poisoning" and eventually STATIC_URL misconfiguration.
CORS: A request is considered cross-domain if any of the scheme,
hostname, or port do not match. You are requesting file both from localhost:8000 (or 192.168.1.102:8000) and from localhost:3000. So CORS issues will rise if you request files from an external device/laptop;
localhost is the same machine
as 192.168.1.102 on your "working computer" but it isn't on your second
laptop or any other device in your network;
Do you generate the URLs for css and js files using {% url %} or {% static %} tags? It seems no, but still they look dynamically generated (i.e. the missing "public/" part of their URL). I'm not aware of a way to get different paths using vanilla Django and the same settings, so you should provide the source code of your view, at least, to get a precise answer.
Solutions (or, at least, hints :-) ):
Serve the bundle from the same port (add them to your STATIC path)
Replace every localhost reference in your html URLs (it may require to change your sites - see sites framework)
Use standard template tags/filters and avoid hard-coded URLs in templates and code.
or install https://github.com/owais/django-webpack-loader/ (pip install django-webpack-loader) and follow their README or http://owaislone.org/blog/webpack-plus-reactjs-and-django/ guide

Disable SSL for Heroku App (django)

We've decided not to use SSL anymore and unfortunately our server guy has quit and now I need to fix this. I've revoked the certs from Comodo, removed the SSL app from Heroku but that was apparently not enough and now we have serious problems with our site.
When visiting inteokej.nu one gets redirected to the app, but automatically http turns to https and instead of showing the domain (inteokej.nu) the app link is shown https://inteokej.herokuapp.com (I want inteokej.nu to be shown, not the actual app link).
That is a problem but not the biggest problem, which is that it's not possible to use the site anymore (e.g login, the static pages works though). When I try to login I first get a https security error and when I proceed I get to the following page: https://www.inteokej.nu/cgi-sys/defaultwebpage.cgi ("Sorry! If you are the owner of this website, please contact your hosting provider: webmaster#inteokej.nu").
I've now learned the hard way that SSL is a complex thing but I really need to get this site up again as soon as possible. So, where should I start and how could I proceed from this point? I guess there's some back end coding that should be done in the django code as well?
Thanks a lot in advance!
Your issue doesn't seem to be with SSL but DNS or at least however your server guy set things up.
The error page you're seeing isn't a Heroku error, inteokej.nu isn't being hosted on Heroku but on a server run by your DNS provider svenskadomaner.se .
If you use the Firefox Live HTTP Headers plugin you can follow the request/response cycle and you'll see that there is a 301 redirect from www.inteokej.nu to inteokej.herokuapp.com (probably an .htaccess redirect).
Check the DNS records for your domain (like here http://viewdns.info/dnsrecord/?domain=inteokej.nu ) you'll see that there is no CNAME record to Heroku, only an A Record to 46.22.116.5 which is an IP Address owned by svenskadomaner.se.
So the thing to do is to set up the custom domain as recommended on Heroku's site:
https://devcenter.heroku.com/articles/custom-domains
and set the CNAME to Heroku's recommendation.
One reason your server guy might have set things up like they did is that Heroku doesn't easily allow "naked domains", so people often do .htaccess redirects from example.com to www.example (which does work easily with CNAMEs).
Good luck!

Django language select on Heroku using Gunicorn doesn’t work

I’m running a Django app on Heroku and have installed this piece of code: https://github.com/st4lk/django-solid-i18n-urls
It works just as expected on the Django built-in server, but when deployed to Heroku, I’m starting to see some really nasty problems.
I have Gunicorn running with multiple workers and if I use this code, the language preference starts to work randomly (I guess it depends which instance the request goes against).
For example if I have EN as the default (non-prefixed url) and DE as the second language, everything is working fine if I just browse the / urls. Now, if I switch to /de/ (manually enter URL or switch from the Django language switcher), the main / url starts to work intermittently – if I keep refreshing the page, I get either a 404 or the page in DE. But there’s no way to switch it back to using EN completely again. Same happens with all the other URLs as well, I get either a 404 or the corresponding page in DE. And there’s no way to force it back to EN, even from the Django’s set_language view.
If I never touch the /de/ urls, it works all very well without the prefixed URLs.
Does anyone have any ideas how to get this working also on Heroku and Gunicorn?
It turns out Gunicorn and the middleware override works fine, problem was with another piece of custom middleware.

Categories

Resources