KEMBAR78
Django APP | PDF | Directory (Computing) | Databases
0% found this document useful (0 votes)
11 views11 pages

Django APP

This document outlines the steps to create and manage a Django project and app, including setting up a virtual environment, creating a project and app, and running the application. It also covers creating a Django model for a notes table, performing migrations, and setting up a superuser for the Django admin panel. Finally, it explains how to register the model in the admin panel to manage notes through the interface.

Uploaded by

compnetworxxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Django APP

This document outlines the steps to create and manage a Django project and app, including setting up a virtual environment, creating a project and app, and running the application. It also covers creating a Django model for a notes table, performing migrations, and setting up a superuser for the Django admin panel. Finally, it explains how to register the model in the admin panel to manage notes through the interface.

Uploaded by

compnetworxxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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

You might also like