<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Suman</title>
    <description>The latest articles on DEV Community by Suman (@sumanayak).</description>
    <link>https://dev.to/sumanayak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1758759%2F7eb5d752-35c4-4a7a-bedb-0b9e2fc92ba3.png</url>
      <title>DEV Community: Suman</title>
      <link>https://dev.to/sumanayak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sumanayak"/>
    <language>en</language>
    <item>
      <title>Adding Comments In Your Django Project</title>
      <dc:creator>Suman</dc:creator>
      <pubDate>Mon, 25 Aug 2025 09:10:33 +0000</pubDate>
      <link>https://dev.to/sumanayak/adding-comments-in-your-django-project-4oan</link>
      <guid>https://dev.to/sumanayak/adding-comments-in-your-django-project-4oan</guid>
      <description>&lt;p&gt;&lt;strong&gt;Utilizing Bootstrap Accordion for adding comments in the UI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In post_view.html:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;
  &amp;lt;a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample"&amp;gt;
    Comments : {{post.cmt_count}}
  &amp;lt;/a&amp;gt;

&amp;lt;/p&amp;gt;
&amp;lt;div class="collapse" id="collapseExample"&amp;gt;
 {% for comment in post.comment_set.all %}
  &amp;lt;div class="card card-body"&amp;gt;
    {{comment.content}}
  &amp;lt;/div&amp;gt;
{% endfor % }
&amp;lt;/div&amp;gt;
 &amp;lt;form method="POST"&amp;gt;
        {% csrf_token %}
&amp;lt;div class="col-md-8"&amp;gt;
              {{ c_form }}
            &amp;lt;/div&amp;gt;
 &amp;lt;input type="submit" class="btn btn-secondary" value="Comment"/&amp;gt;
 &amp;lt;/form&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a model to link user and post to the comment :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Comments(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    post = models.ForeignKey(UserPost,on_delete=models.CASCADE)
    content = models.CharField(max_length=500)

    def __str__(self):
        return self.content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Register your model to the admin site *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib import admin
from .models import UserPost,Comments

admin.site.register(Comments)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Make Migrations : *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py makemigrations
python manage.py migrate

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In case you face operational error delere all migraions folder to restart your database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;delete respectively : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;db.sqlite3&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in your app/migrations&lt;br&gt;
delete all files except &lt;strong&gt;init&lt;/strong&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;make migrations again, that's it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go to the Django admin panel and add some data in them models&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reverse Relationship to get number of comments on a post :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserPost(models.Model):
    title = models.CharField(max_length=150,null=False)
    content = models.TextField()
    author = models.ForeignKey(User,on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to=get_image_path )

    class Meta:
        ordering = ('-created_at',)

    def cmnt_count(self):
        return self.comments_set.all().count()

    def __str__(self):
        return self.title
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;views.py to render post:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .forms import PostForm

def post_view(request,pk):
    post = get_object_or_404(UserPost,id=pk)

    context={
        'post':post,
    }
    return render(request,'post_view.html',context)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Include the count as well as Comments on the UI using this reverse mapping&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;Comments : {{post.cmt_count}}&amp;lt;/p&amp;gt;
{% for comment in post.comment_set.all %}
  &amp;lt;div class="card card-body"&amp;gt;
    {{comment.content}}
  &amp;lt;/div&amp;gt;
{% endfor % }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Now to let users comment on the post :**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;in app/forms.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .models import Comments


class CommentForm(forms.ModelForm):
    content = forms.CharField(label="", widget=forms.TextInput(attrs={'placeholder': 'Write Your Thoughts on This Blog','class':'mt-2 focus:ring-1 focus:ring-blue-500'}))

    class Meta:
        model = Comments
        fields = ['content']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;in views.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .forms import PostForm,CommentForm

def post_view(request,pk):
    post = get_object_or_404(UserPost,id=pk)
    if request.method == "POST":
        if not request.user.is_authenticated:
            messages.warning(request, "Please log in to send a message.")
            login_url = f"{redirect('new_login').url}?{urlencode({'next': request.path})}"  # Preserve return URL
            return redirect(login_url)

        c_form = CommentForm(request.POST)
        if c_form.is_valid():
            instance = c_form.save(commit=False)
            instance.user = request.user
            instance.post = post
            instance.save()
            return redirect('post_view',pk=post.id)
    else:
        c_form = CommentForm()


    context={
        'post':post,
        'c_form':c_form
    }
    return render(request,'post_view.html',context)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Render this form in the UI :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form method="POST"&amp;gt;
        {% csrf_token %}
&amp;lt;div class="col-md-8"&amp;gt;
              {{ c_form }}
            &amp;lt;/div&amp;gt;
 &amp;lt;input type="submit" class="btn btn-secondary" value="Comment"/&amp;gt;
 &amp;lt;/form&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, Happy Coding you all :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>django</category>
      <category>python</category>
    </item>
    <item>
      <title>Django Registration,Login Logout and Profile Easy Setup</title>
      <dc:creator>Suman</dc:creator>
      <pubDate>Sun, 24 Aug 2025 10:20:45 +0000</pubDate>
      <link>https://dev.to/sumanayak/django-registrationlogin-logout-and-profile-easy-setup-3oke</link>
      <guid>https://dev.to/sumanayak/django-registrationlogin-logout-and-profile-easy-setup-3oke</guid>
      <description>&lt;h2&gt;
  
  
  Registration Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create a new app User&lt;/strong&gt;&lt;br&gt;
 -- to seperate the logic of registeration,login and logout and create a module that can be integrated in any Django application&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In main app urls.py :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;users/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;users.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;in User urls.py :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.urls import path
from .import views

urlpatterns = [
    path('sign_up/', views.index, name='users-sign-up')
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create forms.py in User app and write the following:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ['username','email','password1','password2'] 

        def __init__(self, *args, **kwargs):
            super(SignUpForm, self).__init__(*args, **kwargs)

            for fieldname in ['password1','password2']:
                self.fields[fieldname].help_text = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In views (User) :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.shortcuts import render,redirect
from .forms import SignUpForm

def index(request):
    if request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = SignUpForm()
    context = {
        'form':form
    }
    return render(request,'User/sign_up.html',context) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Sign_up.html :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;title&amp;gt;{% block title %}Sign Up{% endblock %}&amp;lt;/title&amp;gt;
&amp;lt;form method="POST"&amp;gt;
    {% csrf_token %}
    {{ form|crispy }}
    &amp;lt;input type="submit" value="Register" class="btn btn-primary"&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add this to base.html to show title dynamically:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;title&amp;gt;{% block title %}{% endblock %}&amp;lt;/title&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Login Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In the same urls.py:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.auth import views as auth_view

path('login/', auth_view.LoginView.as_view(template_name='User/login.html'), name='login'),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Sign_in.html :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;title&amp;gt;{% block title %}Sign In{% endblock %}&amp;lt;/title&amp;gt;
&amp;lt;form method="POST"&amp;gt;
    {% csrf_token %}
    {{ form|crispy }}
    &amp;lt;input type="submit" value="Login" class="btn btn-primary"&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In settings.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOGIN_REDIRECT_URL = 'home' 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optimize your navbar :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="collapse navbar-collapse" id="navbarNav"&amp;gt;
  &amp;lt;ul class="navbar-nav ms-auto"&amp;gt;
    {% if request.user.is_authenticated %}
      &amp;lt;li class="nav-item"&amp;gt;
        &amp;lt;a class="nav-link" href="{% url 'blog:create' %}"&amp;gt;New BlogPost&amp;lt;/a&amp;gt;
      &amp;lt;/li&amp;gt;
      &amp;lt;li class="nav-item"&amp;gt;
        &amp;lt;a class="nav-link" href="{% url 'logout' %}"&amp;gt;Logout&amp;lt;/a&amp;gt;
      &amp;lt;/li&amp;gt;
    {% else %}
      &amp;lt;li class="nav-item"&amp;gt;
        &amp;lt;a class="nav-link" href="{% url 'login' %}"&amp;gt;Login&amp;lt;/a&amp;gt;
      &amp;lt;/li&amp;gt;
      &amp;lt;li class="nav-item"&amp;gt;
        &amp;lt;a class="nav-link" href="{% url 'register' %}"&amp;gt;Register&amp;lt;/a&amp;gt;
      &amp;lt;/li&amp;gt;
    {% endif %}
  &amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logout Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In the same urls.py:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path('logout/', views.LogoutView.as_view(next_page='home'), name='logout'),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Logout.html :&lt;/strong&gt; (only for the flow of execution)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;title&amp;gt;{% block title %}Logout{% endblock %}&amp;lt;/title&amp;gt;
&amp;lt;form method="POST"&amp;gt;
    {% csrf_token %}
    {{ form|crispy }}
    &amp;lt;input type="submit" value="Login" class="btn btn-primary"&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;hr&amp;gt;
&amp;lt;div class="alert alert-info"&amp;gt;
  You have been logged out.
&amp;lt;/div&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use this url template tag wherever required in navbar:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% url 'logout' %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Profile Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In forms.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from .models import Profile

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ['username','email','password1','password2']

class EditProfile(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username','email']

    def __init__(self, *args, **kwargs):
        super(EditProfile, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'email']:
                self.fields[fieldname].help_text = None

class EditPro(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['bio','pic']

        widgets = {
                'bio': forms.Textarea(attrs={'class': 'form-control', 'rows': 5, 'placeholder': 'Write about yourself...'}),
                'pic': forms.ClearableFileInput(attrs={'class': 'form-control'}),
            }

    def __init__(self, *args, **kwargs):
        super(EditPro, self).__init__(*args, **kwargs)

        for fieldname in ['bio', 'pic']:
                self.fields[fieldname].help_text = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In views.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.shortcuts import render,redirect
from .forms import SignUpForm,EditProfile,EditPro
from django.contrib import messages
from django.contrib.auth.views import LogoutView
from myBlog.models import UserPost,Category
from django.contrib.auth.decorators import login_required
# Create your views here.

def index(request):
    if request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('new_login')
    else:
        form = SignUpForm()
    context = {
        'form':form
    }
    return render(request,'User/sign_up.html',context)

@login_required
def userProfile(request):
    if request.method == "POST":
        u_form = EditProfile(request.POST or None,instance=request.user)
        p_form = EditPro(request.POST or None,request.FILES or None,instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            return redirect('profile')
    else:
        u_form = EditProfile(instance=request.user)
        p_form = EditPro(instance=request.user.profile)

    blog_count = UserPost.objects.filter(author=request.user).count()
    category = Category.objects.filter(status = 0)
    context = {
        'u_form':u_form,
        'p_form':p_form,
        'blog_count':blog_count,
        'category':category

    }
    return render(request,'User/profile.html',context)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;In urls.py *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path('profile/',views.profile, name='user-profile'),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In navbar.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="{% url 'user-profile' %}"&amp;gt;Profile&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In User/profile.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block title %}Profile Page{% endblock %}

{% block content %}
&amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="row mt-5 pt-3"&amp;gt;
        &amp;lt;div class="col-md-6 offset-md-3"&amp;gt;
            &amp;lt;div class="card my-3 shadow"&amp;gt;
                &amp;lt;div class="card-body"&amp;gt;
                    &amp;lt;div class="row"&amp;gt;
                        &amp;lt;!-- Profile Image --&amp;gt;
                        &amp;lt;div class="col-md-4 text-center"&amp;gt;
                            {% if user.profile.pic %}
                                &amp;lt;img src="{{ user.profile.pic.url }}" 
                                     class="img-fluid rounded-circle mb-3 shadow-sm" 
                                     alt="{{ user.username }}"&amp;gt;
                            {% else %}
                                &amp;lt;img src="https://via.placeholder.com/150" 
                                     class="img-fluid rounded-circle mb-3 shadow-sm" 
                                     alt="Default Profile"&amp;gt;
                            {% endif %}
                            &amp;lt;h5 class="mt-2"&amp;gt;{{ user.username }}&amp;lt;/h5&amp;gt;
                        &amp;lt;/div&amp;gt;

                        &amp;lt;!-- Profile Form --&amp;gt;
                        &amp;lt;div class="col-md-8"&amp;gt;
                            &amp;lt;form method="POST" enctype="multipart/form-data"&amp;gt;
                                {% csrf_token %}
                                {{ form|crispy }}
                                &amp;lt;div class="d-grid"&amp;gt;
                                    &amp;lt;button type="submit" class="btn btn-primary"&amp;gt;
                                        Update Profile
                                    &amp;lt;/button&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/form&amp;gt;
                        &amp;lt;/div&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bootstrap Modal Example (optional)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- Button trigger modal --&amp;gt;
&amp;lt;button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"&amp;gt;
  Edit Profile
&amp;lt;/button&amp;gt;

&amp;lt;!-- Modal --&amp;gt;
&amp;lt;div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"&amp;gt;
  &amp;lt;div class="modal-dialog"&amp;gt;
    &amp;lt;div class="modal-content"&amp;gt;
      &amp;lt;form method="POST" enctype="multipart/form-data"&amp;gt;
        {% csrf_token %}
        &amp;lt;div class="modal-header"&amp;gt;
          &amp;lt;h1 class="modal-title fs-5" id="exampleModalLabel"&amp;gt;Edit Profile&amp;lt;/h1&amp;gt;
          &amp;lt;button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"&amp;gt;&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div class="modal-body"&amp;gt;
          &amp;lt;div class="row"&amp;gt;
            &amp;lt;div class="col-md-4 text-center"&amp;gt;
              &amp;lt;img src="{{ user.profile.pic.url }}" alt="Profile Picture" class="img-fluid rounded"&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="col-md-8"&amp;gt;
              {{ u_form|crispy }}
              {{ p_form|crispy }}
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div class="modal-footer"&amp;gt;
          &amp;lt;button type="button" class="btn btn-secondary" data-bs-dismiss="modal"&amp;gt;Close&amp;lt;/button&amp;gt;
          &amp;lt;button type="submit" class="btn btn-primary"&amp;gt;Save changes&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In User/models.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models
from django.contrib.auth.models import User
import os,datetime
from django.core.validators import FileExtensionValidator

class Profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    pic = models.ImageField(default='default.png',upload_to='avatars/',validators=[FileExtensionValidator(['png','jpg','jpeg','webp'])])
    bio = models.TextField(blank=True,null=True)

    def __str__(self):
        return self.user.username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Serve Media Files&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pillow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# urls.py
from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dump your all Static files for production&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / "staticfiles"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py collectstatic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;admins.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib import admin
from .models import Profile

admin.site.register(Profile)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In template&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form method="post" enctype="multipart/form-data"&amp;gt;
  {% csrf_token %}
  {{ form.as_p }}
  &amp;lt;button type="submit"&amp;gt;Upload&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Also Add default.jpg/png in media folder to show default pic&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Use Django Signals to create profile automatically after user sign up&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .models import Profile
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

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

@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;apps.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.apps import AppConfig

class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'user'

    def ready(self):
        import user.signals
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;That's it :) Happy Coding you all.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>backenddevelopment</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
