Django Introduction Example : 1 To test Python Django working Example :2 To display welcome message Example :3 To To Display Students Data In Table Format
Example 3
To display Student Data in Table Format
Goto folder e:\django
Step 1: create a project
E:\Django>django-admin startproject secondproject
E:\Django>cd secondproject
E:\Django\secondproject>
Step :2
Create the application
E:\Django\secondproject>python manage.py startapp testapp
Step:4
Now goto application folder (testapp) and add a view to display the welcome message
Views.py (in testapp folder)
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def display(request):
n=”””
<h1>Student Data</h1>
<table border=’1′>
<tr>
<th>Roll No </th>
<th>Name </th>
<th>Per</th>
</tr>
<tr>
<td>101</td>
<td>Amit</td>
<td>98.90</td>
</tr>
<tr>
<td>102</td>
<td>Sumit</td>
<td>99.90</td>
</tr>
<tr>
<td>103</td>
<td>Kishan</td>
<td>97.90</td>
</tr>
</table>
“””
return HttpResponse(n)
Step:5
Define urls in urls.py in project related to views defined in views.py in application
Urls.py (in project folder firstproject)
from django.contrib import admin
from django.urls import path
from testapp import views
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘display/’,views.display),
]
Step:6
Run the server so that project can be executed
E:\Django\firstproject>python manage.py runserver
Now put the below address in browser to get the output
http://127.0.0.1:8000/
http://127.0.0.1:8000/display/
Output:
Django Introduction Example : 1 To test Python Django working Example :2 To display welcome message Example :3 To To Display Students Data In Table Format