BackendGitHub source
Django view generator
/viewsCreate Django views following a proper structure and best practices.
// skill content
Django Views Generator Create Django views with proper structure and best practices. ## Purpose This command helps you quickly create Django views (Function-Based Views and Class-Based Views) following Django conventions. ## Usage `` /views ` ## What this command does 1. **Creates view functions/classes** with proper structure 2. **Handles HTTP methods** (GET, POST, PUT, DELETE) 3. **Includes form handling** and validation 4. **Adds authentication/authorization** checks 5. **Follows Django best practices** and security guidelines ## Example Output ``python # views.py from django.shortcuts import render, getobjector404, redirect from django.contrib.auth.decorators import loginrequired from django.contrib import messages from django.http import JsonResponse from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverselazy from .models import Post, Category from .forms import PostForm # Function-Based Views def postlist(request): """Display list of posts with pagination and filtering.""" posts = Post.objects.filter(status='published').selectrelated('author', 'category') # Search functionality searchquery = request.GET.get('search') if searchquery: posts = posts.filter(titleicontains=searchquery) # Category filtering categoryid = request.GET.get('category') if categoryid: posts = posts.filter(categoryid=categoryid) context = { 'posts': posts, 'categories': Category.objects.all(), 'searchquery': searchquery, } return render(request, 'blog/postlist.html', context) def postdetail(request, slug): """Display individual post details.""" post = getobjector404(Post, slug=slug, status='published') context = { 'post': post, 'relatedposts': Post.objects.filter( category=post.category, status='published' ).exclude(id=post.id)[:3] } return render(request, 'blog/postdetail.html', context) @loginrequired def postcreate(request): """Create new post.""" if request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.isvalid(): post = form.save(commit=False) post.author = request.user post.save() form.savem2m() # Save many-to-many relationships messages.success(request, 'Post created successfully!') return redirect('postdetail', slug=post.slug) else: form = PostForm() return render(request, 'blog/postform.html', {'form': form}) @loginrequired def postedit(request, slug): """Edit existing post.""" post = getobjector404(Post, slug=slug, author=request.user) if request.method == 'POST': form = PostForm(request.POST, request.FILES, instance=post) if form.isvalid(): form.save() messages.success(request, 'Post updated successfully!') return redirect('postdetail', slug=post.slug) else: form = PostForm(instance=post) return render(request, 'blog/postform.html', { 'form': form, 'post': post }) # Class-Based Views class PostListView(ListView): """List view for posts with pagination.""" model = Post templatename = 'blog/postlist.html' contextobjectname = 'posts' paginateby = 10 def getqueryset(self): return Post.objects.filter(status='published').selectrelated('author', 'category') def getcontextdata(self, kwargs): context = super().get_context_data(kwargs) context['categories'] = Category.objects.all() return context class PostDetailView(DetailView): """Detail view for individual posts.""" model = Post templatename = 'blog/postdetail.html' contextobjectname = 'post' def getqueryset(self): return Post.objects.filter(status='published') class PostCreateView(LoginRequiredMixin, CreateView): """Create view for new posts.""" model = Post formclass = PostForm templatename = 'blog/postform.html' def formvalid(self, form): form.instance.author = self.request.user return super().formvalid(form) class PostUpdateView(LoginRequiredMixin, UpdateView): """Update view for existing posts.""" model = Post formclass = PostForm templatename = 'blog/postform.html' def getqueryset(self): return Post.objects.filter(author=self.request.user) class PostDeleteView(LoginRequiredMixin, DeleteView): """Delete view for posts.""" model = Post templatename = 'blog/postconfirmdelete.html' successurl = reverselazy('postlist') def getqueryset(self): return Post.objects.filter(author=self.request.user) # API Views def apipost_list(request):
// original public source
davila7/claude-code-templates/cli-tool/templates/python/examples/django-app/.claude/commands/views.md
License: MIT License
Independent project, not affiliated with Anthropic. This skill remains the property of its original author.