I'm new to Django. I've come from a React and NodeJs background primarily and have previously dabbled a little bit with Laravel/PHP. Django's file structure is a bit confusing at the moment, and I'm hoping you can help.
Am I right in assuming the each app in a project can have its own admin.py file? Does that mean there are likely to be multiple admin dashboards in each project? It appears to be unlike any of the other web frameworks.
Is it possible to move the admin.py file one level up (where manage.py exists) or the core app (where settings.py exists)?
Once I breakthrough this conflict in my head, I think I can crack on with my task.
Thanks for your help.
Yes, you can have as many admin panels as you have apps developed, if you want. Providing you include the admin URL in each app's url.py file, by convention, you will access them at localhost:8000/admin/name_of_the_app (as long as you have previously set up your URLs conf). Yet, if you want a single admin panel with all your apps' models in it, you have to register them in the same admin.py file which is usually centralized in a core folder (or app) serving other apps. For instance, inside your project's main folder (where the setting.py file is located).
Here's an example of how you can set up separate admin panels for two apps named: 'app1' and 'app2', each with its own three models. First for App1:
# App1 admin.py
from django.contrib import admin
from .models import model1, model2, model3
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
Now for App2:
# App2 admin.py
from django.contrib import admin
from .models import model4, model5, model6
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
You can then access each admin panel by navigating to the corresponding URL. By default, the URLs for the admin panels will be 'localhost:8000/admin/App1/' and 'localhost:8000/admin/App2/'.
However, if you want one single admin panel for all apps' models, then you register all your models in the admin.py file located in your core folder. Again, make sure your URLs conf are correctly set. Here is the example for a single admin panel with models from two distinct apps:
# Core folder admin.py
from django.contrib import admin
# Import models from each app
from App1.models import model1, model2, model3
from App2.models import model4, model5, model6
# Register models from all the apps
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
In the above code, you will access your admin panel at 'localhost:8000/admin after login in as a superuser.