20
loading...
This website collects cookies to deliver better user experience
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required
def profile(request):
return render(request, 'users/profile.html')
login_required
decorator limits access to logged in users.login_required()
will redirect him/her to settings.LOGIN_URL
(which we set up in the login/logout part of the series) by passing the current absolute path in the query string. Example: /login/?next=/profile/
from django.urls import path
from .views import profile
urlpatterns = [
# Add this
path('profile/', profile, name='users-profile'),
]
{% extends "users/base.html" %}
{% block title %}Profile Page{% endblock title %}
{% block content %}
<div class="row my-3 p-3">
<h1>This is the profile page for {{user.username}}</h1>
</div>
{% endblock content %}
OneToOneField
model field.from django.db import models
from django.contrib.auth.models import User
# Extending User Model Using a One-To-One Link
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(default='default.jpg', upload_to='profile_images')
bio = models.TextField()
def __str__(self):
return self.user.username
OneToOneField
specifies which model the current model will be related to, which in our case is the User model. The second argument on_delete=models.CASCADE
means that if a user is deleted, delete his/her profile as well. ImageField
, default='default.jpg'
is the default image to use for a user if they don't upload one themselves. The second argument upload_to='profile_images'
is the directory where images get uploaded to.__str__
method converts an object into its string representation which makes it more descriptive and readable when an instance of the profile is printed out. So, whenever we print out the profile of a user, it will display his/her username.pillow
which is one of the most common image processing library that lets us do different kinds of image manipulations in python. ImageField
, so go to your terminal and type the following.pip install pillow
python manage.py makemigrations
python manage.py migrate
from django.contrib import admin
from .models import Profile
admin.site.register(Profile)
admin.site.register
to register it.MEDIA_ROOT
is full path to a directory where uploaded files are stored. Usually we store such files by creating a directory inside the project's base directory.MEDIA_URL
is the base URL to serve media files. This is what lets us access the media through our web browser.MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
As you can see from the image above, the profile picture that users upload will live inside /media/profile_images/your_image
. Till we create the frontend where uses will upload pictures, you can go to the admin panel and create images for registered users to see if all this is working well.
The default profile picture that is given to users lives inside /media/
therefore, put any default image you want inside this directory with the name default.jpg
.
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
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()
create_profile
is the receiver function which is run every time a user is created. post_save
is the signal that is sent at the end of the save method. save()
method has finished executing, it sends a signal(post_save
) to the receiver function (create_profile
) then this function will receive the signal to create and save a profile instance for that user.ready()
method of the app's configuration by importing the signals module. Open apps.py where we can include any application configuration for the users app.from django.apps import AppConfig
class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'users'
# add this
def ready(self):
import users.signals # noqa
ready()
method of the users app config to perform initialization task which is registering signals.