Python – FullStack
Creating Templates:
**********************
[Link]
TEMPLATES = [
{
'BACKEND': '[Link]',
'DIRS': [[Link](BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'[Link].context_processors.debug',
'[Link].context_processors.request',
'[Link].context_processors.auth',
'[Link].context_processors.messages',
],
},
},
]
[Link]:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<h2>Welcome to Django!!!</h2>
</body>
</html>
[Link]
from [Link] import render
#importing loading from django template
from [Link] import loader
# Create your views here.
from [Link] import HttpResponse
def index(request):
template = loader.get_template('[Link]') # getting our template
return HttpResponse([Link]()) # rendering the template in
Django Variables Example:
[Link]:
from [Link] import render
#importing loading from django template
from [Link] import loader
# Create your views here.
from [Link] import HttpResponse
def index(request):
template = loader.get_template('[Link]') # getting our template
name = {
'student':'rahul'
}
return HttpResponse([Link](name)) # rendering the template in
HttpResponse
[Link]:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<h2>Welcome to Django!!!</h2>
<h3>My Name is: {{ student }}</h3>
</body>
</html>
*************************8
Tags:
{% csrf_token %}
{% if user.is_authenticated %}
Hello, {{ [Link] }}.
{% endif %}
*********************************************
Django URL Mapping:
[Link]:
from [Link] import admin
from [Link] import path
urlpatterns = [
path('admin/', [Link]),
]
Django URL Mapping:
// [Link]
from [Link] import render
# Create your views here.
from [Link] import HttpResponse, HttpResponseNotFound
from [Link] import require_http_methods
@require_http_methods(["GET"])
def hello(request):
return HttpResponse('<h1>This is Http GET request.</h1>')
//[Link]
from [Link] import admin
from [Link] import path
from myapp import views
urlpatterns = [
path('admin/', [Link]),
path('index/', [Link]),
path('hello/', [Link]),
]