Python Django-the Practical Guide 99%

from django.shortcuts import redirect from .forms import PostForm def create_post(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): form.save() return redirect('home') else: form = PostForm() return render(request, 'blog/create.html', 'form': form) Django provides django.contrib.auth .

% if user.is_authenticated % Hello user.username % else % <a href="% url 'login' %">Login</a> % endif % For rapid CRUD: python django-the practical guide

def home(request): posts = Post.objects.all().order_by('-created_at') return render(request, 'blog/home.html', 'posts': posts) blog/templates/blog/home.html from django

# urls.py from django.contrib.auth import views as auth_views urlpatterns += [ path('login/', auth_views.LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), ] a href="% url 'login' %"&gt

from django.views.generic import ListView, CreateView from .models import Post class PostListView(ListView): model = Post template_name = 'blog/home.html' ordering = ['-created_at']

CBVs reduce code repetition significantly. blog/tests.py