Skip to main content

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>)

Metadata

Release files for city-ch-autocomplete 0.4.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for city-ch-autocomplete 0.4.9
File Size Uploaded
city_ch_autocomplete-0.4.9.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for city-ch-autocomplete 0.4.9
File Interpreter ABI Platform
city_ch_autocomplete-0.4.9-py3-none-any.whl Python 3 none any Details

Total release size: 29.7 kB

Release files / city_ch_autocomplete-0.4.9.tar.gz

Download URL city_ch_autocomplete-0.4.9.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
df48aca000d59eac405a507bf2f45509281c78a53645ffb1d1be493a91656fe0
BLAKE2b-256 checksum
How to use checksums
74b38fa1af2de4568b21913c223e464af353b91fa9877af1918d106c557576f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / city_ch_autocomplete-0.4.9-py3-none-any.whl

Download URL city_ch_autocomplete-0.4.9-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9a7dfe74b5996757605afd2c12fd7f99a25bc13ad6a2e206d8f361520be4c77
BLAKE2b-256 checksum
How to use checksums
464a81df930d3f06336a55410b97577692d7a0e37ab45dede62a59e49f803d4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.4.9 This release

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page