A simple Django application that provides an API endpoint to get the current date and time in JSON format.
GET /api/current_datetime/
returns the current server date and time in JSON format.GET /api/
returns {message: Hello world}.
- Python 3.12
-
Create and activate a virtual environment:
python -m venv .venv source .venv/bin/activate # For Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install django
-
Run migrations:
python manage.py migrate
-
Start the server:
python manage.py runserver
Once the server is running, visit the following endpoint to get the current date and time:
GET http://127.0.0.1:8000/api/current_datetime/
The API responds with JSON in the following format:
{
"current_datetime": "YYYY-MM-DD HH:MM:SS"
}
datetime_api/
├── api/
│ ├── __init__.py
│ ├── views.py # Contains the current_datetime API view
│ ├── urls.py # URL configuration for the api app
├── datetime_api/
│ ├── __init__.py
│ ├── settings.py # Project settings
│ ├── urls.py # Main project URL configuration
│ ├── wsgi.py
├── manage.py
└── README.md # Project README
Docker image has been build and push to Docker Hub. Available at:
https://hub.docker.com/repository/docker/hammad2024/datetime-api/general