Django - Admin panel
In the previous tutorial, you learnt to connect and do CRUD operations against Mysql database using Django REST interface. Now we learn to do the same tasks and other tasks using Django Admin panel. Django comes with Admin panel that can help do crud operations, search, filters, and more on your database. It is a very useful feature making your project development fast.
In the jobform folder, open admin.py file and add the following content:
from django.contrib import admin from .models import JobForm # Register your models here. class JobFormAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name','sex','dob','email','position','education') list_filter = ['position','education'] search_fields = ['first_name','last_name'] ordering = ['id'] # Register your models here. admin.site.register(JobForm, JobFormAdmin)
We create JobFormAdmin configuration class in which fields to display as list, filtered by fields, search fields, and sorting fields are specified. Then, we register the JobForm model with the configuration to the Admin panel using register method.
To access the Django Admin panel, you need a user account. Let execute the command below to create an admin user. It will ask you to put user name and password for the super user.
python manage.py createsuperuser
Run python manage.py runserver 5000 to start Django development server on port 5000.
While Mysql and the development server are running, visit http://localhost:5000/admin to access Django Admin panel. A login form is present. Enter user name and password created above. Successful login will redirect to the Django Admin panel. In the Admin panel, you can find, add, update, delete records from the database. Also you are able to cerate user and group, define user and group permissions, etc.
To tell Django to serve uploaded files from the media folder, update djsite/djsite/urls.py file as below:
......
from django.conf import settings
from django.conf.urls.static import static
router = routers.DefaultRouter()
router.register(r'jforms', views.JobFormView, 'jobform')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(router.urls)), #api/jforms
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# tell django to serve uploaded files from media folder in development mode
Comments
Post a Comment