Home > front end >  How this code upload file in django can run the html file?
How this code upload file in django can run the html file?

Time:01-06

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

CodePudding user response:

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.

  • Related