<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Onasanya Tunde</title>
    <description>The latest articles on DEV Community by Onasanya Tunde (@rammyblog).</description>
    <link>https://dev.to/rammyblog</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F199993%2F54bbf188-67e6-4a30-a807-92d821e86a8c.jpeg</url>
      <title>DEV Community: Onasanya Tunde</title>
      <link>https://dev.to/rammyblog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rammyblog"/>
    <language>en</language>
    <item>
      <title>How to create multiple independent admin sites in Django</title>
      <dc:creator>Onasanya Tunde</dc:creator>
      <pubDate>Fri, 15 Jan 2021 08:29:59 +0000</pubDate>
      <link>https://dev.to/rammyblog/how-to-create-multiple-independent-admin-sites-in-django-1klh</link>
      <guid>https://dev.to/rammyblog/how-to-create-multiple-independent-admin-sites-in-django-1klh</guid>
      <description>&lt;p&gt;Django framework comes with a powerful part called the &lt;strong&gt;admin&lt;/strong&gt; interface. The admin reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site ( &lt;a href="https://docs.djangoproject.com/en/3.1/ref/contrib/admin/" rel="noopener noreferrer"&gt;Django official docs&lt;/a&gt; ).&lt;/p&gt;

&lt;p&gt;With the admin interface, You can rapidly perform CRUD operations with any database model. The admin comes with hooks for easy customization.&lt;/p&gt;

&lt;p&gt;In this tutorial, I will guide you on how to create multiple independent admin sites. The common method to make the admin interface is to place all models in a single admin. Anyway, it is possible to have different admin pages in a single Django application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;For this tutorial, we are going to create a simple Django application with two models. Before you begin this tutorial you'll need the &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Virtual Environment&lt;/li&gt;
&lt;li&gt;A web browser&lt;/li&gt;
&lt;li&gt;A code editor e.g VSCode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s get started…&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Django Application
&lt;/h2&gt;

&lt;p&gt;Step 1:  Create and activate a virtual environment, I will name the virtual environment venv.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;virtualenv venv
source venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll know that your virtual environment has been activated, because your console prompt in the terminal will change. It should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(venv) $
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Install the latest version of Django using the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install Django
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Create the Django application, for the purpose of this tutorial we would name the application &lt;strong&gt;core&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django-admin startproject core .   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The folder structure should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548108352%2FqccUNrVv7.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548108352%2FqccUNrVv7.jpeg" alt="django-admin1.jpg"&gt;&lt;/a&gt;&lt;br&gt;
Run the application using the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py runserver   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your server should be running on &lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548049952%2FbIOdB6ap4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548049952%2FbIOdB6ap4.png" alt="Screenshot_2021-01-13 Django the Web framework for perfectionists with deadlines .png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Ignore the unapplied migrations message&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up the Django Application
&lt;/h2&gt;

&lt;p&gt;In the previous section, we created our Django application, now it is time to set it up.&lt;br&gt;
Step 1: Migrate the default tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; python manage.py migrate 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Create a superuser, this superuser account will be used to access the admin pages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll then be asked to enter a username followed by your email address and password. Once you’ve entered the required details, you’ll be notified that the superuser has been created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Username: admin
Email address: admin@example.com
Password:
Password (again):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the app and models
&lt;/h2&gt;

&lt;p&gt;Step 1: Create an app called blog.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py startapp blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548767046%2F8X7-_JAaF.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610548767046%2F8X7-_JAaF.jpeg" alt="Screenshot from 2021-01-13 15-37-15.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Open the &lt;code&gt;settings.py&lt;/code&gt; file located the core folder(&lt;code&gt;core/settings.py&lt;/code&gt;), then edit the INSTALLED_APPS to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog'
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: we need to create two simple models in the blog app called todo and post.  In &lt;code&gt;blog/models.py&lt;/code&gt; add the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models

class Todo (models.Model):
    title = models.CharField(max_length=100)    

    def __str__(self):
        return self.title


class Post(models.Model):
    title = models.CharField(max_length=500)
    image_url = models.URLField(max_length=500)

    def __str__(self):
        return self.title
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the code, we need to makemigrations and migrate, so that the Post and Todo tables can be created in the database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py makemigrations
python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Add the two models to the admin interface so we can perform crud operations on both models, In &lt;code&gt;blog/admin.py&lt;/code&gt; add the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib import admin
from .models import Post, Todo

admin.site.register(Post)
admin.site.register(Todo)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the models to the &lt;code&gt;blog/admin.py&lt;/code&gt; file you can then start the server(&lt;code&gt;python manage.py runserver&lt;/code&gt;), then navigate to &lt;code&gt;http://127.0.0.1:8000/admin/&lt;/code&gt;, it will ask you to login, input the superuser details you created earlier. The page should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610549858634%2F8ZurYjL_h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610549858634%2F8ZurYjL_h.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, our todo and post models are in the same place. Now we want to split the admins.&lt;/p&gt;

&lt;p&gt;We will not tamper with the default admin but rather create an independent admin for post which will be a new subclass of &lt;code&gt;admin.AdminSite&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;blog/admin.py&lt;/code&gt; we do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib import admin
from .models import Post, Todo

class PostAdminSite(admin.AdminSite):
    site_header = "Blog Post admin"
    site_title = "Blog Post Admin Portal"
    index_title = "Welcome to Rammyblog"

post_admin_site = PostAdminSite(name='post_admin')


post_admin_site.register(Post)
admin.site.register(Todo)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a new class called PostAdminSite and inherited from admin.AdminSite, then I declared the following varaibles &lt;br&gt;
site_header is the &lt;/p&gt; HTML tag of the Post Admin Page&lt;br&gt;
site_title is the  HTML tag of the Post Admin Page&lt;br&gt;
index_title will replace the Django Adminstration title on the admin page.

&lt;p&gt;Then I created an Instance of the PostAdminSite class called  post_admin_site, then I registered the Post model there.&lt;/p&gt;
&lt;h2&gt;
  
  
  Routing the independent admin pages
&lt;/h2&gt;

&lt;p&gt;We already have a separate admin class for the Post model, we just need to find a way to access it on the web interface. &lt;br&gt;
Step 1: Open &lt;code&gt;core/urls.py&lt;/code&gt; change the urls.py to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib import admin
from django.urls import path
from blog.admin import post_admin_site

urlpatterns = [
    path('admin/', admin.site.urls),
    path('post-admin/', post_admin_site.urls)
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separates the Post admin from the Todo admin. Both admins are available at their respective URLs, &lt;code&gt;http://127.0.0.1:8000/admin/&lt;/code&gt; and &lt;code&gt;http://127.0.0.1:8000/post-admin/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The Post Admin should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610551929521%2FBULUb7kUX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610551929521%2FBULUb7kUX.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;while the common admin page should loom like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610551967911%2FFTnZ9KJVk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1610551967911%2FFTnZ9KJVk.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can create more models and add them to the admin class you want, you can have a model registered in different admin class as well.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>djangorestframework</category>
    </item>
  </channel>
</rss>
