How to upload an image in Django - 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?

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.

Related

Uploading images/file in Django in production mode

I am a noob in Django and I am about to deploy my first site in Django. I have a Profile model in my models.py file which has profile_pic attribute which will store the profile pic of the user. It is working well when upload the image in the development mode,ie, when DEBUG=True. But what changes do I have to make in the urls.py so that users can easily upload their profile picture even after deploying my site. Any code snippetor examples or some link would be very helpful.

Django: linking users to models in an App (new to django><)

I am quite new to django, I know little terms... so it will be appreciated if you would answer more specifically how to implement. <3
I am working on a web development assignment about an image sharing platform. functionality involves users upload, download, delete and search images, etc.
recently I have implemented 'allauth' plugin to my site so that users can login in to the site, also I created an app called 'AllImages' with a model - 'Image'.
MY QUESTION IS:
How can I link the users to 'AllImages' and 'Image', so that the system knows who uploaded a particular image.
because I want to only let the uploader himself being able to delete his image
I really know nothing about the plugin, its just done by someone haha....
this is the allauth plugin
In your Images model add a foreign key field to user.
from django.contrib.auth.models import User
Further you can query something like..
Images.objects.filter(user=logged_user) to get all images of this particular user.
Django auth User

Django Forms without admin

I am learning Django, I saw so many videos of DJango.
I just wanted to know one thing that can we create an app (like login app, or contact app) without registering it into in admin (admin.py).
Off course it should have model etc to save the contact details or login details etc. Is it possible in Django ?
Just don't create your admin.py file or not register the model that you don't want to see there. Django admin is fully optional.

How to relate a user to Django-avatar plugin?

I am new to Django and trying to create a registration form for users. I want users to upload there image during signup. After some research I got django-avatar and easy thumbnail as way to go, am I correct? So I went ahead with django-avatar but I am confused how to add a form field to userprofile that talks to django avatar. Am I in correct path or should I use some other plugin for uplaoding an user image. If Django-avatar is the way to go how should I create a form field for image and how my view look like?
Try using Formsets with the form set to avatar.forms.UploadAvatarForm

Adding fields to user's personal info in Django admin page

I just started a Django project (there are no apps in it). I activated the admin in settings file and can access the Django administration page. There is a column in Django page to add users; while adding users I get only three fields under personnal info, but I need to store some more information about users. I Googled around and found that I can use user profiles to accomplish this. I tried, but I am having problems.
My aim is to add three more fields to the user table:
role
contact number
other
I need details like: which function I need to write and where to do this.
I found this, but I do not know where I need to write these steps. I would greatly appreciate a more clear explanation of this.
Django User Profiles is what you need. The blog you linked to has clear steps on how to do it. You can check out the Django documentation. http://www.turnkeylinux.org/blog/django-profile also provides a good explanation.
Basically you need to create a new model with User as ForeignKey and define the model in the settings.py as AUTH_PROFILE_MODULE = "django_app.your_profile_modelname". Create the profile and save it just like any other model, and access it using user.get_profile()
Adding a couple of things in response to your questions below:
First, do not create apps as a directory. Use startapp <appname> [destination] as described here. That will create the app directory.
Second, you have to add the app to INSTALLED_APPS in the project's settings file, do a syncdb. Basically, follow the steps in Django tutorial on writing your first app.
Third, UserProfile is a separate model. It is not an extension of User. It is associated with the User just because you added User as the ForeignKey.
Fourth, to be able to see the user profile model in admin, you do exactly what you would do to add any other model to admin page. Create a file names admin.py under your app with:
from django.contrib import admin
from myproject.app.models import UserProfile
admin.site.register(UserProfile)
There are three key concepts to understand:
There is no built in "profile" system in Django, beyond the limited auth app which is really geared just to user login. You are expected to roll your own.
There is nothing magical about a profile record in itslef, it is just like any other record that takes User as a foreign key (or, more properly, a one-to-one field as per the docs). You create it by creating a custom django app (traditionally called profiles) and a model for that app (traditionally called UserProfile, since Profile is not allowed as a model name).
The only thing that sets UserProfile aparts as a model is that you specify it as the AUTH_PROFILE_MODULE which means that it is accessible when called .get_profile() on a User record. That's it. If you set up the UserProfile like so:
def UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
other fields
then you can also access the profile as user.profile rather than user.get_profile() which some people prefer.
Again, nothing magical about the profile model -- it is just a model record like any other model record.
If you want to be able to edit additional fields within the user form that's more complicated; easiest way is probable unregister User and then register it again using your custom ModelAdmin and form class but judging by your question you're probably not at that level yet.