Reputation: 35
I am making a personal training website and am having trouble getting the profile pic to upload
my form looks like this:
<form class="form-horizontal" action="updateProfile" method="post" enctype= "multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label class="control-label col-sm-2" for="gym">Main Gym</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="gym" placeholder="Enter gym name" name="gym" id="gym">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="last name">Qualifications</label>
<div class="col-sm-10">
<textarea name="qualifications" rows="10" cols="130" name="qualifications" id="qualifications"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="last name">About Me</label>
<div class="col-sm-10">
<textarea name="aboutme" rows="10" cols="130" id="aboutme"></textarea>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="servicedetails">Service Details</label>
<div class="col-sm-10">
<textarea name="servicedetails" rows="10" cols="130" id="servicedetails"></textarea>
</div>
<div class="form-group">
<label for="avatar">Choose a profile picture:</label>
<div class="form-group">
<input type="file"
id="avatar" name="avatar"
accept="image/png, image/jpeg">
</div>
</div>
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
models.py:
class trainerabout(models.Model):
userID = models.IntegerField()
gym = models.TextField(max_length=30)
qualifications = models.TextField()
aboutme = models.TextField()
servicedetails = models.TextField()
profilepic = models.ImageField(upload_to='images/')
added this to urls.py
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py:
def updateProfile(request):
if request.method == 'POST':
gym = request.POST['gym']
qualifications = request.POST['qualifications']
aboutme = request.POST['aboutme']
servicedetails = request.POST['servicedetails']
avatar = request.POST['avatar']
trainer = trainerabout(gym=gym, qualifications=qualifications, aboutme=aboutme, servicedetails=servicedetails, profilepic=avatar, userID=request.user.id)
trainer.save()
return render(request, 'updateProfile.html')
added this to settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
when I enter in stuff on my form it saves the url of the image to the database no problem but the image doesn't get saved into the media folder. I was under the impression that it created a media folder for you but it didn't. I then created a media folder and still nothing. What am I doing wrong here?
Upvotes: 0
Views: 317
Reputation: 1596
You should be using request.FILES, not request.POST to access uploaded files. Please, see Django docs for more. And, it is convention that model names are Capitalized at each containing word, like TrainerAbout
, not trainerabout
.
Upvotes: 1