Using Django on Runnable. cant render a simple html - html

I'm trying to use django and render a simple html on Runnable. I guess I'm missing something obvious but I get the error "Template does not exist" all the time.
This is the code: runnable
Thanks in advance for your help.

You need to set TEMPLATE_DIRS var in Django Settings (Settings.py) like the code comment says, to let Django get all the templates successfully.
Put os.path.join(os.path.dirname(__file__),"templates") in the variable and import os.path in python. Make a templates folder inside your app folder, and move all the .html files there.

Related

How this code upload file in django can run the html file?

The code is here
I am confused about the html file, it looks like it was never called but it can be run and when I change upload_form.html name to upload.html, it could be an error. So how come? and how to make the program can run upload_form.html file in another name file?
This is my first question, so sorry if my question makes you confused >.<
I wish I could understand the code
The code link you have attached is using one of Django's generic views CreateView. And Django follows some conventions to decide what template to use for the view. In the case of CreateView Django tries to find the template with the name of model used by your view in lowercase and the default template_name_suffix which is _form. In your case your model name is
model = Upload
So Django tries to find a template with the name upload_form.html. And if you wish to change the default template_name you can always override it like this.
class UploadView(CreateView):
model = Upload
template_name = 'upload.html'
Besides this, you can also override the get_template_names method as well, it is sometimes useful when you have to conditionally decide the name of the template i.e you are planning to use a different HTML file for different users.
Here is the Django docs link and another very useful ref about CBVs.

Unable to update local json file in React native app

My React Native app file structure looks like this:
/assests
/json
example.json
/components
view.js
I need to take input from the user in the view.js component and store it in the example.json file for future use. I tried using react-native-fs but could not figure out how to use it to get the absolute path of example.json and the relative path kept on throwing errors. If there is some other way to do this, it would be most appreciated.
Thanks in advance for the help!!!!
Found out what the problem was. I was trying to update files that are in the app bundle and forgot that they are read-only. So I copied the files from the app bundle to the document directory and is accessible from there. I can edit and delete from there.

Need to change Python HTML Template Language

I'm working on a project in PyCharm that involves Python, Flask and Jinja code, but PyCharm isn't recognizing the Jinja code when I go to preview the html pages. I've read that I need to change my Template Language to Jinja2 in PyCharm for it to work, but I believe it's only available for the professional version.
Is there a workaround of do I need to switch to another IDE? Thanks
Found a solution. In order for the jinja code to be recognized I needed to run the python app containing Flask code in the command line:
python application.py
This will generated a url in the terminal and clicking on it opened up the html page in my browser.
Make sure that the following lines are at the bottom of your python code:
if __name__ == '__main__':
app.run()

Import behaviour when file/folder has the same name

ES6 newbie here. Maybe it's an easy question, but haven't found any useful info on this. I have modals.js file and modals folder in actions directory.
How can I tell javascript whether I want to import a file or a folder if they have same name?
My expectation is that:
import {...} from './actions/modals'
Can be treated either as modals.js file or modals/index.js. Is there any widely spread solution that will work on any bundler, that will guarantee what exactly do I require?
I understand that I can try to use '.js' extension like:
import {...} from './actions/modals.js'
But my research shown that not all builders support file extension.
I would really appreciate your help in my ES6 learning
Combining everything that was mentioned in the comments I've ended up in a way to always specify either / in the end if I refer to a folder or .js if I refer to specific file.

How can I require a directory in ES6?

I know I can require a file in ES6 like this:
require('./config/auth');
When I try to do this
require('./config/');
I get: Module not found: Error: Cannot resolve directory './config'. Why does this happen? How can I require a directory?
First of all, your requires are in NodeJS/io.js syntax, module in ES6 syntax looks like this:
import "./config/auth";
Or if you want to load something from it:
import authenticate from "./config/auth";
You can't load whole directories at once, but in Node/io.js you can create a module and then load that.
Note that as a workaround you can load a single file that in turn loads multiple files and returns their results. There is also work in progress on an asynchronous loader but that changes so often it's hard to keep track so I wouldn't rely on it just yet.
I personally use a package called require-dir. This should work for you:
import requireDir from 'require-dir';
requireDir('./config');