Python-Django Tutorial: Database and admin interface

Part 2 – Database and admin Interface

Django is a Python-based, free, open-source web framework. it designed by Adrian Holovaty and Simon Willison on July 2005. In this tutorial, (Python-Django Tutorial: Database and admin interface) we focus on How to work with Database using Django

Part 1 – Python-Django Tutorial: Tutorial for Beginners.

Documentation: https://docs.djangoproject.com/en/2.2/

Database Configuration

Enter the following command in cmd and press enter to migrate the database.

python manage.py migrate

Create new Models

Open the file blog/models.py and add following code.

from django.db import models

# Create your models here.
#author_Model
class Author(models.Model):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=50)
    username  = models.CharField(max_length=50)

    def __str__(self):
        return self.username + " - " + self.fname + " " + self.lname #return data
    
"""
CREATE TABLE `blog_author` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `fname` varchar(50) NOT NULL, `lname` varchar(50) NOT NULL, `username` varchar(50) NOT NULL);
"""

#post_Model
class Post(models.Model):
    title = models.CharField(max_length=50)
    content = models.CharField(max_length=500)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    date_posted = models.DateField()
    date_updated = models.DateField()

    def __str__(self):
        return str(self.id) + " - " + self.title + " " + str(self.date_updated) #return data

"""
CREATE TABLE `blog_post` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `title` varchar(50) NOT NULL, `content` varchar(500) NOT NULL, `date_posted` date NOT NULL, `date_updated` date NOT NULL, `author_id` integer NOT NULL);
ALTER TABLE `blog_post` ADD CONSTRAINT `blog_post_author_id_dd7a8485_fk_blog_author_id` FOREIGN KEY (`author_id`) REFERENCES `blog_author` (`id`);
"""

To activate the model, open firstProject/settings.py and find this part,

Then enter the following code and save settings.py

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
]

Enter the following command in cmd and press enter to migrate the Post database.

python manage.py makemigrations blog

Enter the following command in cmd and press enter to migrate the new database.

python manage.py migrate

Admin Interface – Create a User Account

127.0.0.1:8000/admin

Enter the following command in cmd and press enter to create a superuser

python manage.py createsuperuser

Then enter Username and Password to Create a new User

Then visit 127.0.0.1:8000/admin and login

Username: admin
Password: admin123456

Admin Interface – Setup

Open the file blog/admin.py and add following code.

from django.contrib import admin
from .models import *

admin.site.register(Author)
admin.site.register(Post)

Github: https://github.com/ctechf/django_tutorial/

Part 2 – Completed

Sources : 
https://pixabay.com/ (images) 
https://docs.djangoproject.com/en/2.2/ (Documentation)

Hello Guys,

Hope this post ( Python-Django Tutorial: Database and admin interface) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in web development and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page: https://www.facebook.com/ctechf

Also Read :

Python Tutorials

How to be a Programmer

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

181 Shares
Share
Tweet
Share
Pin
Share