settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
models.py
class Profile(models.Model):
img=models.ImageField(upload_to='media',null=True)
bio=models.TextField(max_length=50, null=True)
user=models.OneToOneField(User,related_name='profile',on_delete=models.CASCADE)
urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
so basically after I upload an image no media folder is created, i am working on latest version of django, if more info need to be provided, please tell.
EDIT: i created a media folder as someone suggested, but the image is not displaying, and it is not inside the media folder i have created
<html>
<body>
<img src="{{ user.profile.img.url }}" alt="{{profile.user}}">
<br>
<br>
{{profile.user}}
</body>
</html>
this is the code used to display image
the output of html in developer tool console is :
<html>
<body>
<img src="" alt="somename">
<br>
<br>
somename
</body>
</html>
the html code that is responsible for uploading the image:
<html>
<form method='post' enctype="multipart/form-data">
{% csrf_token %}
<input type="file" id="img" name="img" accept="image/*">
<textarea name='bio' placeholder='Write about yourself'></textarea>
<input type="submit">
</form>
</html>
views.py
#this function responsible for uploading image
def user_profile_view(request,pk):
user=User.objects.first()
if request.method == 'POST':
img=request.POST.get('img')
bio=request.POST.get('bio')
profile=Profile.objects.create(img=img,bio=bio,user=user)
return render(request,'profile.html')
#this function responsible for displaying profile.
def user_profile_display(request,pk):
user=get_object_or_404(User,pk=pk)
profile=Profile.objects.get(user=user)
return render(request,'display_profile.html',{'profile':profile})