Every Django Project you Create one or more Django App
we will look how to create them and how to manage to
them
how to run our django app
create a notes folder
cd into it
create a django project
pip install pipenv
pipenv install django
this takes a while
clear the screen
Activate this project virtualenv run
so that you will enter the main virtual environment
which python env gives us
pipenv shell
now you start working on the project
but before doing that pip install pipenev create two file
pipfile and pipfile lock
pipfile its a dependencies and the package we are using
let start work on porject
django-admin startproject notes_project .
dot notation indicates this will create a new project in a
current directory
now we have a project lets open this folder inisde the
visual studio code
code .
you can see the project directory structure
every django project you create can have one or more
Apps right now we dont have app
so lets create a APP
and to create a App
django-admin start app name of a app
djanog-admin startapp notes
so we have notes_project which is main project in which
we have settings and the configuration
then we have notes folder this is our app where we
write our main code
but before doing anything we have to register our new
app
we have to get into settings.py
check installed app
'notes' ,
now we are ready to run our application
python manage.py runserver
you will get the loopback address
copy it and open in the browser
Rocket shows which indicates that our Django
Application Successfully
summary :
Lesson 2 Starts
Create Django Models
and
Migrations
Migrate our Database
now we have our applications running now it's time to
set up our database
first we will create Django Model
and then we will run migrations
once we have that
we can start storing our Data inside our Table
in this case notes Table
lets get inside the project folder as you can see when you
create Django project by default its db.sqlite3
inside our main project directory if you see seetings.py
file scroll down to bottom
you will see Databases with default engine has sqlite3
the name is a file to our db that sqlite3
the main database where we are storing our Data
lets create our first Django Model
so fo to app folder here it s notes go to models.py
from django.db import models
create a class
class Note(models.Model):
title = models.Charfield(max_length=150)
content =models.TextField()
created_at
=models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField((auto_now=True)
so we have our tile which has 150 characters
and in contents we will stores our notes Data
in Date time field
and get time stamp when it got created
which automatically add new note to the database
and aslo how to update that
and aslo update the date time
we dont have to do it our selves
create a Method called string
def__str__(self):
return self.title
basically what it does when it call the notes object it will
default self instead of returning the note object it will
save the title
now we have our model ready
now lets create a migrations and write our database
complete code
from django.db import models
# Create your models here.
class Note(models.Model):
title = models.CharField(max_length=150)
content = models.TextField()
created_at =
models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
#create a Method
def __str__(self):
return self.title
stop the development server
cntrl c
python manage.py makemigrations
python manage.py migrate
resatrt the Development server
python manage.py runserver
lets gets into admin panel of Django
http://127.0.0.1:8000/admin
give the username and password to Django admin form
so lets configure that
stop the server
cntrl c
create user
and password
python3 manage.py createsuperuser --usrname=admin
create email
and password
now we have superuser
so that we can log into django admin panel with this
useraname and password N:\notes\notes_project\
settings.py
N:\notes\notes\models.py
N:\notes\notes\admin.py
start the develoment server
now you will get into the Django Adminsitration area
but we dont see our notes model or table
so how do we add to our notes model
so go to admin.py file
we have to import our notes model
# Register your models here.
from .models import Note
admin.site.register(Note)
so get back to admin panel we have not table here
we add new note button
where we have two field title and content
now we will see the Django rest framework