Setting up the Django Admin Interface for Beginners

Sulav Jung Hamal - Blog - 2023/05/12 -

Django is a powerful web framework that makes building web applications a breeze. One of its most useful features is the built-in admin interface, which provides an easy-to-use interface for managing the content of a web application. In this article, we'll walk through how to set up the Django admin interface for a beginner-friendly experience.

Step 1: Create a Django project

The first step is to create a new Django project. Open your terminal and navigate to the directory where you want to create the project. Then, run the following command:

django-admin startproject myproject

This will create a new Django project called "myproject" in the current directory.

Step 2: Create a Django app

The next step is to create a new Django app within the project. An app is a module that contains code related to a specific feature of the application. To create a new app, run the following command:

python manage.py startapp myapp

This will create a new app called "myapp" within the project.

Step 3: Create a database

Before we can use the admin interface, we need to create a database for our application. Django supports several different databases, including PostgreSQL, MySQL, and SQLite. For this tutorial, we'll use SQLite, which is a simple database that stores data in a file on disk.

To create the database, run the following command:

python manage.py migrate

This will create the necessary tables in the database for the admin interface.

Step 4: Create a superuser

The admin interface requires a user with administrative privileges to access it. To create a new user, run the following command:

python manage.py createsuperuser

Follow the prompts to enter a username, email, and password for the superuser. This user will be able to log in to the admin interface and manage the content of the application.

Step 5: Register models

The final step is to register the models that we want to manage using the admin interface. Models are the building blocks of a Django application, and represent the data that is stored in the database.

Open the admin.py file in the myapp directory, and add the following code:

from django.contrib import admin 
from .models import MyModel
admin.site.register(MyModel)

Replace MyModel with the name of the model that you want to register. You can register multiple models by repeating the admin.site.register() line with the names of the additional models.

Step 6: Run the development server

Now that we've set up the admin interface, we can run the development server and test it out. To start the server, run the following command:

python manage.py runserver

Open your web browser and navigate to http://127.0.0.1:8000/admin/. You should see the login screen for the admin interface. Log in with the superuser credentials that you created earlier.

Once you're logged in, you'll see the admin interface dashboard, which displays links to the registered models. Click on a model to see the list of objects in the database, and click on an object to edit its details.

Congratulations, you've set up the Django admin interface! With just a few simple steps, you now have a powerful tool for managing the content of your web application.

Latest Post


Personal

20 things, one week, and one me.

Sulav Jung Hamal - 2024/05/04

Personal

Horrible week of front end submission

Sulav Jung Hamal - 2024/04/27

Tech Tutorial

How to Install Nginx and configure it in Ubuntu server?

Sulav Jung Hamal - 2024/02/24

Web Development

What are Progressive Web Apps?

Sulav Jung Hamal - 2023/11/08

Daily Vibes