You are on page 1of 4

THIS IS DURING DJANGO – 2.

python manage.py help

1. Saw PK Django for few minutes and came here. Lol


2. Created a folder named projects and a virtual environment variable.
3. Inside projects folder , created a django project named telusko.
4. Created an app named calc and doing evenything similar to Mosh Django Tutorial.
5. In urls.py while doing urlpatterns :

from django.urls import path


from . import views

urlpatterns = [
path("", views.home, name="home") // That name keyword attribute
is not specified in
Mosh Tutorial
]

6. In Mosh he placed the templates inside the app but here we have created a folder
called templates and static in the same folder where our main app and other apps are
and created a folder called calc (appname) and then placed the home.html file into that
calc ( Also in Documentation they have placed the templates, static folders inside each
apps and not as a common folder ). In views.py we have referred it as calc/home.html
and not as home.html (this also works) alone. In the same folder where our apps and
main project are . We need to do some changes to say django to look into that
templates because usually in looks for the templates folder whether it is inside the apps
and it wont check if the templates is outside the apps folder (as far as I know). Mostly
see Telusko Templates for path (might help)

1. Open settings.py in main project

2. For templates :
Import os
There will be a list called TEMPLATES in that type 'DIRS': [
os.path.join(BASE_DIR, "templates") ]
Here, after doing this command , django will took for templates folder not
only inside all the apps as it will do by default, but also from the root of our
project( BASR_DIR ) ( See telusko for more )

3. For static :
Scroll down till last type : STATICFILES_DIRS = [
os.path.join(BASE_DIR, "telusko/static") //“mainapp/foldername”
] // We can write many os.path. command to say django about the
location of static files to search for.
STATIC_ROOT = os.path.join(BASE_DIR, "assets")

Here , after doing this command, django will collect all the files in this static
folder and it will put them in a particular folder you specify ( assests in this
case ). But for that we need to say django to create a folder named assests.

For that in cmd :


python manage.py collectstatic

This will collect all files inside static folder and put into assets ( in this
case ) folder

4. Then in our html page:

1. Type {% load static %} to load those contents( css, js, images


etc., ) from that assests (in this case) folder.
2. Enclose each src, href which refers to any files in that assets
(in this case) folder with {% static ‘ link in string form ’ %}

Example :
Change src=”images/destination.jpg as src="{% static
'images/destination_1.jpg' %}

7. PROGRAMS USING FORM :


1. Created a form to get two numbers . When we submit the form it will go to
the link provided in the action attribute of the form.
2. So we have to do something in urls.py and views.py to send a page for that
link named add (in this case)
3. See the def add function in views.py
4. See the urlpatterns in urls.py
5. See the {% csrf_token %} inside the form . We must write that word for
protection. Its mandatory.
6. Form takes two numbers and displays their sum in new page.
7. Created an app named travello and then downloaded some html, css, js
files from net ( link is in the video description ).
8. Created a folder named travello inside templates folder.
9. Added a file named index.html into our templates/travello folder
10. Designed index.html , added some cards and created a model in
models.py to set some data dynamically for the contents of the card.

8. Object-Relational Mapper (ORM) , Getting Data from DataBase and Displaying :


We are mapping objects with relations ( tables ) in DataBase by which we can create our
tables with attributes in our relational database automatically by looking at the classes
we have created, without actually typing SQL Queries but by making the Framework to
create it for us. ( Don’t know much )
We are using postgres database here.
1. Download postgres DataBase and its DBMS.
2. To Connect out project with our DataBase, write this code in settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'telusko',
"USER" : "postgres",
"PASSWORD" : "1234",
"HOST" : "localhost"
}
}
3. pip install psycopg2 - Database adaptor between python and postgres
as they both are different softwares so we need this connector.
4. pip install Pillow - Need this package when we are using
models.ImageField() in our models.py.

Note : img = models.ImageField(upload_to='pics') => It has a default


argument which is a folder where this image will be uploaded

5. Now we can type those two commands for migrations


6. Then we have model called Destination and we have sent that to the admin
page ( see Mosh ).
7. Now we are going to add values to that Destination model from admin
page.
8. Before that, we need to make some change to store the images in our
project. Once the user uploads the Data via admin or maybe from a form,
Its stored in DB but in case of images, its url is alone stored in DB not the
entire image. We have to make configurations to store images in our
project itself.
9. Go to settings.py and type this at bottom :
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

By doing so we are saying the location where our images will be. It will be
pics folder (We specifies this pics folder in our Model) which is in media
folder . Not sure if the media folder is automatically created or I did
10. In urls.py of main project type :

from django.conf import settings


from django.conf.urls.static import static

urlpatterns = urlpatterns + static(settings.MEDIA_URL,


document_root=settings.MEDIA_ROOT)

Don’t know why . Don’t know much about 8,9,10 point


11. For images we need to specify {{ objectname.img.url }} and not
{{objectname.img}} alone
9. Created many html pages, urls, views to get input from user and verify it and store
it in DB . Cant write everything here. Better see the project or see that tutorial .

10. For some validation like if the user name that the user has provided has already
been taken by someone else, we have inbuilt methods by which we can check
them.

You might also like