Skip to main content
rule

python-django-best-practices-cursorrules-prompt-fi — Cursor rule

Directs Cursor to generate idiomatic Django code with clean MVT architecture and optimized ORM queries.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/python-django-best-practices-cursorrules-prompt-fi.mdc
Open on GitHub

What python-django-best-practices-cursorrules-prompt-fi does

The rule instructs Cursor to enforce the Django MVT pattern. It directs the assistant to store domain logic inside models and forms rather than overloading views. Complex endpoints require class-based views, while simple endpoints stay as functions. Database reads rely on Django ORM with select_related and prefetch_related to avoid N+1 queries.

For APIs and background workloads, the guidelines point to Django REST Framework and Celery backed by Redis. Production storage targets PostgreSQL or MySQL. Automated tests use pytest-django or the built-in unittest framework.

Because alwaysApply is set to false, Cursor skips this instruction set unless the working context matches.

What it helps with

  • keep business logic inside models and forms instead of bloated views
  • prevent N+1 queries using select_related and prefetch_related
  • offload heavy tasks to Celery background workers with Redis
  • split project features into modular Django apps

Example prompts for the agent

  • Create a class-based view that preloads related profiles using select_related
  • Move this long report export process into an asynchronous Celery task
  • Implement form validation for our signup flow using Django model forms

How to install python-django-best-practices-cursorrules-prompt-fi

Manually: put the files in place

.cursor/rules/python-django-best-practices-cursorrules-prompt-fi.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about python-django-best-practices-cursorrules-prompt-fi

How to install python-django-best-practices-cursorrules-prompt-fi in Cursor?

Create .cursor/rules/python-django-best-practices-cursorrules-prompt-fi.mdc in your project root and add the rule content to it.

What does python-django-best-practices-cursorrules-prompt-fi do?

It provides Cursor with development standards for Django projects, emphasizing MVT separation, PEP 8 compliance, and query performance.

Which databases and tools are expected by this rule?

The configuration targets Django with Django REST Framework, Celery, Redis, and prefers PostgreSQL or MySQL in production.

Source text

python-django-best-practices-cursorrules-prompt-fi.mdc · CC0-1.0
---
description: "Cursor rules for Python Django development with best practices."
globs: **/*
alwaysApply: false
---
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.

© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules

Similar IDE rules

All IDE rules for Backend and APIs →

From the same set210

python-github-setup-cursorrules-prompt-file Tells Cursor how to write and review Python code: style, tests, GitHub Pull Requests and GitHub Actions CI. Useful when the team has no shared rulebook. ★ 41,024repo stars python-flask-json-guide-cursorrules-prompt-file Guides Cursor on using the drawscape_factorio module. Helps parse exported-entities.json and generate SVG blueprints. ★ 41,024repo stars python-fastapi-scalable-api-cursorrules-prompt-fil This rule sets coding and project-structure guidance for FastAPI and React apps. Use it when you want Cursor to follow shared conventions for implementation, tests, and deployment. ★ 41,024repo stars python-fastapi-best-practices-cursorrules-prompt-f A Cursor rule that sets the agent's coding style for Python and FastAPI: functional code, Pydantic v2, async operations. ★ 41,024repo stars python-developer-cursorrules-prompt-file This rule gives Cursor a Python-focused development context for debugging, command-line work, and file operations. Use it for Python project tasks. ★ 41,024repo stars python-cursorrules-prompt-file-best-practices A Cursor rule that makes the assistant write Python with type annotations, PEP 257 docstrings and pytest tests. ★ 41,024repo stars python-312-fastapi-best-practices-cursorrules-prom Guides Cursor to build FastAPI backends using Python 3.12, Poetry, and SQLAlchemy. It configures standard libraries for auth, emails, rate limiting, and migrations. ★ 41,024repo stars python--typescript-guide-cursorrules-prompt-file This rule guides Cursor on Python and TypeScript work with Django, React, Tailwind, and InertiaJS. It fits projects that need readable code and explicit file references. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor