In this lab, you will create a web application using Django to deploy a machine learning model. You can use the Iris dataset classification model (same as used for Flask) or train and save another model of your choice.
- Python 3.7+ installed
- Basic knowledge of Python and machine learning
- Virtual environment setup knowledge
- Create a new project directory and navigate to it
- Create and activate a virtual environment
- Install required packages:
django,scikit-learn,pandas,numpy,joblib - Create a new Django project called
ml_project - Create a Django app called
predictor - Add your app to
INSTALLED_APPSin settings.py
Choose one of the following options:
Option A: Use Iris Classification Model (Recommended)
- Use the same Iris dataset classification model from the Flask lesson
Option B: Build Your Own Model
- Choose your own dataset and machine learning problem
- Train a classification or regression model of your choice
- Save your trained model as a
.pklfile in your project root
- Create views in
predictor/views.py:- A
homeview to display the input form - A
predictview to handle form submission and return predictions
- A
- Load your saved model in the views file
- Handle user input validation and error cases
- Create
predictor/urls.pywith URL patterns for your views - Include your app URLs in the main
ml_project/urls.py
- Create a
templates/predictor/directory structure - Create the following HTML templates:
base.html- Base template with navigation and Bootstrap CSShome.html- Form for user input (extends base template)result.html- Display prediction results (extends base template)
- Run Django migrations:
python manage.py migrate - Start the development server:
python manage.py runserver - Test your application in the browser
- Verify that:
- Input form displays correctly
- Model predictions work as expected
- Error handling works for invalid inputs
- Results are displayed properly
Your completed Django application should include:
- Working web interface for model predictions
- Proper error handling for invalid inputs
- Clean, responsive design using Bootstrap
- Proper project structure with templates and static files
- Screenshot of your Django Web application running on Local Host.
- Upon completion, add your deliverables to git.
- Then commit git and push your branch to the remote.
- Make a pull request and paste the PR link in the submission field in the Student Portal.
Good luck!
