Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Django Templates

    Working on Django Templates is one of the favorite parts of any front-end developer of a website. Django Templates are the Django’s way of serving the pages to the web server in its own unique way. While deploying our first app, we returned an HTTP Response to the client, which was a string in Python that was parsed as HTML by a web browser.

    Need of DjangoTemplating

    What if our project layout is too big and for every function, we have a fully designed webpage? Now consider the size of web pages of today’s time, some can range to 500 lines of code easily with the migrations of Stylesheets and JavaScript code.

    Imagine putting all that code for each function in a single view file of the application. It will be a very complex task for front-end developers.

    Drawbacks of the Traditional MVC Pattern

    1. Many big companies like Amazon have different departments for the website like frontend department, backend, database administrator, etc. Now, here the main issue is that the front-end developer would need to learn the Python too. Thus, leading to a lengthy process and slower development.

    2. The approach contradicts Django’s design philosophy of loosely coupled, i.e. different components of code modifies with little or no change to other components.

    3. Website design (HTML, CSS, etc) change much more frequently than the backend, but with this approach, the frontend developer will need to assign the Python variables in the new design again and that means he is creating the website again. That’s a different story whether he knows python which itself is a skill required.

    These problems are serious and need to get solve while also retaining the MVC architecture benefits.

    The Django templates are the answer to this approach, they implement the loosely coupled design philosophy as one of the best implementations in the Industry.

    Also, Django templates are much easier to use, when compared to other popular frameworks and are quite reliable.

    Django is a web framework and that makes it really important to serve HTML pages dynamically, so to achieve that, Django has a special component, which is Django Templates.

    What are Django Templates?

    Django templates are a combination of static HTML layout and Django Syntax which is essentially Python code. Both of these components help in generating HTML pages dynamically and make your website more user-engaging.

    The main purpose of Django templates is to separate the data representation with the data itself. That means, the browser part is only to render the HTML sent to it by the server, and all the relevant data is given to the template by Django itself. This makes the process much easier and pages render easily as there is less clutter in both the front-end and back-end.

    Creating Your First Django Templates

    A Django template defines various template tags and placeholders, which are used to make the layout and how the data should be placed in the HTML. Also, Django templates are capable of displaying more than HTML, they can generate pdf, CSV and other popular formats.

    Django templates use certain tags, first, we will link our Django templates with the views.

    Step 1. Open the views.py file created from our previous tutorial.

    There was a function that we imported which was rendered in Django Views Tutorial.

    Code:

    from .models import Student
    defstudent_show(request):
    student = Student.objects.order_by('roll_no')
    return render(request, 'student/student_show.html', {'student': student})
    

    Step 2. Create the Django template rendered in views.py file

    Make a new directory named student same as the application name, and inside that create one HTML file named student_show.html which we also passed in the render function.

    HTML Code:

    < !DOCTYPE html>
    < html>
    < head>
    < title>Student_Show< /title>
    < /head>
    < body>
    < h1>The Students Here at Dataflair< /h1>
    < div>
    < ul>
    {% for stud in student %}
    < li>
    < h2>Name {{ stud.name }}< /h2>
    < p> department {{ stud.department}}< /p>
    < p>class {{ stud.standard}}< /p>
    < /li>
    {% endfor %}
    < /ul>
    < /div>
    < /body>
    < /html>
    

     

     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.