A simple and efficient URL shortening service built with Django and Django REST Framework. This service allows users to shorten long URLs, track the number of times they are accessed, and manage their URLs through a RESTful API.
- Backend: Django, Django REST Framework
- Database: PostgreSQL (recommended), SQLite (default)
- Cache/Broker: Redis
- Asynchronous Tasks: Celery
- Frontend: HTML, Tailwind CSS, HTMX
- Shorten long URLs
- Retrieve original URLs from shortened URLs
- Track the number of times a shortened URL has been accessed
- Create, retrieve, update, and delete shortened URLs via API
- User Authentication: Register, login, and logout functionality.
- User-Specific URLs: Shortened URLs can be associated with authenticated users.
- Permissions: Only the owner or a superuser can modify/delete a user's URL.
- Interactive Dashboard: Authenticated users can manage their URLs (delete, change password, regenerate token) directly from the homepage using HTMX.
- URL Expiration:
- Anonymous URLs expire and are deleted after a set time.
- User-owned URLs expire and are deactivated (marked inactive) after a set time.
- Background Tasks: Uses Celery for asynchronous operations like URL expiration.
- Customizable Forms: Styled Django forms with Tailwind CSS.
- Use Redis as cache for improved performance
-
Clone the repository:
git clone https://github.com/yourusername/urlshortener.git cd urlshortener -
Create a virtual environment and install dependencies (using
uv):uv venv source .venv/bin/activate # On Windows use `.venv\Scripts\activate` uv pip install -r requirements.txt
-
Configure environment variables: Create a
.envfile in the root of your project and add the necessary environment variables:DATABASE_URL=sqlite:///db.sqlite3 SECRET_KEY=your_django_secret_key REDIS_LOCATION=redis://127.0.0.1:6379/0 # For Celery broker and result backend, and Django cache
-
Apply migrations:
python manage.py makemigrations && python manage.py migrate -
Create a superuser (optional, but recommended for admin access):
python manage.py createsuperuser
-
Install Node.js dependencies for frontend:
npm install
-
Build Tailwind CSS:
npm run build:css
For development, you can use
npm run watch:cssto automatically recompile CSS on changes.
To run the full application, you need to start the Django development server, a Redis server, and Celery workers.
-
Start Redis Server: Ensure you have a Redis server running. If you have Docker, you can run:
docker run -d -p 6379:6379 redis/redis-stack-server:latest
-
Start Celery Worker: In a new terminal, from the project root:
celery -A urlshortener worker -l info
-
Start Celery Beat (for scheduled tasks like URL expiration): In another new terminal, from the project root:
celery -A urlshortener beat -l info
-
Run the Django Development Server: In your main terminal:
python manage.py runserver
Access the application at http://127.0.0.1:8000/.
- Homepage (
/): Serves as an interactive dashboard. Authenticated users can manage their URLs. Unauthenticated users can shorten URLs. - Register (
/register/): Create a new user account. - Login (
/login/): Log in to an existing account. - Logout (
/logout/): Log out of the current session. - Admin Panel (
/admin/): Access the Django administration interface. Users must haveis_staff=Trueto access. Superusers see all URLs; staff users see only their own.
- Create Short URL:
POST /shorten/ - List Short URLs:
GET /shorten/ - Retrieve/Update/Delete Short URL:
GET/PUT/PATCH/DELETE /shorten/<short_url>/ - Redirect to Original URL:
GET /<short_url>/
For more detailed information on specific components and configurations, refer to the docs/ directory:
docs/frontend.md: Frontend setup with Tailwind CSS and HTMX.docs/celery.md: Celery configuration and task management.docs/deployment.md: General deployment considerations.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License. See the [LICENSE](LICENSE](LICENSE) file for details.
This project was inspired and guided by the URL Shortening Service Project from roadmap.sh. Their comprehensive guide provided valuable insights and structure for developing this URL shortening service.