django
django
Tags
Fundamentals
A batteries included framework: it comes with a lot of features.
Admin interface
Object-relational Mapper
Authentication package
we can give API end points on the server to get the data
Setup
install python from browser
First project
pipenv install django
Django 1
the browser makes a socket connection to the server and then requests
something from the server. The server sends out information ( htmsl / json or
anything else) what the browser recieve is usually what ends up as a display
page.
Network sockets
http: invented n the 1990
Request for Comments
the client makes the first move.
we send out html tags to the browser and the browser renders this html.
Django 2
the views file can contain all views we define
HTML_STRING ="""
<h1>Hello world</h1>
"""
def home_view(request):
return HttpResponse(HTML_STRING)
Django 3
💡 startapp:
creating a piece of the current django project
Models:
Reference
when we create and edit models, we should let django know about these actions.
Migrations are run to do that
Migrations
Django 4
Doing it with code:
article_obj=Article.objects.get(id=2)
HTML_STRING =f"""
<h1>{article_obj.Title}</h1>
<p> {article_obj.content}</p>
"""
def home_view(request):
return HttpResponse(HTML_STRING)
Django templates
templates can be added as html files.
the html file can be rendered to a string with django’s inbuild feature.
Django 5
from django.template.loader import render_to_string
Inheritance template:
templates can inherit from other templates .
<!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>
<h1>Title from base</h1>
{% block content %}
<h3>placebolder</h3>
{% endblock content %}
</body>
</html>
the block content is what gets replaced by the templates that inherit from this
base template
The child inheriting would look like this:
Django 6
{% block content %}
<h1>{{ title }} </h1>
<p> {{ content }}</p>
{% endblock content %}
article_queryset= Article.objects.all()
the above code takes out all the objects of Article and gives us the list.
to render this we can pass the object as an element in the context and call it in the
view
{% for x in object_list %}
<li>
<a href="articles/{{x.id}}">{{x.Title}} </a></li>
{% endfor %}
for loops and any other logical statements can be performed within the template
Django 7
Dynamic Routing
Context processors
Deplaying Apps:
Serializers :
In DRF, serializers convert Django model instances into JSON format and vice
versa.
Django 8
from django.shortcuts import get_object_or_404
from .serializers import SolutionSerializer, QuestionSerializer, GradeSerializer, Su
class CRUDDetailView(APIView):
model = None
serializer_class = None
Django 9
serializer = self.serializer_class(instance, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SolutionDetailView(CRUDDetailView):
model = Solution
serializer_class = SolutionSerializer
class QuestionListCreateView(CRUDView):
model = Question
serializer_class = QuestionSerializer
class QuestionDetailView(CRUDDetailView):
model = Question
serializer_class = QuestionSerializer
class GradeListCreateView(CRUDView):
model = Grade
serializer_class = GradeSerializer
class GradeDetailView(CRUDDetailView):
model = Grade
serializer_class = GradeSerializer
Django 10
class SubjectListCreateView(CRUDView):
model = Subject
serializer_class = SubjectSerializer
class SubjectDetailView(CRUDDetailView):
model = Subject
serializer_class = SubjectSerializer
class ChapterListCreateView(CRUDView):
model = Chapter
serializer_class = ChapterSerializer
class ChapterDetailView(CRUDDetailView):
model = Chapter
serializer_class = ChapterSerializer
class BoardListCreateView(CRUDView):
model=Board
serializer_class = BoardSerializer
class BoardDetailView(CRUDDetailView):
model = Board
serializer_class = BoardSerializer
Django 11