0% found this document useful (0 votes)
2 views3 pages

Django_730am - Copy

The document provides a guide on setting up a Django project with REST API, including instructions for creating applications, views, and URL routing. It emphasizes the importance of separating HTML code into templates for better readability and reusability. Additionally, it covers the MVC and MVT design patterns, and how to set up template directories programmatically in Django settings.

Uploaded by

kumargpc7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Django_730am - Copy

The document provides a guide on setting up a Django project with REST API, including instructions for creating applications, views, and URL routing. It emphasizes the importance of separating HTML code into templates for better readability and reusability. Additionally, it covers the MVC and MVT design patterns, and how to set up template directories programmatically in Django settings.

Uploaded by

kumargpc7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Django with Rest API @ 7:30 AM IST by Mr Mahesh

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),
]

Create another project:


-----------------------------------
D:\DJANGO_23JAN_730AM>django-admin startproject maheshproject

-->Copy testapp from applevelurlsproject and paste it in current project, then


include app level urls in project level urls.

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/

CHAPTER-3:Django Templates & Static Files


=======================================
Django Templates:
---------------------------
-->It is not recommended to write html code inside python script(views.py file)
because:
1).It reduces readability because python code mixed with HTML code.
2).No separation of roles. Python developers has to concentrate on both
python code and HTML code.
3).It doesnot promote re-usability of code.
-->We can overcome these problems by separating HTML code into separate html file.
This html file is nothing but template.
-->From python file(views.py) we can use these templates based on our requirement.
-->We have to write templates at project level only, we can use these templates in
multiple applications.

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.

MVC design pattern:


-------------------------------
M-->Model(Business logic)
V-->View(Presentation logic)
C-->Controller(Co-ordination)

MVT Design pattern:


------------------------------
M-->Model(DB tables)
V-->View(Python files)
T-->Template(HTML files)

Template Based Application:


-------------------------------------------
1).django-admin startproject templateproject
2).py manage.py startapp testapp
3).Add app in settings.py

4).Create a 'templates' folder inside main project folder.


In that templates folder create a separate folder namsed with 'testapp' to
hold that particular application specific templates.

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'

-->Specify this TEMPLATE_DIR inside settings.py


'DIRS': [TEMPLATE_DIR]

6).Create html file inside templateproject/templates/testapp folder. This html file


is nothing but template.

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)

You might also like