Priya Sharma
Jan 2026
2 tabs
from django.contrib import messages
from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
@login_required
def update_profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST, instance=request.user.profile)
if form.is_valid():
form.save()
messages.success(request, 'Profile updated successfully!')
return redirect('profile')
else:
messages.error(request, 'Please correct the errors below.')
else:
form = ProfileForm(instance=request.user.profile)
return render(request, 'accounts/profile.html', {'form': form})
def delete_account(request):
if request.method == 'POST':
request.user.delete()
messages.warning(request, 'Your account has been deleted.')
return redirect('home')
{% if messages %}
<div class="messages">
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
<button type="button" class="close">×</button>
</div>
{% endfor %}
</div>
{% endif %}
2 files · python, django
Explain with highlit
Django's message framework provides one-time notifications to users. I use messages.success(), messages.error(), messages.warning(), and messages.info() to add messages. Messages persist across redirects and are displayed once. I configure message storage backend in settings—session-based is default. In templates, I iterate over messages to display them. For AJAX, I return messages in JSON. The framework works seamlessly with forms and generic views. This gives users feedback on their actions without manual session management.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem
class DashboardView(TemplateView):
Django aggregation with annotate for statistics
django
python
database
by Priya Sharma
1 tab
erb
<h1>Products</h1>
<%= form_with url: products_path, method: :get,
data: { turbo_frame: "products_list", turbo_action: "advance" } do |f| %>
<div class="filters">
<%= f.text_field :q, value: params[:q], placeholder: "Search products" %>
Frame navigation that targets a specific frame via form_with
rails
hotwire
turbo
by codesnips
3 tabs
Share this code
Here's the card — post it anywhere.