How can I store a file path in Mysql database using django?
<p>I need to store a file path in db using django via ajax form submission . </p>

<p>Here is my view:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>def dashboard(request):
    container=[]
    DIR = os.path.realpath("/home/user/Desktop/Demo")
    WAY = os.listdir(DIR)
    for file in WAY:
        if file.endswith('.mp4'):
            file_name = file
            FDIR=os.path.join(DIR, file)
            container.append(FDIR)
            
    return render(request, 'dashboard.html', {'container': container})</code></pre>
</div>
</div>
</p>

<pre><code>def new_scheduler(request):
    if request.method =='POST':
        f_name = request.POST.get('file')
        dateAndTime = request.POST.get('dateAndTime')
    Scheduled_data = schedulesdb.objects.create(
            f_name = file,
            dateAndTime = dateAndTime,  
        )
    Scheduled_data.save()
    return HttpResponse ('done')
</code></pre>

<p>It save in database like <code>&lt;type 'file'&gt;</code> .</p>

<p>Here is my model.py:</p>

<pre><code>class schedulesdb(models.Model):
    f_name = models.CharField(max_length=100)
    dateAndTime = models.DateTimeField(['%Y-%m-%d %H:%M:%S'],null=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=2)
    def __unicode__(self):              #  on Python 2
        return self.f_name
</code></pre>

<p>Thanks in advance :)</p>

<p>From your code it's not 100% clear whether you're intending to handle file uploads from the client, or simply store strings that happen to be a file path (potentially for locating a file on some remote filesystem).</p>

<p><strong>1. File uploads</strong></p>

<p>Consider using the <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.FileField" rel="nofollow">FileField</a> model field type rather than the CharField.</p>

<p>The Django documentation has a solid explanation and examples of how to do simple <a href="https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/" rel="nofollow">file uploads</a>.</p>

<p><strong>2. Obtaining the actual POST data value for the f_name field</strong></p>

<p>Your code sample is storing "", because you're assigning 'file' (which is a builtin type) rather than the <code>f_name</code> variable that you previously declared. Like this:</p>

<pre><code>def new_scheduler(request):
if request.method =='POST':
    f_name = request.POST.get('file')
    dateAndTime = request.POST.get('dateAndTime')
Scheduled_data = schedulesdb.objects.create(
        f_name = f_name, # Note the use of f_name instead of file
        dateAndTime = dateAndTime,  
    )
Scheduled_data.save()
return HttpResponse ('done')
</code></pre>

python mysql ajax django