1

各国で異なる広告を表示しようとしています。たとえば、スペインは 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 %}

私がうまくいかない何かが間違っています。助けていただければ幸いです。

ありがとう。

4

1 に答える 1

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.
    // ...
}
于 2014-01-18T14:18:27.420 に答える