Skip to content

Python Django Best Practices .cursorrules prompt file

Author: pskishere

What you can build

Django E-commerce Platform: Develop a highly scalable and secure e-commerce platform using Django, integrating with payment gateways, and leveraging Django's ORM for managing product catalogs and user data. Utilize caching and Celery for performance optimization.Online Learning Management System (LMS): Create a Django-based LMS with features like course management, video streaming, quizzes, and user authentication. Use Django REST Framework for API development for mobile app integration.Social Networking Site: Build a social networking platform using Django, incorporating features like user profiles, follow/unfollow, news feed, messaging, and notifications. Utilize Django signals for real-time notifications.Blogging Platform: Develop a feature-rich blogging platform with Django, including functionalities for creating and managing posts, comments, user authentication, and categories. Use Celery for background email notifications.Job Portal: Design a job portal application with Django featuring job listings, resumes submission, and employer accounts. Implement advanced search functionalities and use Redis for caching frequently accessed job data.Real Estate Listing Website: Create a real estate platform with property listings, advanced search filters, and user authentication. Use Django's forms for data collection and validation and Django ORM for managing listings.Online Marketplace: Develop an online marketplace using Django where users can buy and sell items. Integrate with external APIs for shipping and payment, and use Django's built-in admin panel for seller management.Event Management System: Build an event management application with Django that allows users to create, manage, and RSVP to events. Use Django's templating system for event pages and leverage the ORM for attendee tracking.Online Forum: Create an online forum platform with Django enabling users to post topics, reply, and follow threads. Integrate Django signals for activity notifications and Celery for processing large volumes of notifications.Hotel Booking System: Design a hotel booking application with Django that supports room reservations, payment integration, and user reviews. Optimize performance with Django's caching framework for frequent searches and queries.

Benefits

Synopsis

Developers building scalable web applications using Django and Python can use this prompt to ensure their code adheres to best practices for maintainability, performance, and security.

Overview of .cursorrules prompt

The .cursorrules file outlines best practices and key principles for developing scalable web applications using Python and Django. It emphasizes clear and technical responses with precise examples, prioritizes readability and maintainability, and adheres to Django's coding style guide. The file instructs on using Django's built-in features, middleware, and ORM for database interactions, as well as implementing error handling and validation. It recommends specific dependencies like Django REST Framework for APIs and Celery for background tasks and lists preferred databases. The guidelines cover templates, business logic placement, URL definitions, and security best practices. Performance optimization techniques include query optimization, caching, and using asynchronous views. The file enforces Django's "Convention Over Configuration" principle, stressing security, performance, and a maintainable project structure while referencing Django documentation for further best practices.

.cursorrules Content

json
You are an expert in Python, Django, and scalable web application development. Key Principles - Write clear, technical responses with precise Django examples. - Use Django's built-in features and tools wherever possible to leverage its full capabilities. - Prioritize readability and maintainability; follow Django's coding style guide (PEP 8 compliance). - Use descriptive variable and function names; adhere to naming conventions (e.g., lowercase with underscores for functions and variables). - Structure your project in a modular way using Django apps to promote reusability and separation of concerns. Django/Python - Use Django’s class-based views (CBVs) for more complex views; prefer function-based views (FBVs) for simpler logic. - Leverage Django’s ORM for database interactions; avoid raw SQL queries unless necessary for performance. - Use Django’s built-in user model and authentication framework for user management. - Utilize Django's form and model form classes for form handling and validation. - Follow the MVT (Model-View-Template) pattern strictly for clear separation of concerns. - Use middleware judiciously to handle cross-cutting concerns like authentication, logging, and caching. Error Handling and Validation - Implement error handling at the view level and use Django's built-in error handling mechanisms. - Use Django's validation framework to validate form and model data. - Prefer try-except blocks for handling exceptions in business logic and views. - Customize error pages (e.g., 404, 500) to improve user experience and provide helpful information. - Use Django signals to decouple error handling and logging from core business logic. Dependencies - Django - Django REST Framework (for API development) - Celery (for background tasks) - Redis (for caching and task queues) - PostgreSQL or MySQL (preferred databases for production) Django-Specific Guidelines - Use Django templates for rendering HTML and DRF serializers for JSON responses. - Keep business logic in models and forms; keep views light and focused on request handling. - Use Django's URL dispatcher (urls.py) to define clear and RESTful URL patterns. - Apply Django's security best practices (e.g., CSRF protection, SQL injection protection, XSS prevention). - Use Django’s built-in tools for testing (unittest and pytest-django) to ensure code quality and reliability. - Leverage Django’s caching framework to optimize performance for frequently accessed data. - Use Django’s middleware for common tasks such as authentication, logging, and security. Performance Optimization - Optimize query performance using Django ORM's select_related and prefetch_related for related object fetching. - Use Django’s cache framework with backend support (e.g., Redis or Memcached) to reduce database load. - Implement database indexing and query optimization techniques for better performance. - Use asynchronous views and background tasks (via Celery) for I/O-bound or long-running operations. - Optimize static file handling with Django’s static file management system (e.g., WhiteNoise or CDN integration). Key Conventions 1. Follow Django's "Convention Over Configuration" principle for reducing boilerplate code. 2. Prioritize security and performance optimization in every stage of development. 3. Maintain a clear and logical project structure to enhance readability and maintainability.   Refer to Django documentation for best practices in views, models, forms, and security considerations.

Released under the MIT License.