Why I am not getting images from django model in correct ways?
I have created a model in which there are many fields, one of which is the picture field. When I am storing the data from the admin panel the images are stored with the correct address in the database but when I am storing the images from an HTML form they are stored with another address so I am missing the images in my template. Here is my model in Django:
class User(AbstractUser):
picture = models.ImageField(upload_to='profile_pictures', null=True, blank=True)
full_name = models.CharField(max_length=100, help_text='Help people discover your account by using the name you\'re known by: either your full name, nickname, or business name.')
email = models.EmailField(blank=True)
# Optional fields
bio = models.TextField(null=True, blank=True, help_text='Provide your personal information, even if the account is used for a business, a pet or something else. This won\'t be a part of your public profile.')
website = models.URLField(null=True, blank=True)
phone_number = models.CharField(max_length=20, null=True, blank=True)
gender = models.CharField(max_length=10, choices=GENDER_CHOICES, null=True, blank=True)
is_private_account = models.BooleanField(null=True, blank=True)
first_name = None
last_name = None
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['full_name']
objects = CustomUserManager()
def __str__(self):
return self.email
Here is the HTML form that I am using to store the data from the user:
<form action="posts" method="POST">
{% csrf_token %}
<input type="text" name="text" placeholder="enter your story"><br><br>
<input type="file" name="image"><br><br>
<input type="submit">
</form>
I am taking this form from the user and storing it in the database as follows;
def posts(request):
if request.method=='POST':
text=request.POST['text']
image=request.POST['image']
post_obj=Post(text=text,image=image,user=request.user)
post_obj.save()
return redirect('login')
else:
return render(request,'user/posts.html')
With this, there are two types of URLs in my User table for storing the images. These are as follows.
"post_images/images_6.jpeg"
"post_images/images5.jpeg"
"post_images/images.jpeg"
"post_images/download21.jpeg"
"sample-clouds-400x300.jpg"
The first four images are stored by the admin and the last is stored by the user. Why it is going so.