Django Admin Panel

 Django Admin

* Django provides a ready-to-use user interface for administrative activities. We all know how an admin interface is important for a web project. Django automatically generates admin UI based on your project models.

Starting with Admin Interface
* First create a project in django.
* Create application in django.
* Install application in django project.
* Include application url in django project urls.py file.
* Create one file in application and name it urls.py file.

Create Django Framework using following command: 


Create New Folder= folder name(Hrsikh)

Hrsikh>python -m venv env
Hrsikh>env\Scripts\activate
(env) Hrsikh>pip install django
(env) Hrsikh>django-admin startproject projectname .
(env) Hrsikh>python manage.py startapp appname
(env) Hrsikh>cd projectname
(env) Hrsikh\projectname>python manage.py migrate
(env) Hrsikh\projectname>python manage.py makemigrations
(env) Hrsikh\projectname>python manage.py runserver 

Migrate Django Tables

1) Before launching your server, to access your Admin Interface, you need to initiable the database: python manage.py migrate
2) Sync dB will create necessary tables or collections depending on your dB type, necessary for the admin interface to run. Even if you don't have a superuser, you will be prompted to create one.

Create Superuser
1) Now create super-user login for the admin panel.
2) Without creating super-user you cannot access django admin panel.
3) For creating super-user follow following command in CMD: python manage.py createsuperuser

Django models

1) A model is a class that represent table or collection in our DB, and where every attribute of the class is a field of the table or collection. Models are defined in the app/model.py (in our example: app1/models.py)
2) How to create a models in django
Structur e of models:
class ModelName(models.Model):
             var_name=models.Datatype_name(attributes)

Models Makemigrations

1) After creating a models in django, you have fire this command in CMD and VSCode: python manage.py makemigrations
2) After doing makemigrations, do migrate.

How to Add models in Django Admin Panel
1) To add models in admin panel, you have to register your models in Admin.py file.
2) Write this following line in Admin.py file to register your models. admin.site.register(model name)

#models.py
#Admin.py
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!