I am trying to upload an image using Django and there is no errors. But there is no file or directories inside the media folder.And except image field all other fields are updated in table.
Models.py
from django.db import models
# Create your models here.
class Player_Profile(models.Model):
name=models.CharField(max_length=50, null=True)
email=models.EmailField(max_length=50)
profile_picture=models.ImageField(upload_to='profile_picture/%y%m%d', blank=True, null=True)
age = models.BooleanField()
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from upload.models import Player_Profile
def home(request):
return render_to_response('upload/index.html',context_instance=RequestContext(request))
def submit(request):
if request.method == 'POST':
username=request.POST.get('username')
email=request.POST.get('email')
age=request.POST.get('age')
pic=request.FILES.get('myfile')
profile_obj=Player_Profile(profile_picture=pic,name=username, email=email,age=age).save()
return render_to_response('upload/welcome.html',context_instance=RequestContext(request))
index.html
<form action="/upload/submit/" method="POST" encrypt="multipart/form-data">
{% csrf_token %}
User Name :<input type="text" name="username" id="usrname"/><br/>
Age :<input type="text" name="age" id="age"/><br/>
Email :<input type="email" name="email">
<input type="file" name="myfile" /><br/>
<input type="submit" name="submit" value="Upload" />
and inside settings
MEDIA_ROOT = '/home/mridul/Desktop/Django/interim/pic/uploadpic/media'
MEDIA_URL = '/media/'
and manually create media directory inside uploadpic directory.
It's not:
<form action="/upload/submit/" method="POST" encrypt="multipart/form-data">
it's
<form action="/upload/submit/" method="POST" enctype="multipart/form-data">
i.e. enctype
not encrypt
As an aside, you should use a Form
or ModelForm
to do this, it will make your life much easier.