Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make a Country field in Django?
If you need to add a location field in your form or database, you can use a CharField but it's not the ideal solution. Django provides a third-party package called 'django-countries' that offers a dedicated country field with built-in validation and country list management. In this article, let's see how to use django-countries to add a Country field in Django.
Installation and Setup
First, create a Django project and an app. Add the app in INSTALLED_APPS and set up URLs.
Install the django-countries module ?
pip install django-countries
In settings.py, add this ?
INSTALLED_APPS += ['django_countries']
Creating the Model
In models.py ?
from django.db import models
from django_countries.fields import CountryField
# Create your models here.
class Data(models.Model):
name = models.CharField(max_length=100)
salary = models.CharField(max_length=20)
country_of_work = CountryField(blank=True)
Here we created a model with a country field that will store the country data with proper validation.
Setting Up URLs
In app's urls.py ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
Creating the View
In views.py ?
from django.shortcuts import render
from django import forms
from .models import Data
class SalaryForm(forms.ModelForm):
class Meta:
model = Data
fields = "__all__"
def home(request):
if request.method == 'POST':
form = SalaryForm(request.POST)
if form.is_valid():
form.save()
else:
form = SalaryForm()
return render(request, 'home.html', {'form': form})
Here we created a ModelForm and handle both GET and POST requests. In the POST handler, we save the form data after validation.
Creating the Template
Create a templates folder in app directory and a home.html in it ?
<!DOCTYPE html>
<html>
<head>
<title>Country Field Demo</title>
</head>
<body>
<h2>Employee Information Form</h2>
<form action="/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</body>
</html>
Key Features of CountryField
The CountryField provides several advantages:
- Validation − Automatically validates country codes
- Dropdown − Renders as a select dropdown with all countries
- Storage − Stores ISO 3166-1 alpha-2 country codes
- Display − Provides country name for display purposes
Final Steps
Now, make migrations and migrate ?
python manage.py makemigrations python manage.py migrate
You are all done. The country field will automatically render as a dropdown with all world countries.
Output
Conclusion
The django-countries package provides an elegant solution for handling country fields in Django. It offers built-in validation, proper storage using ISO country codes, and automatic rendering as a dropdown with all world countries.
