Bad request (400) on Django Localhost - python

I have the project successfully deployed through AWS. However, I would like to make some changes and experiment on them using localhost:8000/ first before making the changes to AWS server. I set my settings.py to: ALLOWED_HOSTS = ['IP address', 'www.website.com'] to deploy it. Though, I also want to be able to run the server in my local computer so I can experiment it before showing to public. The url is (r^homepage$') When I go to localhost:8000/homepage, it gives a Bad Request (400). I tried many permutations of the urls but nothing works.

you can set Debug=True to see the error message.

Related

Django print statements/debug on hosted app pythonanywhere

I have a Django app working fine on the local server but when I host it on the pythonanywhere (It is a cloud-based python site), there are some issues with the rendering on the template.
While I was coding on the local server, I used to use print statements to debug the functions in views.py, but how to use print statements for debugging if the site is deployed on the web because after hosting it on the pythonanywhere, I am unable to find where the output of the print statements goes to?
Please suggest another way, if this is not feasible, to get the backend output after deploying the Django web app.
Thanks for the help.
To do so you have to go on:
1)your web app: you have to replace yourusername by your real username in pythonanywhere.com
https://www.pythonanywhere.com/user/yourusername/webapps/#tab_id_yourusername_pythonanywhere_com
2) scroll to bottom where you can see : Log files and in this section your will find a server log click on that and you will be redirected to an other page where you can scroll to bottom from there you can see the print from your views.
i hope you understand and sorry for my poor english.
Short Solution
visit the server log file here
https://www.pythonanywhere.com/user/yourusername/files/var/log/yourusername.pythonanywhere.com.server.log replace yourusername by your real username in pythonanywhere

How can I handle subdomains with Flask, Cloudflare and Heroku?

I bought a domain, let's say its "mydomain.org".
I connected that domain to Cloudflare and Heroku, and I can access my web app(site) on mydomain.org, handle the flask's route('/') requests.
But when I use, for example, route('/', subdomain="test") and try to return "This is a subdomain", it does nothing, or if it does, it redirects me to the simple route('/') (so basically without the subdomain).
I registered a subdomain on Heroku and created an alias for test.mydomain.org to point to the DNS that Heroku gave me, but I still get redirected to the main page, instead of getting simply the "This is a subdomain" message.
How could I make it work?

Google App Engine Produces 502 Error on Python/Flask API

I've been all over google, and stack and read EVERY article I could find but have not been able to solve me issue. I recently wanted to take my project that is still in development and get it up on GAE so I could test it and make sure it would work as I assumed. After some trail and error I was able to get a Postgres database setup and push my project using
gcloud app deploy app.yaml
My Flask app is using Swagger and Flask-Restplus for the API. I can access the swagger page and perform some requests, however all of those requests come back with
502 Bad Gatway
While reading the live log in the console I can see the data is coming in as I would expect. There are no exceptions, and the GAE error console does not even log the 502 error. In the console I can also see..
[CRITICAL] WORKER TIMEOUT (pid:17)
So I went into the logs and see this NGINX error..
textPayload: "[error] 32#32: *4086 upstream prematurely closed connection while reading response header from upstream, client: 130.211.2.231, server: , request: "POST /auth/login_user HTTP/1.1", upstream: "http://172.17.0.1:8080/auth/login_user", host: "blah-191811.appspot.com", referrer: "http://blah-191811.appspot.com/""
I don't think it is an error connecting to my database because I can connect to the Postgres DB hosted on GAE from my local computer. I also have another API endpoint that just uses a google API to retrieve some info and that also produces a 502 error.
I am at my wits end, and honestly about to throw in the towel and try to get it going on AWS if I can't figure this out. Any help would be much appreciated.
EDIT WITH MORE INFO:
The problem still persists. However, I have two routes on my API that require a valid JWT header. When using these routes with an invalid header token, or no token the API returns the correct response, a 401. When sending the correct token the API again returns a 502. This makes me believe that there is nothing wrong with my code, but that somewhere the response is not getting sent back.
I should add that these are not data intensive calls. The login_user is just two strings, an email and a password. The database has only one entry in it.
When I run locally but CONNECT to the remote Postgres database the API works as expected. I.e if I run a Flask server locally and do; 127.0.0.1:5000/auth/login_user and send the correct information, it is able to read back from my Postgres database on my GAE project. So I don't believe the database is the issue either.
Still looking for any wisdom because this seems to be a very common issue with little resolve.
For anyone else experiencing this issue..I found the solution after much digging. It was in the way I was connecting to the potgres SQL instance on GAE.
I was using this;
SQLALCHEMY_DATABASE_URI = 'postgresql://user:password#remote.ip.addr/database_name'
This code works fine when you are accessing the SQL instance from a whitelisted IP. However this code will not work inside an instance that also runs on GAE. You need to change it to this;
SQLALCHEMY_DATABASE_URI = 'postgresql://user:password#/database_name?
host=/cloudsql/database_instance_id'

How to test Facebook authentication using python social auth in localhost? Edited with more details

The question explains mostly everything. I am writing a Django app which will use Facebook authentication through Python social auth. I created an app using Facebook developers. The thing is, I don't have the domain set up with my website as I am still testing on localhost. I could not put in the localhost URL in the Facebook app settings. When I try to login through localhost, it says the URL must match with the domain.
What to do now? Without testing, I cannot host my website either as I am not sure if everything will work like i want it to.
EDIT:
I created a test app and was able to put localhost:8000 as the site url and localhost as app domain. However, the same problem still exists. It says that the 2 urls has to match or something. NOTE: In my local host, the login is 127.0.0.1:8000/login/facebook. And yes, I did update the ID and the app secret for my test app.
What to do now?
You should be able to create a test app within Facebook (There is a 'Create Test App') menu option. It may be that you need to first create an App, and once you have it, you will be able to create the Test App for it, but for sure you can create a Test App.
On that test app, use http://localhost:8000/ as your local URL. Just make sure it is the exact same URL (i.e. you cannot have 'localhost' on Facebook, but then start the server for '127.0.0.1'). Note that the AppID and AppSecret you will use is of the Test App (not the production app).
This should work. I have not used this for python-social-auth but use this to test with django-allauth, and there is no reason why it will be any different.

Flask Session will not Persist

I have recently deployed my first Flask application (first web application ever actually), one problem I am running into and haven't had luck tracking down is related to sessions.
What I am doing is when the user logs in I set session['user'] = user_id and what is happening is I occasionally get a key error when making a request involving that session key. If I try to make the request again the session key is there and the request works fine. I have done research and set the app.config['SERVER_NAME'] to my domain and made sure the secret_key was static, it was dynamic before.
This does not happen when on my local development server so I am a bit stumped at this point.
Problem was that I had the key static in my init which caused it to work in dev but in production in the .wsgi it was still dynmaic, I have changed this and all seems to be working now.

Categories