I'm pretty new to Python\Django.
I'm trying to make a local Netflix-like Library for my Movie\TV shows collection.
The general idea is that the user chooses a media folder, the server side runs on the files in the folder adds them to the database and then the user can search for the items and play them back in the GUI.
The first snag I ran into is getting the folder path from the user without actually uploading any files. After doing some searching online I found this :
<input type="file" id="file_input" webkitdirectory="" directory="">
This HTML code allows the user to choose a folder and iterates through all the files inside, however, I don't know how can I pass this information to views.py so that I could run logic on the input.
Does anyone know how this could be accomplished ?
For security reasons, browsers do not allow to get folder path (Stack Overflow). Since you do not want the user to upload files, the possible solution would be to explicitly mention the folder path in <input type="text">. The simplest solution would be python –m SimpleHTTPServer (source, docs). You may be also interested in this Django app.
Related
I am currently trying to extract the images from a document that the user is uploading into the media repository of my Django app. The code that currently works for me is:
html = pypandoc.convert(
tmp_loc,
'html5',
extra_args=['--extract-media=']
)
This correctly extracts the images into the media directory as image01.jpg
In HTML the img src is:
<img src="/media/image01.jpg" />
Now the problem is that when the user uploads another docx which also has a image it replaces the previous image when it is uploaded as it is also saved by the name image01.jpg.
To solve this problem I thought we could just create a new folder in the media repository and name of the new folder would be the doc-name. So now the code looks like this:
html = pypandoc.convert(
tmp_loc,
'html5',
extra_args=['--extract-media=/media/<some_doc_name>']
)
But the moment I run this I get the following error:
Pandoc died with exitcode "1" during conversion: b'pandoc: /media/docs: createDirectory: permission denied (Permission denied)\n'
Could someone guide me what is going wrong? How to fix this?
Any alternative methods of solving this problem would also be appreciated!!
I am using the Pypandoc module in python.
error you are getting clearly says that you do not have permission to create directory under /media/docs
there maybe a multiple reasons why such a thing happens
you do not have permission to create subdirectories under "/media/docs" - just change the permissions
you have a permissions, but you are running your application under other user name that does not have permissions - create group and change permission for that group
you want to extract to "media" directory under your application, not the system root "/media" - your path is wrongly specified - should have "more" before e.g. "/home/user/program/media/docs" or "media/docs" (without leading "/")
you are trying to extract data to the non-existing subdirectory and your program can't handle such a situation, because can't create "parent" directories - so just make sure that directory is created
last thing - if you are uploading documents, do not assume that they do have unique names, use something unique (like primary key of the created record), or check uniqueness by validating that directory does not exist and if it is there, create new one with some additional number or random text at the end.
I've a website running on Django, Heroku.
I need to add few static JavaScript files for a third-party plugin.
My newly added files are available at domain.com/static/filename.js.
I need them to be available at domain.com/filename.js.
How to make ONLY the newly added Javascript files available at domain.com/filename.js?
If the info is not sufficient please ask which code is needed in the comments.
My first choice in this situation would be to fix whatever is stopping you from putting it into /static/. I can't imagine any half-decent third-party plugin would demand that the files be in the root; there must be some way to configure it to work from a subdirectory. If there isn't, I'd fork the project and add the option, then try to get them to merge it back. I realise you've probably already explored this option, but can you give us some more details about the plugin you're trying to use, and the reason it needs to go into the root? This really would be the best solution.
If you really must have the file in the root, and want to keep it as part of your django project, I'd try symlinking the files into the public root. This would mean it would be available in both locations; I can't see why that would be a problem, but you do specify "ONLY" in the root and I'm sure you have your reasons; in that case, perhaps you could configure your web server to redirect from /static/filename.js to /filename.js?
Lastly, you technically could change the settings STATIC_URL and STATIC_ROOT to point at the root directory, but that sounds like a pretty terrible idea to me. If you've got this far and still need to do it, it would be far better to take the file out of your django project altogether and just manually place it in your web root.
If there are only a couple of these files, I guess you could do the following:
Create URLs for each of the files you want to serve
Hook those URLs up to a view that returns the file with the right content
refer to this snippet for an example view
I need a component that's a browser-based file browser, and I expect some django app to currently provide this. Is there such a thing?
The full story:
I'm building a django app that is used for testing. I want to use it to serve files (and strings, and etc.) and attach custom headers to it.
Currently, I have a model FileSource which has a single file_path field, which is of type django.db.models.FileField.
When creating a FileSource from the admin, the user has a nice file upload dialog, and when saving, the file he chose, is saved on the server (in a really weird location, inside the directory where django is installed, or something weird like that, because i didn't customize the storage, nor will it help me in any way)
My problem: I only want to use the file dialog for the user to select a full path on the server. The file that the user chose must be only referenced, not copied (like currently), and it must reside on the server.
The server must thus be able to list the files it has, so i basically need a little browser-based file-browser.
At that point, I expect to be able to save a full path in my DB, and then I'll be able to access that file and serve it (together with whatever custom headers the user will chose from my app).
Currently, as you might know, the browsers always lie about the full path of the file. Chromium appends "C:\fakepath" to the file name, so I need support of the backend to accomplish this.
Also, I checked out django-filebrowser and django-filer and from what I understood, they weren't built for this. If I'm wrong, a little assistence in configuring them would be awesome.
You can use a FilePathField for that. It won't upload a file, but rather allow you to choose a pre-existing file. A caveat is that you can only use one directory. If you need multiple directories, then you'd need do go with something like django-filer.
I finally managed to get my web-server up and running python.
I am now able to enter the direct url pointing to the python script and it is being executed.
I wrote a script which generated a html page and returns it - working; and if string is saved as html and opened, it is displayed exactly how I want it.
However, I want this page to be displayed if a user navigates to the "Database" link which is displayed in the navigation menu
<nav>
<ul class="main-nav">
**<li>Database</li>**
<li>About</li>
<li>Admin</li>
</ul>
</nav>
I would like the script to be executed when the user clicks on the menu link but I do not want the user to see the actual link to the python script.
Ie the link should display localhost/database.html but the script should be executed and whatever is returned should be displayed.
I hope this makes sense - any help is appreciated
The easiest way to achieve what you want (the end user not seeing that its not a HTML file) is to move the file into a folder and configure the server to execute and server the results of that file when the user navigates to that folder, i.e. move the file into a folder called:
/database
and then change your link to point there, then configure the webserver to accept the python filename as a default file, in your case running apache it should be something like this:
Find the DirectoryIndex directive in your Apache configuration file (httpd.conf) or add it to a .htaccess file and change it to look like this if you want to limit your default index file to just index.html:
DirectoryIndex index.html
You can also include more resources and they will be used in the order given, e.g
DirectoryIndex index.html index.php
would display the index.html file first if both index.html and index.php existed.
Don't forget to restart Apache if you made the change to the httpd.conf file.
this way the url that you would see when testing locally would be:
localhost/database
and avoids putting any extension on it, this is simpler that trying to do some kind of redirection.
personally when using pages that do something like perform a query i tend to call them process to indicate that they actually do something when i'm working on it, and static files i tent to call index (unless there are multiple files in that location) this isn't necessary, just my personal preference, but it helps keep the configuration simple as there are then only so many names you have to add to the default list.
I have this file input:
<input type="file" name="file" id="file_id" />
and I want to upload a file from local machine (C:\my_file.xls) to the server, but the problem is when I want to upload the file.
It has writen a fakepath to the file and I have got this message when uploading:
No such file or directory: u'C:\\fakepath\\my_file.xls'
I knew that it has relation with browser security feature, and I have tried to make some solutions for that like creating a folder with the name fakepath or delete this word from the given path...
Is there an other (elegant or efficient) way to get file's local full path in the input (the real path) for the file ?
You cannot actually get the file path from the client side. That just wont work.
You have to change how you think - the client is completely separate from the server. What you do on one is only partially related to the other (and good thing, too!)
What you need to do is decide where on your server you want to store files. And then put them there. If you want to be able to download the files that have been uploaded, you have to decide who can access the files, how you're going to list them, etc.
But these should be completely unrelated to the information you get from the client - the only thing you should get from the client is the data contained in the file (and maybe a filename).