Tanzania regions and districts package
Project description
# Tanzania Locations 🗺️
[](https://pypi.org/project/tanzania-locations/)
[](https://pypi.org/project/tanzania-locations/)
[](LICENSE)
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:
```bash
pip install tanzania-locations
```
> For testing with TestPyPI:
```bash
pip install --index-url https://test.pypi.org/simple/ tanzania-locations
```
---
## 🐍 Quick Usage
```python
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`
```python
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`
```python
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`
```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**:
```python
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
```python
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**.
```
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tanzania_locations-1.3.0.tar.gz.
File metadata
- Download URL: tanzania_locations-1.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14d6468a48752a9c5e2c0ea429346deb7dd39be6e77088bca8cb0c2749528298
|
|
| MD5 |
c8654c9c7631389fc521d35a906cfeda
|
|
| BLAKE2b-256 |
03624a478b09d6a27d4046c496a284de970bd55a47529354cc5a97b9526ad1c2
|
File details
Details for the file tanzania_locations-1.3.0-py3-none-any.whl.
File metadata
- Download URL: tanzania_locations-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb08842088234e5d076a27c9cb8e4ba27f549ac7ea2256bf9ed8f5ca37304093
|
|
| MD5 |
3fbfb5946d2e630e4ad6443dcf12f7ab
|
|
| BLAKE2b-256 |
84c849d75324df75b30a7c5a6d7c8a834127bd9d11ba21876ba8c66e5021e795
|