0

I am working on a Django App on Heroku and need the possibility to temporarily store something on my dyno there. As far as I understood, this is possible by using a workaround with the /tmp/-folder in the root-directory of the dyno. However, I am trying to reference to a JSON stored there in JS and it seems like Django does not load, once something is outside of the static folder. How can I force Django to accept other folders as well? Or am I missing something very simple?

This is the error message I get from my server:

 "GET /tmp/honoradar/mediumsname_temporary.json?v=1539425460111 HTTP/1.1" 404 2731

This is my jquery code with static as a reference:

var options = {
   url: "static/honoradar/mediumsname.json?v=" + versionUpdate, 
   getValue: function(element) {
   return element.name;
   },

This is my jquery code with tmp as a reference:

var options = {
   url: "tmp/honoradar/mediumsname.json?v=" + versionUpdate, 
   getValue: function(element) {
   return element.name;
   },

For making sure that the file structure is the exact same, I duplicated the static folder and renamed it "tmp".

Haluka Maier-Borst
  • 75
  • 1
  • 4
  • 13

1 Answers1

0

There are at least two things wrong with what you are trying to do here.

Firstly, you are confusing file system paths with URLs. You can certainly store anything you like in the file system, but there is no correspondence with URLs served by your web application. The only reason /static/ works is because you have explicitly mapped it to a path on the file system; on Heroku, this is usually done by using the whitenoise application. And note that the /static/ URL does not point to the actual file path /static/, but to a directory inside your application's BASE_DIR.

If you think about this, you can see that you would not want it to work anyway. If it did, anyone would be able to get your code, including your database credentials and any other secret information on your server, just by browsing to a URL.

Secondly, you almost certainly don't want to store things on the filesystem on Heroku anyway. If you do so it will only be available on that very same dyno; that is the whole point of an ephemeral non-shared file system. If you generate the file via a separate process, it won't be available to the web process; or even if you are running your web application on more than one dyno, there's no guarantee your next request will be served by the same one that has the file.

Now, if taking all this into account you still want to try and do this, the solution is simple: store the file in a directory inside STATICFILES_DIR, and it will be available via STATIC_URL.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Dear Daniel, thanks for your answer and the clarification. So the idea for me is to create on every dyno a JSON that consists of names that I have defined earlier in a default JSON and extend it by names I find in my DB. This extended JSON should be stored and then be read out by my autocomplete function (above). I have tried to do the writing and storing process of the JSON in my "class IndexView" in my views.py. However, I cannot find it with my JS. What is the thing that I am missing? Because according to you, I should be able to write into my Static-Folder, correct? – Haluka Maier-Borst Oct 13 '18 at 20:05
  • It's not clear how you would create the file "on every dyno"; dynos are not individually addressable so I don't know how you would ensure this. But I'm also not clear what your problem is now. Are you saying the static version is not found either? If so it's probably because you don't have a leading slash: your URL should be `"/static/honoradar/..."`. – Daniel Roseman Oct 13 '18 at 20:11
  • In any case, as I said to you yesterday, this whole approach is pretty misguided. You shouldn't be trying to write a JSON file at all, you should be returning the relevant matching items dynamically in response to the Ajax call from the autocomplete widget. – Daniel Roseman Oct 13 '18 at 20:16
  • Thanks. Let me clarfiy: -We started with an empty DB. -We wanted to offer users an autocomplete for entries that we thought would be likely. This is driven by a JSON and JQuery library easyautocomplete -Now, that we have entries in DB from users that we did not expect, we want to update our autocomplete. It should offer predefined from the original JSON and unexpected names from DB. This is the new JSON I want to create and store, once user requests our page. -I want to drive this via JSON because AJAX would be creating a lot of traffic, when a static solution is enough. – Haluka Maier-Borst Oct 13 '18 at 21:14
  • Or am I am making a mistake and you would fire off the Ajax with the page load, hold the updated JSON in a variable and then use this again, once the user starts typing? – Haluka Maier-Borst Oct 13 '18 at 21:18