Skip to main content

Tanzania regions and districts package

Project description

Tanzania Locations 🗺️

A lightweight Python package providing Tanzania regions and districts for easy integration into Django forms, scripts, or any Python project. Perfect for building dynamic dependent dropdowns for location selection.

⚡ Installation

Install via PyPI:

pip install tanzania-locations

🐍 Quick Usage

from tanzania_locations import get_regions, get_districts

# List all regions
regions = get_regions()
print(regions)
# ['Arusha', 'Dar es Salaam', 'Dodoma', 'Mwanza', ...]

# List districts for a region
districts = get_districts("Dar es Salaam")
print(districts)
# ['Ilala', 'Kinondoni', 'Temeke', 'Ubungo', 'Kigamboni']

Django Integration

1️⃣ views.py

from django.shortcuts import render
from django.http import JsonResponse
from tanzania_locations import get_regions, get_districts

# Display the form with regions
def location_form(request):
    context = {
        # Convert list of regions to tuples for template
        "regions": [(r, r) for r in get_regions()]
    }
    return render(request, "app/location_form.html", context)

# Return districts as JSON for a given region
def load_districts(request):
    region = request.GET.get("region")
    districts_data = get_districts(region)
    districts = districts_data['districts']
    return JsonResponse({"districts": districts})

2️⃣ urls.py

from django.urls import path,include
from . import views

urlpatterns = [
    path("", views.location_form, name="location_form"),
    path("ajax/load-districts/", views.load_districts, name="load_districts"),
]

3️⃣ location_form.html

<form method="post">
    {% csrf_token %}
    <label for="region">Region</label><br>
    <select id="region" name="region">
        <option value="">-- Select Region --</option>
        {% for value, label in regions %}
            <option value="{{ value }}">{{ label }}</option>
        {% endfor %}
    </select>

    <br><br>

    <label for="district">District</label><br>
    <select id="district" name="district">
        <option value="">-- Select District --</option>
    </select>

    <br><br>
    <button type="submit">Submit</button>
</form>

<script>
document.addEventListener("DOMContentLoaded", function () {
    const regionSelect = document.getElementById("region");
    const districtSelect = document.getElementById("district");

    regionSelect.addEventListener("change", function () {
        const region = this.value;
        districtSelect.innerHTML = '<option value="">Loading...</option>';

        if (!region) {
            districtSelect.innerHTML = '<option value="">-- Select District --</option>';
            return;
        }

        fetch("{% url 'load_districts' %}?region=" + encodeURIComponent(region))
            .then(res => res.json())
            .then(data => {
                districtSelect.innerHTML = '<option value="">-- Select District --</option>';
                data.districts.forEach(d => {
                    const option = document.createElement("option");
                    option.value = d;
                    option.textContent = d;
                    districtSelect.appendChild(option);
                });
            })
            .catch(err => console.error(err));
    });
});
</script>

✅ Features

  • List all Tanzania regions
  • List districts by region
  • Works with Django forms, vanilla JS, or plain Python
  • Fully offline-ready
  • Can be extended for wards or other administrative levels

🔧 Advanced Usage

Preload all districts for no-AJAX forms:

from tanzania_locations import get_regions, get_districts

locations = {region: get_districts(region) for region in get_regions()}

Now your JS can populate district dropdowns instantly without any server requests.


📜 License

MIT License – free to use, modify, and distribute.


💡 Example Output

from tanzania_locations import get_regions, get_districts

for region in get_regions():
    print(region, get_districts(region))

This prints all regions and their districts, perfect for forms, scripts, or data analysis.


🤝 Contributing

Found a bug or have a feature request? Please keep in touch to: mtavangucornel@gmail.com

📞 Support

For help or questions:

  • Check the documentation above
  • Ensure you're using the latest version of the package

Happy coding with Tanzania Locations! 🇹🇿

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

tanzania_locations-1.5.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tanzania_locations-1.5.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file tanzania_locations-1.5.0.tar.gz.

File metadata

  • Download URL: tanzania_locations-1.5.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.8

File hashes

Hashes for tanzania_locations-1.5.0.tar.gz
Algorithm Hash digest
SHA256 ba009b7aa00285fdd41a40161de4dd9d3f62744701d2b3b14e2ac308bfaad265
MD5 e368fdf7f5546295aaa574637181ca9c
BLAKE2b-256 5d4cedbc699252bc0fdad15b918ec66b1179d164e265981c3bb2f61455684c16

See more details on using hashes here.

File details

Details for the file tanzania_locations-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tanzania_locations-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffe2cf5cc2b427d38e9c4e0f2311a147ee4097c30efd4d5713af11f5dd0c6545
MD5 9b8ea3ca5bb4f78d9e2e1131da3c9f3c
BLAKE2b-256 5b0c254cd6e3ca1baaf3e0f0396c020b3d0199cdd3aa1729dd0a8a45942b65aa

See more details on using hashes here.

Supported by

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