Admin panels are at the heart of every business operation, be it sales, analytics, user management, and so much more. That said, it isn't easy to find the right tools to help you build robust and scalable systems without spending too much time.
Django admin has been the go-to solution for many developers over the years. It provides a fast, easy-to-use, and highly maintainable framework for building full-stack applications and admin dashboards. These reasons make it a great choice to build admin panels.
To show you how to build admin panels with Django, we will build a dashboard for moderators to manage business reviews. All of this will be built on Postgres and the Public Yelp dataset.
Here’s a sneak peek of what we’ll be building:
Awesome right? Let’s get started!
We start by setting up a project. A project is a collection of settings for an instance of Django, which includes a database and app configuration. A project may contain multiple apps, but we are only going to need just one.
To set up a project for our Yelp review dashboard, use your terminal and cd into a directory where you’d like to store the project and run the below command:
When that’s done, cd into the yelp directory that was created and start up the server by running the command below:
The server automatically watches the project for changes, so you don’t need to restart it manually
In Django, you can have multiple apps inside a project. This means that apps are modular and can be used in multiple projects.
Go ahead and create a reviews app that will hold the code for the dashboard by running the below command:
You’ll notice that a reviews directory has been created with a couple of files. This is where all the code will go.
Next, we’ll need to let Django know about the reviews app we just created. We do this by registering the app.
To register the app, open yelp/settings.py file and add reviews.apps.ReviewsConfig to the INSTALLED_APPS list. After adding this, INSTALLED_APPS should look like this:
When that’s done, move on to configure your database connection in the DATABASES dictionary:
Save the changes and run the below command so that Django can set up tables in the database for the app in the INSTALLED_APPS list:
The last thing that needs to be done here is to modify the path of the admin dashboard in the yelp/urls.py file so that it is mounted on the root path instead of /admin. Update the urlpatterns list to:
With everything set up, we can continue with creating modes that describe the database layout. Models also show relationships between tables and can be used to specify validation and the widgets that Django will create for the dashboard UI.
We are going to need three models: one for businesses, users, and reviews. To create these, head to reviews/models.py and paste the below code:
You’ll notice that each class represents a model of a table in the database. Each model has variables that represent the fields. The fields are annotated with their respective field types and validation/relationship where necessary.
Now let’s create migrations for these models. Run the below command to do this:
Then apply the migrations
Taking a look at the database, you’ll notice that tables for reviews_business, reviews_user, and reviews_review have been created. At this point, you can import the yelp dataset.
Download the public Yelp dataset and import it into the reviews_business, reviews_user, and reviews_review tables respectively.
Are we ready to take the app for a spin? Not so fast. You’ll need to create an admin user to be able to view the site.
To create an admin user, run the following command and answer the prompt:
Once that’s done, start the server(if you previously closed it) and visit http://127.0.0.1:8000/:
After logging in, you’ll notice that none of the models we created is showing up. Let’s fix this in the next section.
To make the models from the reviews app visible in the admin panel, you’ll need to register them in the reviews/admin.py file.
This is quite easy to do. Add the below code to your reviews/admin.py file:
Save the file and head back to the browser. Upon reloading, you should now see Business under the Review app.
Easy as pie, right?
Play around with the dashboard to see the power of Django. But you’ll notice that the way data is displayed a little off, and we can’t see reviews for each business. Django gives us the room to improve user experience, so let’s do that.
We can make the reviews dashboard much better by adding some options to customize it.
Head to the reviews/admin.py and replace its content with the below code:
Save the file and head to your browser. You’ll immediately notice that you have a much better dashboard experience.
We can further make the dashboard cleaner by removing the annoying AUTHENTICATION AND AUTHORIZATION app that shows up by default.
To do this, add these lines to your reviews/admin.py file:
Save the file and reload your browser. Now we have a super clean functional Django reviews dashboard.
Now that the application is fully built, you can play around with it. This dashboard makes it easy for moderators to search for a business and approve reviews made by users.
Here’s everything in action:
And that’s a wrap!
If you found this article helpful, please leave a like and check out our awesome product for building internal tools appsmith.com
The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.
The rich text element allows you to create and format
A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!
Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.