Receiving the application URL. Django - python

I need to get the current URL of my application in my views.py. Not the bookmark I am in but the address to the home page (all).
I finded this solution:
url = "{0}://{1}".format(request.scheme, request.get_host())
But i but I think it can be simpler . Not using 'request ....' twice and by obtaining an interpretation of one variable.
Any good suggestions will be appreciated.

url = request.build_absolute_uri("/")
See the Django documentation reference:
Returns the absolute URI form of location. If no location is provided, the location will be set to request.get_full_path().
If the location is already an absolute URI, it will not be altered. Otherwise the absolute URI is built using the server variables available in this request.
This seems to be what you are asking for.

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.

Flask doesn't match route

I have a route linked to a Flask-RESTful Resource:
api.add_resource(File, '/api/files/<int:id>')
If I now want to access that route via
/api/files/89?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc0FkbWluIjp0cnVlLCJ1c2VySWQiOjEsImV4cCI6MTQ3OTg0MTcxN30.DkoPeMeXms9j0nzmEAsGKOpIi_cRyTf4m6mQJjl17o0
i get:
"The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. You have requested this URI [/api/files/89] but did you mean /api/files/<int:id> or /api/files or /api/users/<int:id> ?"
If I try to simply:
/api/files/89
without the query parameter, everything works fine.
Where is the problem?
I solved the problem. It wasnt something related to the router, instead I messed up the view logic.

How to pass variables with google protocol buffers when using django? bitcoin bips in particular

Link to protofile here.
I need to pass a hash , a database ref, anything really so that I can know for sure that a customer visiting my payment request url came from my website and isn't some arbitrary bot pinging me and causing me to create new btc addresses for no reason.
I tried the following:
bitcoin:<non-bip-btcaddress>?r=http://127.0.0.1:8000/paymentobject/?ref=<unique-hash>
but it wasn't compatible with mobile wallets.
I'm now trying to pass a param inside urls.py
#urls.py
url(r'^paymentobject/(?P<ref>\w+)/$', 'project.views.paymentobject', name='paymentobject'),
#views.py
def paymentobject(request, ref):
secret = ref #and so on...
This approach works in the browser and I'm able to download the octet-stream file just fine, however when accessing the uri through bitcoin-qt (0.9x) or a mobile wallet I'm met with the error:
301: MOVED PERMANENTLY
Any advice would be greatly appreciated :)
Thanks
Problem was that the trailing slash was missing from the end of the url below.
btcbip_url = 'bitcoin:%s?r=%s/paymentobject/%s/'
Original anser here.

How to find out if an image is loaded?

I have a django project which is running (for example) on localhost:8000.
I have an image in this address localhost:8000/static/test.jpg . A user may open just this image by going to it's url and not open the containing page.
I want to find out if this image is loaded in a user's browser (by loading the containing page or just entering the image's url) and I want to get the request object of that request.
Can I have a method in my views just for that specific image? I searched through the internet but didn't find anything useful. any idea or solution?
Are you talking about disallowing hotlinking? This can be easier - and more effectively - done with the webserver that runs in front of your Django server.
For some examples for Apache check out https://wiki.apache.org/httpd/DisableImageHotLinking
This can be only by serving your images with your custom views. E.g you should write your own view that will return static resources, and you will not use a standard django static handler
First of all, please note that serving static files in a production environment should not be handled by Django in the first place. From contrib/staticfiles/views.py:
Views and functions for serving static files. These are only to be used during
development, and SHOULD NOT be used in a production setting.
If you do want to use this, then you could write a custom middleware hooking into process_view or process_request to do your stuff in.
I did it finally. for example i have a file in localjost:8000/media/1.jpg and i want to get ip of the user who enters this url to load the 1.jpg.
I added this line in my urls :
url(r'^media/(?P<path>.*)$', 'project.views.serve','document_root': settings.MEDIA_ROOT,}), and i set the MEDIA_ROOT already. then in project.views.serve i called django.views.static.serve and i returned the result of that as a HttpResponse. i have a request argument in my project.views.serve and i did this to get the user's ip from that :
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
print ip
Your way goes fine but bot for a high traffic. In that case you can use XSendFile library witch works with Apache

Is is possible to determine the local path from url?

I have access to apache web server. It is a web service application using Python.
I would like to access a file (100% sure reside in the local which is the web server itself) but I have only the url.
I am thinking of references any setting file of apache web server to determine the absolute local path of the file.
let say I have this kind of url: http://localhost/service/abc.jpg . So I can parse it to get the filename then obtain the decided local path from setting file.
But.. how about this kind of url: http://localhost/service/imagefile1 . In this case, I have only the url that the filename is not included in the url but still valid url. Is it possible to obtain the absolute local path of the file from url?
Any guidance is appreciated.
Is is possible to determine the local path from url?
The short answer: no.
Generally speaking, there is no concrete association between a URL and a file. In fact, there's nothing to say that the data at a given URL exists in a file on any server anywhere.

Categories

Resources