get ipv4 and ipv6 adr from session

This commit is contained in:
Fabian Faessler 2013-09-28 19:01:40 +02:00
parent bde430646a
commit 04602c53a6
4 changed files with 17 additions and 16 deletions

View File

@ -4,3 +4,11 @@ from django.conf import settings
def MyIpView(request): def MyIpView(request):
return HttpResponse(request.META['REMOTE_ADDR'], content_type="text/plain") return HttpResponse(request.META['REMOTE_ADDR'], content_type="text/plain")
def UpdateIpView(request):
ip = request.META['REMOTE_ADDR']
if ':' in ip:
request.session['ipv6'] = request.META['REMOTE_ADDR']
if '.' in ip:
request.session['ipv4'] = request.META['REMOTE_ADDR']
return HttpResponse('OK', content_type="text/plain")

View File

@ -6,19 +6,10 @@
<p> <p>
Ajax request to retrieve the ipv4/ipv6 remote address. The URL www.*.nsupdate.info is used for the call, so this will fail for the dev server. TODO: dev environment Ajax request to retrieve the ipv4/ipv6 remote address. The URL www.*.nsupdate.info is used for the call, so this will fail for the dev server. TODO: dev environment
</p> </p>
IPv4: <input type='text' class="form-control ipv4adr" placeholder="loading ip..."> </br > IPv4: <input type='text' class="form-control ipv4adr" placeholder="loading ip..." value="{{session.ipv4}}"> </br >
IPv6: <input type='text' class="form-control ipv6adr" placeholder="loading ip..."> IPv6: <input type='text' class="form-control ipv6adr" placeholder="loading ip..." value="{{session.ipv6}}">
<script> <img src="//{{ WWW_IPV4_HOST }}/updateip" />
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
$.get('//{{ WWW_IPV4_HOST }}/myip','',function(res){
alert('Your name is '+res);
});
$.get('//{{ WWW_IPV6_HOST }}/myip','',function(res){
alert('Your name is '+res);
});
</script>
{% endblock %} {% endblock %}

View File

@ -3,11 +3,12 @@ from main.views import (
HomeView, OverviewView HomeView, OverviewView
) )
from api.views import ( from api.views import (
MyIpView, MyIpView, UpdateIpView
) )
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"), url(r'^$', HomeView.as_view(), name="home"),
url(r'^overview/', OverviewView.as_view(), name="overview"), url(r'^overview/', OverviewView.as_view(), name="overview"),
url(r'^myip$', MyIpView), url(r'^myip$', MyIpView),
url(r'^updateip$', UpdateIpView),
) )

View File

@ -20,5 +20,6 @@ class OverviewView(TemplateView):
context['nav_overview'] = True context['nav_overview'] = True
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
context['session'] = self.request.session
return context return context