Build complete web applications with Django, including REST APIs, admin panels, and template rendering.
# Django Python Full-Stack Development
Master full-stack development with Django using Google Antigravity IDE. This comprehensive guide covers project structure, models, views, templates, and API development.
## Why Django?
Django provides batteries-included full-stack development with excellent security. Google Antigravity IDE's Gemini 3 engine suggests optimal patterns and best practices.
## Project Structure
```
myproject/
├── manage.py
├── myproject/
│ ├── __init__.py
│ ├── settings/
│ │ ├── __init__.py
│ │ ├── base.py
│ │ ├── development.py
│ │ └── production.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py
├── apps/
│ ├── users/
│ │ ├── models.py
│ │ ├── views.py
│ │ ├── serializers.py
│ │ ├── urls.py
│ │ └── tests/
│ └── core/
│ ├── models.py
│ └── mixins.py
├── templates/
├── static/
└── requirements/
├── base.txt
├── development.txt
└── production.txt
```
## Model Design
```python
# apps/users/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from apps.core.models import TimeStampedModel
class User(AbstractUser, TimeStampedModel):
"""Custom user model with additional fields."""
email = models.EmailField(_("email address"), unique=True)
avatar = models.ImageField(upload_to="avatars/", blank=True)
bio = models.TextField(max_length=500, blank=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username"]
class Meta:
verbose_name = _("user")
verbose_name_plural = _("users")
ordering = ["-date_joined"]
def __str__(self):
return self.email
def get_full_name(self):
return f"{self.first_name} {self.last_name}".strip() or self.username
# apps/core/models.py
from django.db import models
import uuid
class TimeStampedModel(models.Model):
"""Abstract base model with timestamp fields."""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class SoftDeleteModel(models.Model):
"""Abstract model for soft deletion."""
is_deleted = models.BooleanField(default=False)
deleted_at = models.DateTimeField(null=True, blank=True)
class Meta:
abstract = True
def delete(self, *args, **kwargs):
self.is_deleted = True
self.deleted_at = timezone.now()
self.save()
def hard_delete(self):
super().delete()
```
## Class-Based Views
```python
# apps/users/views.py
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.db.models import Q
from .models import User
from .forms import UserUpdateForm
class UserListView(LoginRequiredMixin, ListView):
"""List all users with search and pagination."""
model = User
template_name = "users/user_list.html"
context_object_name = "users"
paginate_by = 20
def get_queryset(self):
queryset = super().get_queryset()
search = self.request.GET.get("search")
if search:
queryset = queryset.filter(
Q(username__icontains=search) |
Q(email__icontains=search) |
Q(first_name__icontains=search)
)
return queryset.select_related().order_by("-date_joined")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["search"] = self.request.GET.get("search", "")
return context
class UserDetailView(LoginRequiredMixin, DetailView):
"""Display user profile."""
model = User
template_name = "users/user_detail.html"
context_object_name = "user"
slug_field = "username"
slug_url_kwarg = "username"
class UserUpdateView(LoginRequiredMixin, UpdateView):
"""Update current user profile."""
model = User
form_class = UserUpdateForm
template_name = "users/user_form.html"
def get_object(self):
return self.request.user
def get_success_url(self):
return reverse_lazy("users:detail", kwargs={"username": self.object.username})
```
## Django REST Framework API
```python
# apps/users/serializers.py
from rest_framework import serializers
from django.contrib.auth.password_validation import validate_password
from .models import User
class UserSerializer(serializers.ModelSerializer):
"""User serializer for read operations."""
class Meta:
model = User
fields = ["id", "email", "username", "first_name", "last_name", "avatar", "bio", "created_at"]
read_only_fields = ["id", "created_at"]
class UserCreateSerializer(serializers.ModelSerializer):
"""Serializer for user registration."""
password = serializers.CharField(write_only=True, validators=[validate_password])
password_confirm = serializers.CharField(write_only=True)
class Meta:
model = User
fields = ["email", "username", "password", "password_confirm"]
def validate(self, attrs):
if attrs["password"] != attrs["password_confirm"]:
raise serializers.ValidationError({"password": "Passwords don't match"})
return attrs
def create(self, validated_data):
validated_data.pop("password_confirm")
return User.objects.create_user(**validated_data)
# apps/users/views.py
from rest_framework import viewsets, permissions, status
from rest_framework.decorators import action
from rest_framework.response import Response
class UserViewSet(viewsets.ModelViewSet):
"""API viewset for users."""
queryset = User.objects.filter(is_active=True)
permission_classes = [permissions.IsAuthenticated]
def get_serializer_class(self):
if self.action == "create":
return UserCreateSerializer
return UserSerializer
@action(detail=False, methods=["get", "patch"])
def me(self, request):
"""Get or update current user."""
if request.method == "GET":
serializer = UserSerializer(request.user)
return Response(serializer.data)
serializer = UserSerializer(request.user, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
```
## Best Practices
- Use custom user model from the start
- Apply class-based views for consistency
- Implement proper model managers
- Use Django REST Framework for APIs
- Apply database indexing for performance
- Write comprehensive tests
Google Antigravity IDE provides Django patterns and automatically suggests security improvements for your full-stack applications.This Python prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their python implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Python projects, consider mentioning your framework version, coding style, and any specific libraries you're using.