Skip to main content

A Django app to query the Swiss Post API for postcodes and localities.

Project description

city-ch-autocomplete is a Django helper app to add an autocomplete widget in an address form that searches postal codes and locality names through the post.ch API.

It depends on Bootstrap 5 to produce the autocomplete widget.

Quick start

  1. Add “city_ch_autocomplete” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...,
        "city_ch_autocomplete",
    ]
  2. Include city_ch_autocomplete urls to your urlpatterns:

    urlpatterns = [
        ...,
        path("", include("city_ch_autocomplete.urls")),
        ...,
    ]
  1. Get an API access from the Swiss Post (starting from https://developer.apis.post.ch/ui/home). Note the process can take some time. Then add the POST_API_USER and POST_API_PASSWORD settings to your project.

    Optionally you can get the data from a CSV and put it in a database table. Read below for more details.

  2. In the target form(s) of your project, add the CityChMixin to your form inheritance and a CityChField as a form field:

    from city_ch_autocomplete.forms import CityChField, CityChMixin
    
    class YourForm(CityChMixin, forms.ModelForm):
        class Meta:
            fields = [..., '<my_postcode_model_field>', '<my_city_model_field>', ...]
        city_auto = CityChField(...)
        postal_code_model_field = '<my_postcode_model_field>'
        city_model_field = '<my_city_model_field>'

    Don’t forget to include {{ form.media }} in the templates where you are using the form.

Searching from the database

If for some reason, you would prefer searching postcodes/names from a database table instead from the Swiss Post API, you can populate the PLZdb model with external data. The PLZdb.import_from_csv(csv_path) class method allows for importing such data.

A good CSV data source is https://www.swisstopo.admin.ch/fr/repertoire-officiel-des-localites (choose the csv ending with 2056.csv).

Then set the POST_API_USER setting to None and the view will search from the database instead.

Geographic city filter

If your database is spatially enabled, the PLZdb.objects manager allows for searching cities from within a distance around another city:

PLZdb.objects.filter_cities_around(<plz value>, <km>)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

city_ch_autocomplete-0.4.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

city_ch_autocomplete-0.4.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file city_ch_autocomplete-0.4.1.tar.gz.

File metadata

  • Download URL: city_ch_autocomplete-0.4.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for city_ch_autocomplete-0.4.1.tar.gz
Algorithm Hash digest
SHA256 3c419c1119dd803abda7c0f97a2e08aa616edf2562d3483ae28763af1686beeb
MD5 b1434a9e85aa31ae6f63c12902eb39b9
BLAKE2b-256 3bfb92636f90685db17267c2ac283bacb305efee4745decc5f2b48a1cb124554

See more details on using hashes here.

File details

Details for the file city_ch_autocomplete-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for city_ch_autocomplete-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 36aa708b3dda91bc5e9a8d1fac2d59231cce9f04f52ad72290214d16e2d18546
MD5 9a8f4889ab7125434cc4e5f9ca9f2cbd
BLAKE2b-256 f979a6b2e3cf97fae72cb9164da0ac3758d73a3e3652db359487b2a8df2cbde5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page