Cursor Rules Generator for Python + Django
ide
Cursor
.cursorrules
scaffolding
strict_senior
Generate a production-ready .cursorrules file for Python Django projects with comprehensive best practices.
By david_kim
12/8/2025
Prompt
Generate a production-ready .cursorrules configuration file for a Python Django project with the following specifications:
Project Details
- Python Version: 3.11+
- Django Version: [Specify version, e.g., 4.2 or 5.0]
- Project Type: [web application / REST API / full-stack app]
- Database: [PostgreSQL / MySQL / SQLite]
- Additional Tools: Black (formatting), mypy (type checking), pytest-django (testing)
Code Standards to Enforce
Python Requirements
- Use type hints following PEP 484 for all function signatures
- Follow PEP 8 style guide strictly
- Use Black for code formatting (line length: 88 characters)
- Enable mypy strict mode for type checking
- Python 3.11+ syntax and features
Django Model Patterns
- Create abstract base models for common fields (created_at, updated_at, etc.)
- Always implement
__str__()and__repr__()methods - Include
Metaclass withorderingandverbose_name - Use descriptive
related_namefor all ForeignKey and ManyToMany relationships - Add database indexes on frequently queried fields
- Use
choiceswith TextChoices or IntegerChoices enums
Django View Patterns
- Prefer class-based views over function-based views
- Use mixins for reusable view logic (e.g., LoginRequiredMixin, PermissionRequiredMixin)
- Leverage generic views (ListView, DetailView, CreateView, etc.) when applicable
- Always implement proper permission checks
- Use
get_queryset()method to customize query logic - Return proper HTTP status codes
URL Configuration
- Use descriptive URL names for all routes
- Use
path()instead of deprecatedurl() - Include app namespaces for all app-level URLs
- Follow RESTful naming conventions for API endpoints
- Group related URLs logically
File Structure
Generate rules for this project structure:
myproject/
├── apps/
│ ├── users/
│ ├── core/
│ └── api/
├── config/
│ ├── settings/
│ │ ├── base.py
│ │ ├── development.py
│ │ └── production.py
│ └── urls.py
├── templates/
└── static/
QuerySet Optimization
- Always use
select_related()for forward ForeignKey relationships - Always use
prefetch_related()for reverse ForeignKey and ManyToMany relationships - Prevent N+1 query problems
- Use
only()anddefer()strategically to load only needed fields - Add
.count()instead oflen()on querysets - Use
exists()for boolean checks instead of counting
Security Requirements
- Never commit SECRET_KEY or sensitive credentials
- Use environment variables for all configuration
- Ensure CSRF protection is enabled
- Use Django ORM to prevent SQL injection
- Enable XSS protection in templates (autoescaping on)
- Validate and sanitize all user input
- Use Django's built-in password hashers
- Implement rate limiting for authentication endpoints
Testing Standards
- Write unit tests for all views and business logic
- Test all model methods and properties
- Use
pytest-djangoas the testing framework - Use Factory Boy for test data generation
- Aim for 80%+ code coverage
- Test both success and error cases
- Mock external API calls
Performance Optimization
- Add database indexes on fields used in filters and ordering
- Implement query optimization with select_related/prefetch_related
- Use caching strategy with Redis for expensive queries
- Use async views for I/O-bound operations (Django 4.1+)
- Paginate large querysets
- Use database-level constraints when possible
API Development (if applicable)
- Use Django REST Framework for API endpoints
- Implement proper serializer validation
- Use ViewSets and Routers for standard CRUD
- Add pagination to list endpoints
- Implement proper authentication (JWT, Session, or Token)
- Use throttling to prevent abuse
- Version your API (e.g., /api/v1/)
Additional Requirements
Include rules for:
- Import ordering (standard library, third-party, local)
- Docstring style (Google or NumPy format)
- Error handling patterns
- Logging configuration
- Environment-specific settings management
Generate the complete .cursorrules file that will guide Cursor IDE to follow these Django best practices consistently.
Tags
cursor
python
django
backend
Tested Models
gpt-4
claude-3-opus