Create New Django Project and Templates in Python Django



CREATE NEW DJANGO PROJECT


Step 1 : Create a New Folder

First, create a new folder called "DjangoApp" on your computer. Open VSCode and navigate to the "DjangoApp" folder by clicking on "File" > "Open Folder" and selecting the "DjangoApp" folder.

Step 2 : Create New Directory

Now, we will create a virtual environment for our Django project. In the terminal, type "python –m venu .venu" and press Enter. This will create a new directory named "venv" that will contain the virtual environment.

Step 3 : Activate Virtual Environment

Now, Activate the virtual environment. In the terminal, type ".\venv\Scripts\activate" and press Enter. This will activate the virtual environment.

Step 4 : Select Python Interpreter

Open the VSCode view by clicking on "View" > "Command palette > Python: Select Interprete" Select the Python interpreter you want to use for your project, and VS Code will set it as the default interpreter

Step 5 : Install pip Package

In the terminal, type "pip –m install" and press Enter. This will install the pip package on your Django Project.

Step 6 : Install Django Package

In the terminal, type "pip –m install Django" and press Enter. This will install the Django package on your "venu" folder.

DjangoApp/home/views.py
from django.shortcuts import render
from django.http import HttpResponse
 
 
def home(request):
    view_users=[
        {"name":"Joes","age":35},
        {"name":"Tiya","age":12},
        {"name":"Suresh","age":22},
        {"name":"Rajesh","age":45},
    ]
    return render(request, "index.html",context={"users":view_users})
DjangoApp/core/urls.py
"""
URL configuration for core project.
 
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from home import views
 
urlpatterns = [
    path('', views.home),
    path('admin/', admin.site.urls),
]

TEMPLATES IN PYTHON DJANGO


DjangoApp/home/templates/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        border-collapse: collapse;
        margin-top: 10px;
      }
      table,
      td,
      th {
        border: 1px solid #222;
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <h1>Tutor Joes</h1>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Nam reiciendis modi officiis ratione molestiae beatae porro vel quos! Dolorem omnis esse dolorum sequi incidunt illo distinctio molestias sapiente odit doloribus.</p>

  {% comment "Notes" %}
  {{users}} 
  {% endcomment %}

    <table>
      <tr>
        <th>S.No</th>
        <th>Name</th>
        <th>Age</th>
        <th>Eligible</th>
      </tr>
      {% for user in users %}
      <tr>
        <td>{{forloop.counter}}</td>
        <td>{{user.name}}</td>
        <td>{{user.age}}</td>
        <td>{% if user.age >= 18 %} Yes {% else %} No {% endif %}</td>
      </tr>
      {% endfor %}
    </table>
  </body>
</html>

COMMENT TAG IN DJANGO TEMPLATE


In Django templates, you can use comment tags to add comments that are ignored by the template engine and not rendered in the final output. Comment tags are useful for documenting your template code or temporarily disabling parts of the template without removing them entirely.

To add comments in Django templates, you can use the {% comment %} template tag. Here's the syntax:

{% comment %}
    Your comment goes here
{% endcomment %}

You can place any text or notes within the comment tags, and they won't be displayed when the template is rendered.