Django Views and Templates


  • Python
  • Django
    • 1
    • IN
    • Verified
    • Posted byPraveen
    • TimeSept. 4, 2024, 7:29 p.m.
    • StatusActive

    Snippet Description

    Explanation: Create a view: This defines a function that handles requests and returns a response. Create a template: This defines the HTML structure of your page. Render the template: The render function takes the request, template path, and context data as arguments. It replaces placeholders in the template with values from the context..

    Code

      # views.py
      from django.shortcuts import render

      def index(request):
          context = {'name': 'World'}
          return render(request, 'myapp/index.html', context)

      # index.html
      <!DOCTYPE html>
      <html>
      <head>
          <title>Hello, World!</title>
      </head>
      <body>
          <h1>Hello, {{ name }}!</h1>
      </body>
      </html>

    Comments

    Related Posts

    Send SMS using Django-Python

    Python Django

    3 Aug. 14, 2022, 4:32 p.m.
    Proud

    Nginx 502 Bad Gateway - Django

    Nginx Python Django

    13 March 17, 2022, 3:34 p.m.
    Proud

    Django Project Updating on Server

    Nginx Python Django

    14 March 17, 2022, 2:17 p.m.
    Proud

    Want to Hire our experts?

    We'll help you to grow your career and growth.
    SignUp Today