Getting started with Django Basics
Below listed are the high level steps involved to create a basic Django application.
- install python
- use venv before installing django =>python -m venv tutorial-env
- activate the venv: tutorial-env\Scripts\activate
- install django in the venv=> python -m pip install django
- check version =>django-admin –version
- Create a django project => django-admin startproject myApp
- To start the webserver =>python manage.py runserver
- From the myApp location, open cmd and type code .=> which will open vs code for this project from VSCode 1. init.py => when the proj receives a request it will understand that this is a package with the help of this init file 2.asgi & wsgi =>Both required during deployment 3.settings.py =>DB, Language, Timezone, Static, URL etc.,
- URLs.py => will contain the list of urls used for the project
- outside of myApp, db.sqlite3 will be used by default as a lite weight DB
- Manage.py =>Very important file
- Within the project myAPP, we can create multiple application. to create a new app => python manage.py startapp blog 1.migrations => DB related
- init => represent that it is a pkg
- admin => for admin purposes
- apps => app related config eg: name of the app etc.,
- models => contents 6.tests => used for testing the app 7.views 10.Register the app: from myApp->setting.py under Installed_Apps ->add the recently created app ‘blog’ 11.Create the first View:(in general we will receive the request and send back the response) from blog->views.py 1.import HTTPResponse => from django.http import HttpRespose a. Create a python function which take request as a parameter and return the HttpResponse=>A static string output 2.under blog, create a python file by name “urls.py” a.within that file add the urlpatterns list similar to myApp->urls.pyb.in this file, import path, and view from the project->from . import views c.to the urlpatterns list add and entry to the python function created under views.py path(“”<“” represents home directory>,views.index,name=”index”) 3.In myApp-> urls.py a. import path,include from django.urls b. under urlpatterns, add path(“”,include(“blog.urls”)) –> including the url from the blog->urls.py
- Time to test the changes. Go to the application url. it should show the content from views.py->index function
- Alternatively if we want to call the index with a seperate url a. from the myApp->urls.py-> in the urlpatterns.path -> instead of “”, provide “blogs/” b. Test the same with both default application url and url/blogs