1/23/2025
Best practices and guidelines for integrating HTMX with Django to build dynamic web applications efficiently.
# HTMX (Django)
# HTMX with Django .cursorrules
## HTMX and Django Best Practices
```javascript
const htmxDjangoBestPractices = [
"Use Django's template system with HTMX attributes",
"Implement Django forms for form handling",
"Utilize Django's URL routing system",
"Use Django's class-based views for HTMX responses",
"Implement Django ORM for database operations",
"Utilize Django's middleware for request/response processing",
];
```
## Folder Structure
```javascript
const folderStructure = `
project_name/
app_name/
templates/
static/
css/
js/
models.py
views.py
urls.py
project_name/
settings.py
urls.py
manage.py
`;
```
## Additional Instructions
```javascript
const additionalInstructions = `
1. Use Django's template tags with HTMX attributes
2. Implement proper CSRF protection with Django's built-in features
3. Utilize Django's HttpResponse for HTMX-specific responses
4. Use Django's form validation for HTMX requests
5. Implement proper error handling and logging
6. Follow Django's best practices for project structure
7. Use Django's staticfiles app for managing static assets
`;
```
### Detailed Guidelines
1. **Template System**: Leverage Django's template system to embed HTMX attributes directly within your HTML templates. This allows for seamless integration of dynamic content without extensive JavaScript.
2. **Form Handling**: Utilize Django's form handling capabilities to manage form submissions. This includes validation, error handling, and CSRF protection, ensuring secure and robust form processing.
3. **URL Routing**: Make full use of Django's URL routing system to define clear and maintainable URL patterns. This helps in organizing your application's endpoints and makes it easier to manage HTMX requests.
4. **Class-Based Views**: Implement class-based views in Django to handle HTMX responses efficiently. This approach promotes code reuse and simplifies the management of different HTTP methods.
5. **ORM Usage**: Use Django's ORM for all database operations. This ensures that your database interactions are secure, efficient, and consistent with Django's best practices.
6. **Middleware**: Utilize Django's middleware for processing requests and responses. This is particularly useful for adding custom logic, such as logging or authentication, that needs to be applied globally.
7. **Static Assets**: Manage static assets using Django's staticfiles app. This ensures that your CSS, JavaScript, and other static files are served efficiently and are easily maintainable.
8. **Error Handling**: Implement comprehensive error handling and logging to ensure that your application can gracefully handle unexpected issues and provide meaningful feedback to users.
By following these guidelines, you can effectively integrate HTMX with Django to build dynamic, responsive, and maintainable web applications.