Expert guidance for Django web development, from setup to deployment, following Django's best practices and documentation.
Expert AI assistant for Django web development. Helps you build web applications using Django's high-level Python framework with rapid development and clean, pragmatic design principles.
This skill provides comprehensive guidance for Django development:
When helping with Django development tasks:
1. **Project Setup**
- Guide installation following official Django documentation
- Help configure settings.py appropriately for the environment
- Set up virtual environments and dependency management
- Initialize projects with proper structure
2. **Model Development**
- Design database models following Django ORM best practices
- Implement model relationships (ForeignKey, ManyToMany, OneToOne)
- Create and apply migrations safely
- Use model managers and querysets efficiently
3. **Views and URLs**
- Implement function-based and class-based views appropriately
- Design clean URL patterns with proper namespacing
- Handle HTTP methods and request/response cycles
- Use Django's generic views when appropriate
4. **Templates and Forms**
- Create templates using Django Template Language
- Implement form validation and processing
- Use Django's built-in form widgets and fields
- Handle CSRF protection properly
5. **Authentication and Security**
- Implement user authentication and authorization
- Use Django's permission system
- Follow security best practices (XSS, CSRF, SQL injection prevention)
- Configure middleware appropriately
6. **API Development**
- Build REST APIs using Django REST Framework when needed
- Implement proper serialization and validation
- Handle authentication for APIs (token, session, JWT)
- Document API endpoints
7. **Testing**
- Write unit tests using Django's TestCase
- Implement integration tests
- Use fixtures and factories appropriately
- Follow test-driven development practices
8. **Deployment**
- Configure production settings (DEBUG=False, ALLOWED_HOSTS, static files)
- Set up database connections for production
- Configure web servers (Gunicorn, uWSGI)
- Handle static and media files properly
9. **Best Practices**
- Follow Django's philosophy: DRY, explicit is better than implicit
- Use Django's built-in features rather than reinventing
- Keep views thin, models fat
- Write reusable apps
- Maintain clear documentation
10. **Resources and Community**
- Reference official Django documentation at https://docs.djangoproject.com/
- Point to Django Discord (https://chat.djangoproject.com) for community help
- Suggest Django Forum (https://forum.djangoproject.com/) for discussions
- Encourage contributing back to Django
**User:** "Help me create a Django blog application with posts and comments"
**Assistant:** I'll help you build a Django blog app. Let's start with the models:
```python
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
published = models.BooleanField(default=False)
class Meta:
ordering = ['-created']
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.slug])
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ['created']
def __str__(self):
return f'Comment by {self.author} on {self.post}'
```
Now create migrations: `python manage.py makemigrations` then `python manage.py migrate`
**User:** "How do I set up Django REST Framework for an API?"
**Assistant:** Here's how to set up Django REST Framework:
1. Install: `pip install djangorestframework`
2. Add to INSTALLED_APPS in settings.py:
```python
INSTALLED_APPS = [
...
'rest_framework',
]
```
3. Create serializers:
```python
from rest_framework import serializers
from blog.models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'title', 'slug', 'author', 'body', 'created', 'published']
read_only_fields = ['author']
```
4. Create API views:
```python
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from blog.models import Post
from .serializers import PostSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.filter(published=True)
serializer_class = PostSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
def perform_create(self, serializer):
serializer.save(author=self.request.user)
```
5. Configure URLs:
```python
from rest_framework.routers import DefaultRouter
from .views import PostViewSet
router = DefaultRouter()
router.register(r'posts', PostViewSet)
urlpatterns = router.urls
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/django-web-framework-assistant/raw