Python

champak
₹2,000.00 ₹1,500.00
The Python Programming Language at Champak's World
  • 15 students
  • 22 lessons
  • 0 quizzes
  • 10 week duration

Python

How to add a simple view to a Django site?

How to add a view to a DJango Site.

 

Create a file called views.php in the site installation under DJango.

This is the code written in the file.

views.py

from django.http import HttpResponse
def index(request):
html = “<h1>Champak View</h1>”
return HttpResponse(html)

We define a function def index(request).
This is the default function for the url. It receives an object of type HttpRequest. We are not accepting anything from the request here. We simply create a html text and return it by wrapping it in the HttpResponse.

 

Then we add the new url to urls.py

 

urls.py

from django.contrib import admin
from django.urls import path
from .views import index
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘hoopla/’, admin.site.urls),
path(‘lafda/’, index),
path(”, index),
]

Go to the url and you can have a view.

http://127.0.0.1:8000

 

end

Leave a Reply

Your email address will not be published. Required fields are marked *