Templates in Django

 How to create Template in Django?
1) It is possible using Django to separate the coding of HTML and Python. Until now, you must be clear with the fact that Python goes in Views. On the other hand, HTML goes in templates. Now, to link these two separately coded modules, there is a need to rely on the render function. Here, you will also be using Django Template Language.

2) For loading the template on browser you have to create one function in app/views.py file.
def function_name(request):
        return render(request,"app/index.html")

#index.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=h1, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello hrsikh</h1>
</body>
</html>

#views.py
from django.shortcuts import render

# Create your views here.
def Index(request):
    return render(request,"index.html")

#urls.py
from django.urls import path,include
from .import views
urlpatterns = [
    path("",views.Index,name="index"),
]


HTML Form 
1) An HTML form is used to collect user input. The user input is most often sent to a server for processing.
2) The HTML <form> element is used to create an HTML form for user input.
<form>
form element
</form>

#forms.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <center>
        <h1>HRSIKH</h1>
        <form action="" method="post">
            <table>
                <tr>
                    <td>Firstname</td>
                    <td><input type="text" name="" id=""></td>
                </tr>
                <tr>
                    <td>Lastname</td>
                    <td><input type="text" name="" id=""></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><input type="text" name="" id=""></td>
                </tr>
                <tr>
                    <td>content</td>
                    <td><input type="text" name="" id=""></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

#view.py
from django.shortcuts import render

# Create your views here.
def form(request):
    return render(request,"forms.html")

#urls.py
from django.urls import path,include
from .import views
urlpatterns = [
    path("",views.form,name="form"),
]


Output: 


Form Method
There are two methods for sending the data on server:
1) Get method
2) Post method

A) GET Method
1) GET is used to request data from a specified resource.
2) GET request can be cached.
3) GET request remain in the browser history.
4) GET request should never be used when dealing with sensitive data.
5) GET request have length restrictions.

B) POST Method
1) POST is used to send data to a server to create/update a resource.
2) POST requests are never cached.
3) POST requests do not remain in the browser history.
4) POST requests have no restrictions on data length.

C) QRM Queries Methods
1) Create(): This method is used to create the object(insert data) in Django.
2) All(): This method is used to show all the objects(fetch all data from tables) in Django.
3) Get(): Returns a single object. Throws an error if lookup returns multiple objects.
4) Filter(): Filter return a single object single field.
5) Save(): This method is used to update the object data in Django.
6) Delete(): This method is used to delete the object in Django.
m
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!