Image Uploading through form

Hi guys i have created a model name contact and successfully submit data through form into that model, but when i try to submit the image through form it submit successfully too,so far so good but when i try to open the image through admin dashboard by clicking the link it shows me 404 and the image didnt store in media folder. But when i submit the image through admin it follows the correct path and store in my media folder.

Hi!

Can you post the view, form and template relevant to this problem?
Also, have you defined MEDIA_ROOT and MEDIA_URL in your `settings.py?

Take a look at the documentation on handling uploaded files.

views.py

from django.shortcuts import render
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from web.models import Contact,NewsLetter
def home(request):
return render(request, ‘web/home.html’)

def contact(request):
if request.method == ‘POST’:
first_name=request.POST[‘first_name’]
last_name=request.POST[‘last_name’]
email=request.POST[‘email’]
phone=request.POST[‘phone’]
message=request.POST[‘message’]
mm=request.POST[‘mm’]
contact=Contact(first_name=first_name,last_name=last_name,email=email
,phone=phone,message=message,image=mm)
contact.save()
print(‘Data Entered’)
return render(request, ‘web/contact.html’)

def news(request):
if request.method == ‘POST’:
newsNL=request.POST[‘email’]
newsletter=NewsLetter(emailNL=newsNL)
newsletter.save()
return render(request, ‘web/news.html’)

htm form

{% extends "web/base.html "%}
{% load static %}
{% block title %}
Contact
{% endblock title %}

{% block head %}

{% endblock %} {% block content %}
{% csrf_token %}
					  <div class="col-md-6">
						  <div class="form-group">
							  <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name" required data-error="Please enter your name">
							  <div class="help-block with-errors"></div>
						  </div>
					  </div>

					<div class="col-md-6">
						<div class="form-group">
							<input type="email" placeholder="Your Email" id="email" class="form-control" name="email" required data-error="Please enter your email">
							<div class="help-block with-errors"></div>
						</div> 
					</div>
					<div class="col-md-6">
						<div class="form-group">
							<input type="phone" placeholder="Your number" id="phone" class="form-control" name="phone" required data-error="Please enter your number">
							<div class="help-block with-errors"></div>
						</div>
					</div>
					<div class="col-md-12">
						<div class="form-group"> 
							<textarea class="form-control" placeholder="Your Message" id="message" name="message" rows="8" data-error="Write your message" required></textarea>
							<div class="help-block with-errors"></div>
						</div>
						<div class="col-md-4">
							<input type="file" name="mm" accept="application/pdf , image/jpeg">
						</div>
						<div class="submit-button text-center">
							<button class="btn btn-common" id="submit" type="submit">Send Message</button>
							<div id="msgSubmit" class="h3 text-center hidden"></div> 
							<div class="clearfix"></div> 
						</div>
						
					</div>
				  </div>            
				</form>
			  </div>
			</div>
		</div>
	</div>
</div>

{% endblock %}

models.py

from django.db import models
import datetime
from django.utils import timezone

Create your models here.

class Contact(models.Model):
first_name=models.CharField(max_length=20)
last_name=models.CharField(max_length=20)
email=models.EmailField()
phone=models.TextField()
message=models.TextField()
image=models.FileField(null=True, blank=True, upload_to=‘images’)
created=models.DateTimeField(auto_now_add=True)

def __str__(self):
	return self.first_name + " - " + self.email

class NewsLetter(models.Model):
emailNL=models.EmailField()

def __str__(self):
	return self.emailNL

Blockquote Yes i did define MEDIA_ROOT and MEDIA_URL in `settings.py

When you’re posting code, html, css here, please enclose it between lines of three backtick (`) characters to preserve the formatting of the code. This means you’ll have a line of ```, then however many lines of code, then another line of ```. Make sure you use the backtick - ` and not the apostrophe - '.

Also, you probably should be creating a Django form for doing this. Manually creating the fields in the HTML and assigning them in your view is doing a lot more work than necessary. You’re going to find it a lot easier to do this using a form.

Ken

2 Likes

oh sorry, did’nt know that.
And thank you sir i have created forms.py file and follow the guidelines from documentation and it worked fine.

No need to apologize - we’re here to share information. Glad to see you’ve got it working!

Iam trying to upload image through url in Django in my forms.py of Django, but I have bad results. Please I need help

I suggest you open a new topic for your question. Include in that topic your form, view and model, along with a more complete description of your “bad results”.

Note, when you post code, templates, or error messages here, enclose that code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.