2013-04-30 100 views
1

我試圖在每個國家/地區展示不同的廣告,例如西班牙顯示Adsense和CO顯示yieldmanager。如何在(Django GeoIP)中顯示每個國家/地區的廣告

我的意見和模板:

from django.contrib.gis.geoip import GeoIP 
class VideoViewDetail(DetailView): 
    model = Video 

    def get_object(self): 
    return get_object_or_404(Video, slug__exact=self.kwargs['slug']) 

def get_context_data(self, **kwargs): 
    context = super(VideoViewDetail, self).get_context_data(**kwargs) 
      g = GeoIP() 
      ip = self.request.META.get('REMOTE_ADDR') 
      country = g.country('ip') 
      context['country_name'] = country 
     return context 

template 
{% for video in objectc_list %} 
    {{ video.name }} | {{ video.genre.name }} 
    {% if country_name == 'ES' %} 
     **code here ads** 
    {% else %} 
     {% if country_name == 'CO' %} 
      **code here ads** 
     {% else %} 
      {% if country_name == 'MX' %} 
       **code here ads** 
      {% else %} 
       ** nothing ** 
    {% endif %} 

東西我做錯了還是不行,我感謝所有幫助。

謝謝。

回答

0

與他們本地的PHP庫和GeoIP.dat數據集,這是它是如何做:

// Open GeoIP database and get the visitor's location. 
$geoIP = geoip_open("GeoIP.dat", GEOIP_STANDARD); 
$visitorLocation = geoip_record_by_addr($geoIP, $_SERVER["REMOTE_ADDR"]); 
$visitorCountry = $visitorLocation->country_code; 

// Decide whether to show AdSense or not. 
// In this example, we show it only to visitors from the U.S.A., Canada and the United Kingdom. 
if (in_array($visitorCountry, array("us", "ca", "gb"))) 
{ 
    // Show AdSense! 
    // ... 
} 
else 
{ 
    // Show some other ad we don't care about so much. 
    // ... 
} 
相關問題