📝 Django

User Profile Page

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
82
🌿
Level
Medium

Охватываемые темы: Simple Profile (User Data Only), Extending with a Profile Model, Auto-creating a Profile via Signals, Profile Template.

Simple Profile (User Data Only)

# views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView, UpdateView
from django.contrib.auth.models import User

class ProfileView(LoginRequiredMixin, DetailView):
    model = User
    template_name = 'accounts/profile.html'

    def get_object(self):
        return self.request.user  # always the current user
# urls.py
path('profile/', views.ProfileView.as_view(), name='profile'),

Extending with a Profile Model

# models.py
from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    bio = models.TextField(blank=True)
    avatar = models.ImageField(upload_to='avatars/', blank=True)
    website = models.URLField(blank=True)

    def __str__(self):
        return f'Profile of {self.user.username}'

Auto-creating a Profile via Signals

# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import Profile

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

Profile Template

{% extends 'base.html' %}
{% block content %}
<div class="profile">
  {% if user.profile.avatar %}
    <img src="{{ user.profile.avatar.url }}" alt="Avatar">
  {% endif %}
  <h1>{{ user.get_full_name|default:user.username }}</h1>
  <p>{{ user.profile.bio }}</p>
  <a href="{% url 'profile-edit' %}">Edit profile</a>
</div>
{% endblock %}

Editing the Profile

class ProfileUpdateView(LoginRequiredMixin, UpdateView):
    model = Profile
    fields = ['bio', 'avatar', 'website']
    template_name = 'accounts/profile_edit.html'
    success_url = reverse_lazy('profile')

    def get_object(self):
        return self.request.user.profile

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 87
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 78

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!