Django_730am - Copy
Django_730am - Copy
Day-1 https://youtu.be/7fhwr-1ov2g
Day-2 https://youtu.be/n-i9hIbPwpo
Day-3 https://youtu.be/AL_B9MIc7eQ
Day-4 https://youtu.be/YytTjkl3HsU
Day-5 https://youtu.be/aNBlhZC7TWU
-------------------------------------------------------------------------------
Ex:
-----
D:\DJANGO_23JAN_730AM>django-admin startproject applevelurlsproject
D:\DJANGO_23JAN_730AM>cd applevelurlsproject
D:\DJANGO_23JAN_730AM\applevelurlsproject>py manage.py startapp testapp
views.py
-------------
from django.http import HttpResponse
def exams_view(request):
return HttpResponse('<h1>Exams View</h1>')
def attendance_view(request):
return HttpResponse('<h1>Attendance View</h1>')
def fees_view(request):
return HttpResponse('<h1>Fees View</h1>')
urls.py(Application level)
-------------------------------------
from django.urls import path
from . import views
urlpatterns = [
path('exams/',views.exams_view),
path('attendance/',views.attendance_view),
path('fees/',views.fees_view),
]
urls.py(project level)
-------------------------------
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('testapp/', include('testapp.urls')),
]
http://127.0.0.1:8000/testapp/exams/
http://127.0.0.1:8000/testapp/fees/
Python stuff:
--------------------
pathlib ---> module name
Path --> class anme
pathlib module provides various classes representing file system paths based on
different operating system.
Ex:
from pathlib import Path
print(__file__)#It returns the name of the file:test.py
fpath = Path(__file__)
print(type(fpath))#<class 'pathlib.WindowsPath'>
complete_path = fpath.resolve()
print(complete_path )#D:\Mahesh_Classes\test.py
print(Path(__file__).resolve().parent)#D:\Mahesh_Classes
print(Path(__file__).resolve().parent.parent)#D:\
Note:
The main advantage of this approach is we are not required to hard code
system specific paths(locations) in python script.
5).Add templates folder to settings.py file so that django can aware of our
templates.
TEMPLATES = [
'DIRS': [D:\DJANGO_23JAN_730AM\templateproject\templates],
]
-->It is not recommended to hard code system specific location in settings.py file.
To overcome this problem, we can generate templates directory path programmatically
as:
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = BASE_DIR/'templates'
wish.html
---------------
<body>
<h1>Welcome To Django Templates Demo</h1>
<h2>Second hero of django in MVT:Templates</h2>
</body>
7).views.py
----------------
def wish(request):
return render(request,'testapp/wish.html')
urls.py:
path('test/',views.wish)