Getting started with Django
Published: 2024-01-05 19:48:03

****In this article, I tried to put my work experience as someone who is new to Django and trying to complete her academic training.
#######################################################
1) cd ... (P) (the folder where we want to save the project)

2) virtualenv NameEnv (name for isolated environment)

3) NameEnv\Scripts\activate

4) pip install django

5) django-admin startproject 1_ProjectName.
(P where we are
  that's mean
  Creates a new folder (P1)

6) python manage.py startapp 2_projectName() = (P1 is located next to the P2 folder)

7) in P1 setting.py add:
INSTALLED_APPS = [ '2p.apps.2PConfig', ]

8) add this code in views.py(p2):
from django.http import HttpResponse

8_1) def Def_Name(request):
     return HttpResponse('TEXT')

8_2) def Html_tag(request):
     return render(request, 'hello.html', {'name': 'shiva'})

8_3_def Html_index(request):
    return render(request, 'index.html')

9) Create urls.py in p2
add this code:
from django.urls import path
from. import views

urlpatterns = [
     path('Name_url/', views.Def_Name],
(Name_url=we put a name of our choice)

10) in urls.py P1
add this code:
from django.urls import path, include
urlpatterns = [
    path('p2/', 'include(p2.urls))']

11) in TERMINAL: python manage.py runs server

12)in URL chrome: http://127.0.0.1:8000/P2/Name_url/











SQLite3 Database:

13) python manage.py migrate

14) add this code in models.py(p2):
14_1) class Name_Class(models.Model):
name = models.CharField((max_length=200)

14_2) class Name_2(models.Model):
list = todolist = models.ForeignKey (Name_Class, on_delete=models.CASCADE)

14_3) Model of different types of fields for:
1. IntegerField - for integer values
2. CharField - for short text strings
3. TextField - for longer blocks of text
4. BooleanField - for boolean values (true/false)
5. DateTimeField - for date and time values
6. FloatField - for floating point numeric values
7. ImageField - for image files
8. FileField - for any type of file
9. ForeignKey - to establish relationships with other models
10. ManyToManyField - to create many-to-many relationships with other models.


15) python manage.py makemigrations p2

16) python manage.py migrate

17) Add to django database through python shell:

17_1) from P2.models import Name_Class, Name_Class_2
17_2)t = Name_Class(name="shiva\'s List")
17_3)t.save()
17_4) Name_Class.objects.all()




Admin Dashboard:

18) python manage.py shell

19) Add to python shell:
19_1) from P2.models import Name_Class, Name_Class_2
19_2) filter object:
t.filter(name__startswith="salva")
or
t.filter(id=2)

20) To delete an object from a Django database:
20_1)del_object = t.get(id=1)
20_2) del_object.delete()


21) Creating an admin for the login page:
python manage.py createsuperuser

22) add this code in admin.py p2:
from .models import Name_Class

# Register your models here.
admin.site.register(Name_Class)



Templates & Custom HTML:

23) in urls.py P2:
path("/", views.home , name="home"),

24) in views.py P2:
def ClassModels(response, id):
     ls = Author.objects.get(id = id)
     return render(response, "main/base.html", {})

def home(response):
     return render(response, "main/home.html", {})


25) create folder in main/temple/main

26) create base.html in main/temple/main and add this code:
26_1) <h1 > Base Template </h1>

26_2) {% block Name_block %}
other text
{% endblock %}
27) create home.html and add this code:
{% extends 'main/base.html' %}

{% block Name_block %}
Home
{% endblock %}




















Add debug toolba in project:



14) in TERMINAL vs code:
python -m pip install django-debug-toolbar

15) in settings.py project:
2_1) INSTALLED_APPS = [
     # ...
     'debug_toolbar',
     #...
]
2_2) MIDDLEWARE = [
     Enter # ... in the first line
               "debug_toolbar.middleware.DebugToolbarMiddleware",
     #...
]
2_3) INTERNAL_IPS = [
     #...
     "127.0.0.1",
               #...
]

16) in urls.py project:
3_1) import debug_toolbar

3_2) urlpatterns = [
     # ...
     path("__debug__/", include("debug_toolbar.urls")),
]