Django Apps
- A Django project will contain atleast one app
- Usually app contains models for single design domain.
- App can have any name as long as it is valid python module name
- The contents of the app
__init__.py
- admin.py: Django has a built-in admin site for viewing and editing data with a Graphical user interface.
- apps.py: This contains some configuration for the metadata of your app.
- models.py: This is where we define the models of application
- migrations: Django uses migration files to automatically record changes to your underlying database as the model chagnes. They are generated by Django when you run
manage.py makemigrations
command. They do not get applied to the database until you runmanage.py migrate
- tests.py: To test that your code is behaving correctly, Django supports writing tests (unit, functional, integration)
- views.py: Your Django views
View Details
- View created should return HttpResponse object
- To the view method the HttpRequest instance is passed to the view and it contains all the data related to the request
- method: A string continaing HTTP method the browser used to request the page
- GET: A
QueryDict
instance that contains the parameters used in the URL query string - POST: A
QueryDict
instance containing the parameters sent to the view in a POST request - headers: A case insensitive key dictionary with HTTP headers from the request
- path: This is the path used in the request
Solution to simple interest query strings
- Code:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('Welcome to Django application')
def simple_interest(request):
principal = float(request.GET['principal'])
time = float(request.GET['time'])
rate = float(request.GET['rate'])
si = principal * time * rate / 100
response_text = f"<p> principal: {principal} <br/> time: {time} <br/> " \
f"rate: {rate}<br/> Simple Interest: {si}</p>"
return HttpResponse(response_text)
-
url will be
http://127.0.0.1:8000/si/?principal=10000&time=4&rate=11
-
For setting up the Django project in pycharm please follow the recording of this class.