如何在(Django GeoIP)中显示每个国家/地区的广告

时间:2013-04-30 09:32:29

标签: django views geoip

我正在尝试在每个国家/地区展示不同的广告,示例,西班牙显示Adsense和CO显示yieldmanager。

我的观点和模板:

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 %}

我做错的东西不起作用,我感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分: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.
    // ...
}