arpan mahatra
arpan mahatra

Reputation: 97

How Could I Add Image Upload Option To The Registration Form?

My forms.py is this.

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm,UserChangeForm

class RegistrationForm(UserCreationForm):
    email= forms.EmailField(required = True)

    class Meta:
        model = User
        fields = {
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        }

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

And, i have to add "upload image" option for the user so that user could upload their image from any file and load it on the profile.

Upvotes: 0

Views: 2991

Answers (1)

Kryštof Řeháček
Kryštof Řeháček

Reputation: 2483

You have to extend your user model with field profile_image = models.FileField() there is tutorial how to do it.

Than you have to add

class RegistrationForm(UserCreationForm):
    email= forms.EmailField(required = True)
    profile_image = forms.FileField() # add this field

    ...

    def save(self, commit=True):
        ...
        user.profile_image = self.cleaned_data.get('profile_image')
        ...

and add enctype="multipart/form-data" attribute to the <form ...> tag in the html template. Here is tutorial from Django docs on File Uploads.

EDIT

Extending the User model in the most basic way without changing the actual User model is that you'll create new model with one-to-one relation to the user model.

from django.conf import settings

class Profile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    profile_image = models.FileField() # where is stored the user profile image

this is the Profile of each user. You will have to create profile when user is created, so:

place this code to the any models.py or file that is loaded on the startup with django

from django.contrib import auth
auth.models.User.add_to_class('create_profile', create_profile)

then create the method below

from (appname).models import Profile

def create_profile(self, **kwargs):
    Profile.objects.create(
        user=self,
        **kwargs # you can pass other fields values upon creating
    )

then modify to the RegistrationForm as I wrote before and modify the save function

def save(self, commit=True):
    user = super(RegistrationForm, self).save(commit=False)
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.email = self.cleaned_data['email']

    # user has to be saved to add profile
    user.save()
    user.create_profile() 
    user.profile_img = self.cleaned_data.get('profile_image')
    user.profile.save()

    if commit:
        user.save()

also when you are instantiating the RegistrationForm with request.POST do it like

form = RegistrationForm(request.POST, request.FILES)
# to the files have to be passed also, they are not in request.POST
if form.is_valid():
    form.save()

and thats it.

Upvotes: 1

Related Questions