Steps to Setup Django in your PC with example.

 

Django Setup

Step-1. Make folder named "Admin panel". open cmd in "Admin panel" folder.

Note: you can choose any name for your folder.


Step-2. Write following command in cmd:

“python –m venv myvenv”


Step-3. Write second command:

“myvenv\scripts\activate”


Step-4. Write third command:

“pip install django”

(note-connect your pc with internet then run above command)


Step-5. Write fourth command:

“Django-admins Startproject project .


You can see the following folders are being made in folder ‘Admin panel’:

Folders: myenv, project

File: manage.py


Step-6. 5th command:

“python manage.py startapp app”


You can see ‘app’ folder

 being made in ‘Admin panel’ folder.


Step-7. 6th command:

“code .”  

(note : 1 space is between ‘code’ and ‘.’)

This command will open ‘vs-code’ app if it is installed in your pc.

(if vs-code not installed in your pc then first install vs-code)


Step-8. you can see the following folder in side panel of vs-code:

‘app’, ‘myvenv’, ‘project’.

Click  on “project”. You see some files. Click on “settings.py“ in “project”folder.

In “settings.py” file, you see the list named “INSTALLED_APPS”.

Add following element in “INSTALLED_APPS” list:

Write ‘app,’ in end of list.


Step-9.

Open ‘cmd’ in ‘Admin panel’ folder

7th command:

“python manage.py migrate”

You can see ‘Running migrations’ prints in cmd. Then, 10 to 15 lines comes in cmd that ends with ‘ok’.


Step-10.

Following this path and open ‘urls.py’:

‘ADMIN PANEL/project/urls.py’


Step-11.

Now make following named list in ‘urls.py’ file:


Find “from Django.urls import path,       

& add “include”.here -->  ---> --->  👆

 

Step-12.

Make another list in “urls.py” in ‘project’ folder:

“urlspatterns = [

              path(‘admin/’,admin.site.urls),

              path(“”,include(‘app.urls’))

                ]"

Step-12.5

Make ‘urls.py’ file in ‘app’ folder. & Write following code:

“from Django.urls import path,include

urlpatterns = [

]”

 

Step-13.

Open ‘app’ folder from “ADMIN PANEL” folder. Open ‘urls.py from ‘app’ folder.

Write following code:

“from Django.urls import path,include

urlaptterns = [

]”.


(note- if you find anything written in list ‘urlpatterns’ in ‘urls.py from ‘app’ folder then make list empty like above.)


Step-14.

Last command:

“python manage.py runserver”


Starting server at http://127.0.0.1:8000/


Run above address in browser. If all things done perfect, you see page where written:

“The install worked successfully! Congratulations!”.

The procedure of “Django setup”  is over.

 

Now make database with Django. And create 

table.

Steps:

1.    Open cmd in ‘Admin Panel’ folder.

Write following command:

“python manager.py createsuperuser”


In cmd, you see following:

Username(leave blank to use ‘kartavya’) : ”.

Write username which you want and press enter from key-board.

Now comes “Email address :  in cmd. Write email address and press enter from key-board.

Now comes “password :”. Enter 8 chars or more with one or more special chars and one or more digit. Press enter.

Note(you can't see characters while you type password. And password again.)

Now comes “password again :”. Enter password 

again and press enter. You see the following in cmd:

“superuser created successfully.”

 

2.   now go to this address in url bar in browser.

“127.0.0.1:8000/admin/login”


Note: you have to connected your pc to Internet. You see the page opens with login box. Enter username & password which you entered in 1st step.

 

3.   Now go to vs-code & open “models.py” From “app” folder in  “Admin Panel” folder.

Write following code in it.

from django.db import models 

class Teacher(models.Model):

   Firstname=models.charfield(max_length=50)

Lastname=models.charfield(max_length=50)

   Email=models.charfield(max_length=50)

Course=models.charfield(max_length=50)”

 

4.      Now find “admin.py” open it( in “app” folder).

Write following code in “admin.py”:

“from django.contrib import admin

 from.models import *

admin.site.register(Teacher)”

 

5.    Open cmd in “Admin Panel” folder. & write following commands:

1st : “python manage.py makemigrations”

2nd: “python manage.py migrate”

3rd: “python manage.py runserver”

 

6.   Now open login page in browser(url : 127.0.0.1:8000/admin/login).

You can see “Teachers” model in app folder (after login).

7.   You can add or change objects of  this model using “+add” or “change” button on screen.


ADD ANOTHER TABLE “Student”. And attache it 

with Teacher’s table’s primary key.

1 Open ‘models.py’ in ‘app’ folder. And write following code:

class student(models.Model):

teacher=models.ForeignKey(Teacher,on_delete=models.CASCADE)

Firstname=models.CharField(max_length=50)

Lastname=models.CharField(max_length=50)

Email=models.CharField(max_length=50)

def __str__(self):

       return self.Firstname”


now add following code in the end of class ‘Teacher’ in ‘models.py’ in ‘app’ folder:

“def __str__(self):

                     return self.Firstname”  

 

2.  Add following code in ‘admin.py’ in ‘app’ folder:

“admin.site.register(Student)”

 

3. Now run following command in cmd in ‘Admin panel” folder:

1st : “myvenv\Scripts\activate”

2nd: “python manage.py makemigrations”

3rd : “python manage.py migrate”

4th : “python manage.py runserver”

 

Now if you don’t find any error and all command 

successfully run.

 

4.  You will see that another table ‘Student’ is created in the webpage Url: “127.0.0.1:8000/admin/login

Note: first login then you see table-name  created in that page.

5.  Click on +add then it will show fields ‘Firstname’, ‘Lastname’, ‘Email’, ‘teacher’.


Fill this fields & click on ‘save’.

You see one object will be created.

 __________________________________________

 

Comments

Popular posts from this blog

One mind blowing question!!!

Common Interview Questions and Answers