I have an excel sheet . I need to get the data , so I used open pyXl . But now , I don't know how to display that data. How do I use it in my html?
CodePudding user response:
To get an answer to your question, you first need to understand how Django works.
Django follows the MVC (Model, View, Controller) design pattern closely, but differs from it slightly. Django instead uses a design pattern called MVT (Model, View, Template).
In essence, Django Views are called Templates and Controllers are called Views.
In your Django application, you have to create a view. Inside of the view you code the logic behind a given functionality of your application. In simple terms, a view takes a request, performs some business logic and returns a response. This response can be the HTML content of a web page, a redirect, or an error.
When users access a page within your web application, they do so by following a given URL. That URL is mapped to your View. Your view returns a HTML template.
In the view which is corresponding to your wanted URL, you perform the logic of getting the data from an Excel spreadsheet. You then pass this data to the HTML template which users can see. In order to pass data to a template, Django uses The Django template language (DTL).
The DTL has it's own syntax which you have to learn in order to use it. A very basic example of passing data from your view to a given template would look like so:
from django.shortcuts import render
def foo(request):
return render(request, 'FooBar.html', {'your_data': 'Hello World'})
In the above example, we have a view called "foo". foo takes a request as a parameter and returns the request, along with a HTML template called "FooBar.html", and also a dictionary with a key of "your_data" that corresponds to the value of a string "Hello World".
In FooBar.html, you have to use the syntax of DTL to access the value. For example, to access the string "Hello World", you can do so in the following way:
<!DOCTYPE html>
<html lang="en">
...
<body>
<h1>{{ your_data }}</h1>
...
</body>
</html>
Reading through the documentation for DTL, you will be able to figure out how to pass other data structures, such as lists etc.