Siddhu
Siddhu

Reputation: 327

How to upload an image in Django

I'm creating a new app in django environment. I want to upload image of user when he/she sign-up's in my app and i want to use the same image when he/she posts any comment in my app. So how to integrate image-uploading code into my app code?

Upvotes: 1

Views: 216

Answers (1)

Pratik Mandrekar
Pratik Mandrekar

Reputation: 9566

You should be using UserProfile as has been answered here Django user profile and have the ImageField in there.

Then create a ModelForm for the UserProfile and render it wherever you want the user to setup their profile and add a user image. The Model form will render the ImageField as a file upload field and have default handles for it. Make sure you have MEDIA_URL configured in your settings.py. You would also need PIL installed in your path for the image upload to work.

You can access the UserProfile instance for your user doing user.get_profile in your templates and as a callable elsewhere.

Upvotes: 1

Related Questions