No Logs in django developemnet server - python

i am trying to hit the django REST API using POSTMAN , i am able to save the data using django rest views, but when i am trying to save data using the POSTMAN the api is giving 500 , and also no logs are printed on console
it just print this
[09/Apr/2016 10:08:28]"POST /userprofile HTTP/1.1" 500 73961
the actual error is not clear why this happens, please help

Change the value of DEBUG variable in settings.py to True.
It give you all the informations you need in your browser if an error occurs (and in the console as well).

Related

AADSTS90102 error when using django-microsoft-auth for SSO with Azure AD

I followed the steps in this tutorial to enable SSO with Azure Active Directory for the admin portion (to start) of my Django app:
https://django-microsoft-auth.readthedocs.io/en/latest/usage.html
Navigating to /admin yields this page, which is good:
Clicking Microsoft brings up this new window:
The important error seems to be:
AADSTS90102: 'redirect_uri' value must be a valid absolute URI.
In this window, I used the browser console and found that a GET request was being made like this:
https://login.microsoftonline.com/50ce...90ac7/oauth2/v2.0/authorize?response_type=code&client_id=f4...27&redirect_uri=https,https://example.org/microsoft/auth-callback/&s...
Note the redirect_uri=https,https://.... It seems like that leading "https," is superfluous and is causing the problem. Any ideas where that could be coming from?
In my Azure app, the redirect URI is set to https://example.org/microsoft/auth-callback/:
I'm using Python 3.9.6, Django 3.2, django-microsoft-auth 2.4.0, NGINX 1.18.0, uvicorn 0.14.0
I've searched for help on this and haven't found anything relevant to my situation. Thanks in advance!
Based on the SO Thread Reference.
Use http as the redirect URI instead of https to resolve the issue in most cases.
use
http://localhost:8080/microsoft/auth-callback/
Instead of
https://localhost:8080/microsoft/auth-callback/
If there is a option,
Use localhost:8080 into the table django_site
Reference SO Thread: django-microsoft-auth : The provided value for the input parameter 'redirect_uri' is not valid
As you think, the first https is superfluous, you just need to delete it.
https://login.microsoftonline.com/50ce...90ac7/oauth2/v2.0/authorize?response_type=code&client_id=f4...27&redirect_uri=https://example.org/microsoft/auth-callback/&s...
By the way, I think there is no problem with the redirect_uri you set in the Azure portal.
I guess it is a problem of the redirecting URL. The example URL is coming from django site table. So first of all you need to enable the site:
#in settings.py
SITE_ID = 1
Afterwards you can go to the admin interface and set the url of the site to the correct domain. From my experience I know that it won't work without that.

Python Django /w Microsoft Graphs - I keep getting value error "state missing from auth_code_flow"

Python Django /w Microsoft Graphs -
I'm following this Microsoft Tutorial for building Django apps with Microsoft Graph (using it on my existing Django webapp), and I am having an issue with authentication: https://learn.microsoft.com/en-us/graph/tutorials/python
I'm on the step 'Add Azure AD authentication' and, after implementing,
I hit the sign in button and enter credentials...and I keep getting value error "state missing from auth_code_flow".
The "callback" method is only making it to result=get_token_from_code(request) and then fails.
Here is the get_token_from_code method:
def get_token_from_code(request):
cache = load_cache(request)
auth_app = get_msal_app(cache)
# Get the flow saved in session
flow = request.session.pop('auth_flow', {})
result = auth_app.acquire_token_by_auth_code_flow(flow, request.GET)
save_cache(request, cache)
return result
What I'm trying to do is eventually access excel online from my webapp.
Any help is greatly appreciated!
I just had this issue and resolved it. It is one of these two things:
You are starting out at 127.0.0.1:8000 and then when you're redirected you're at localhost:8000, which is a different domain. The sessions aren't remembered from one domain to the other. The solution is to start out on localhost:8000 so that the session persists across login.
Your browser is using super-strict cookie settings. Microsoft Edge appears to default to this mode on localhost and 127.0.0.1. There is a lock or shield icon in or near your address bar that lets you relax the restrictions on your cookie settings.
Try one or both of these and you should succeed.
I'm a beginner coder, so i'm pretty sure im just circumventing around the error. But replacing the website URL with http://localhost:8000/# and re running it somehow got around the error. maybe that could be of some use.
If you are running on chrome, rather than running application on http://127.0.0.1:8000 run it on http://localhost:8000, because chrome isn't saving the cookies when the ip address is being used.

Guincorn seems to be deleting Flask API data on each call

I am facing this issue where when I call an endpoint to my API (built in Flask, running on gunicorn with 9 workers), the data loads the first time, then, when I refresh the page, it seems to throw the follow error:
{'message': 'Internal Server Error'}
For some detail, the API does not store anything in persistent data, it is stored in runtime/ram before a specific endpoint is called (in my case, localhost:8000/see/), which then frees** the ram by deleting said data.
** I have disabled the function to free up stored data so no matter how many times I refresh the page, it should show the data.
So, I am facing two issues:
1: I have an endpoint localhost:8000/data/ where I store information being sent to the server. When I call this endpoint with that is inside of a dict, it should return all the data for that int. All data is shown at first, but when I refresh the page, it gives me the error I mentioned above. After a few refreshes of getting this error, the data shows up again but is gone on the next refresh.
2: When I call the /see/ endpoint, it should show me all the data I have collected, which it does but then when I refresh this page, it returns {} (disabled clearing, as mentioned above), which means the data is being cleared from runtime.
Furthermore, I have tried to fix this issue using the base Flask dev server, where I DO NOT encounter this error. I have also tried using another WSGI service: Waitress, which also does not give me these issues. I have tried running Gunicorn with supervisor and without, but I still am encountering these errors.
Any help would be appreciated as I would like to use Gunicorn. If there are good alternatives, please let me know.

Django rest framework, serializer invalid when sent through Angular

I'm having a problem where when I make a post request from my Angular application served through Django I get invalid serializers, even if the output is correct.
I console log the sent data:
{"email":"example#gmail.com","username":"example","password":"plaintextforthisexample","confirm_password":"plaintextforthisexample"}
Which gives me the response Bad Request (which I have set to happen when serializer.is_valid() is false).
However, if I copy and paste what's in the console log into the browsable API under raw data it accepts it and creates a user.
I can successfully send get-requests to the same API and get the data back, and the permissions for getting and posts are the same, although that would result in another type of error.
The content type is correct as well, when not I get a 415. If the URL is wrong I get a 500. And I can see on the Django server log that post requests are coming in.
Could it be because I'm running it from an angular template and not a Django one?
EDIT:
Picture of the console:
Issue 1: The console logged output that worked in Django was not the one actually being sent, resulting in a JSONParse error.
It looked like something like this
function(formobject){
let payload = JSON.stringify(formobject)
console.log(payload)
return formobject
Issue 2: Now Django gives my my custom response on arrival:
Status:'Bad Request'
Message:'Data does not fullfill requirements for creating a user'
And does not allow me to copy-paste the JSON-output from the console into the API raw data-form.
Solved by not using the same password I had been using for about 15 tries. Seems Django detects common passwords, has not been fully tested.

Django did not display DEBUG error page despite being run on localhost and ``DEBUG=True``

In my django project I did set DEBUG setting to true, and I'm accessing the server from localhost.
Despite that instead of error page I get: "A server error occurred. Please contact the administrator.".
In this case it was due to django raising another exception when displaying error page. There was an error in __str__ method of one of the variables django was trying to render on the error page. It turns out that if error page throws an exceptions Django falls back to page that just says: "A server error occurred. Please contact the administrator.".

Categories

Resources