High-level API usage

The app provides a convenient way to detect user location automatically. If you’ve followed advanced installation instructions, you can access user’s location in your request object:

def my_view(request):
    """ Passing location into template """
    ...
    context['location] = request.location
    ...

User location is an instance of a custom model that you’re required to create on your own (details below).

To avoid unnecessary database hits user location id is stored in a cookie.

Location model

Location model suites the basic needs for sites with different content for users, depending on their location. Ipgeobase forces Country-Region-City geo-hierarchy, but it’s usually too general and not sufficient. Site content might depend on city only, or vary on custom areas, combining various cities, that don’t match actual geographic regions.

In order to abstract geography from business logic, django-geoip requires a model, specific to your own app.

Creating custom location model

Create a model, that inherits from django_geoip.models.GeoLocationFascade. It should implement following classmethods:

class django_geoip.models.GeoLocationFascade(*args, **kwargs)

Interface for custom geographic models. Model represents a fascade pattern for concrete GeoIP models.

classmethod get_available_locations()

Return all locations available for users to select in frontend

Returns:GeoLocationFascade
classmethod get_by_ip_range(ip_range)

Return single model instance for given IP range. If no location matches the range, raises DoesNotExist exception.

Parameters:ip_range (IpRange) – User’s IpRange to search for.
Returns:GeoLocationFascade single object
classmethod get_default_location()

Return default location for cases where ip geolocation fails.

Returns:GeoLocationFascade

Switching region

Works very much like The set_language redirect view. Make sure you’ve included django_geoip.urls in your urlpatterns. Note that set_location view accepts only POST requests.

Project Versions

Table Of Contents

Previous topic

How it works

Next topic

Ipgeobase backend

This Page