Obtaining the user IP-addresses from the request on the Django can be useful for the organization of a functional site, such as the hosts lock, from which users are trying to make password brute force to the site or if you need to remember user IP, to provide access to the site for a particular user with only certain IP address.
The IP address can be either real or transmitted through a proxy server (in this case will be transferred HTTP_X_FORWARDED_FOR header which you can extract the real IP address).
def get_client_ip(request): 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') return ip
Getting the URL as the previous, which was the user can be useful if you need to make the redirect of the user to the page, from that the user came after the authorization.
For example, this site has a login widget on every page. To authorize a widget is sent the POST request to the URL authorization, but then you must return the user to the page from which he came, so the user can continue to work with the article or forum. You can add a widget to a special field, which will contain the current URL and sends it to the POST request. Initially, and so it was done, but it complicates the code. And you can use the referral removing the URL, from which the user came, as is done now on this site.
from django.utils.http import is_safe_url, urlunquote def get_next_url(request): next = request.META.get('HTTP_REFERER') if next: next = urlunquote(next) # HTTP_REFERER may be encoded. if not is_safe_url(url=next, host=request.get_host()): next = '/' return next
With this function, you can get the previous URL in any application and do not write special code to individual referrals for each widget or form, with which the user is working.
For Django I recommend VDS-server of Timeweb hoster .