A simple Django-based URL shortener that allows users to shorten long URLs and redirect to the original links using the generated short codes.
- Shortens long URLs into a unique short code.
- Redirects users to the original URL when they visit the shortened URL.
- Generates random short codes for URLs.
- Stores URLs and short codes in a database (using SQLite or any other database supported by Django).
- Django: The web framework used for building the URL shortener.
- SQLite: Default database (you can switch to PostgreSQL, MySQL, etc.).
- Python: Programming language used to write the project.
Ensure that you have the following installed on your local development machine:
- Python 3.12
- Django
- Git
-
Clone the Repository
First, clone the repository to your local machine using Git:
git clone https://github.com/yourusername/url-shortener-django.git
-
Navigate to the Project Directory
Move into the project directory:
cd url-shortener-django -
Create and Activate Virtual Environment
Create a Python virtual environment and activate it:
python -m venv venv source venv/bin/activate # Mac/Linux venv\Scripts\activate # Windows
-
Install Dependencies
Install all the dependencies listed in the
requirements.txtfile:pip install -r requirements.txt
-
Apply Database Migrations
Set up the database by applying the migrations:
python manage.py migrate
-
Run the Development Server
Start the Django development server:
python manage.py runserver
-
Visit the Application
Open a browser and visit:
http://localhost:8000
- Enter a long URL in the provided form, and the app will generate a short code.
- You will be provided with a shortened URL, which you can share or use to redirect to the original URL.
To use the shortened URL, visit:
http://localhost:8000/<short_code>
This will redirect you to the original URL associated with the short code.
url_shortener/
├── manage.py
├── shortener/
│ ├── migrations/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── urls.py
│ ├── views.py
├── templates/
│ └── shortener/
│ ├── home.html
│ └── success.html
├── url_shortener/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── requirements.txt
└── README.md
- Allow users to create custom short codes.
- Implement user authentication to track personalized short links.
- Add analytics for the number of times a short URL has been visited.
This project is open-source and available under the MIT License.