AI Personal Learning
and practical guidance

Configuring Django Programming Prompt Word Directives for Cursor

This guide is designed to help you leverage Python and Django for scalable web application development. Here are the key takeaways and best practices.

  1. Code style and structure
    • Follows PEP 8 specifications to keep code legible
    • Use descriptive variable and function names
    • Adopting a modular structure to promote code reuse with Django applications
  2. Django Core Concepts
    • Strict adherence to the MVT (Model-View-Template) pattern
    • Appropriate use of Class Views (CBVs) and Function Views (FBVs)
    • Leveraging Django ORM for Database Interaction
    • Using Django's Built-in User Model and Authentication Framework
  3. Forms and data processing
    • Handling Forms and Validation with Django's Forms and Model Forms Classes
    • Validating Form and Model Data with Django's Validation Framework
  4. error handling
    • Implementing Error Handling in the View Layer
    • Handling Exceptions in Business Logic and Views with try-except Blocks
    • Customize error pages to improve user experience
    • Separating Error Handling and Logging with Django Signals
  5. Dependency management
    Key dependencies include.

    • Django
    • Django REST Framework (API Development)
    • Celery (background tasks)
    • Redis (caching and task queues)
    • PostgreSQL or MySQL (production environment database)
  6. Templates and APIs
    • Rendering HTML with Django templates
    • Processing JSON Responses with DRF Serializers
    • Define clear RESTful URL patterns in urls.py
  7. Security Best Practices
    • Apply Django security best practices (CSRF protection, SQL injection protection, XSS prevention, etc.)
    • Using Django Middleware for Authentication, Logging, and Security Tasks
  8. performance optimization
    • Optimizing Query Performance with select_related and prefetch_related
    • Implementing the Django caching framework with a Redis or Memcached backend.
    • Implementing database indexing and query optimization
    • Use asynchronous views and background tasks (via Celery) for I/O-intensive or long-running operations
    • Optimize static file handling (using WhiteNoise or CDN integration)
  9. Testing and Quality Assurance
    • Ensure code quality and reliability using Django's built-in testing tools (unittest and pytest-django)
  10. Key principles
    • Following Django's "convention over configuration" principle
    • Prioritize security and performance optimization at every stage of development
    • Maintain a clear and logical project structure

Using these guidelines, you will be able to develop high quality, maintainable and scalable Django applications. Always refer to the official Django documentation for the latest best practices and detailed information.


 

 

Django

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.
2. Prioritize security and performance optimization in every stage of development. 3.
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.
AI Easy Learning

The layman's guide to getting started with AI

Help you learn how to utilize AI tools at a low cost and from a zero base.AI, like office software, is an essential skill for everyone. Mastering AI will give you an edge in your job search and half the effort in your future work and studies.

View Details>
May not be reproduced without permission:Chief AI Sharing Circle " Configuring Django Programming Prompt Word Directives for Cursor

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish