Git Product home page Git Product logo

school_adminsite_django's Introduction

Django Admin Site

Overview

The code in this repository uses the django framework.

Learning Objectives

  • Understand the concepts of Django admin site
  • Create and customize your Django admin site to manage the content of an onlinecourse app
  • Use PostgreSQL an open-soutce relational database management system.

The main files of interest are:

admin.py: contains the definitions for each model in the admin panel models.py: definitions for each data model urls.py: urls for each viewport views.py: defines views for each webpage (see .html files in templates folder)

Setting up

Cloning the repository

git clone https://github.com/josuecross/school_adminsite_django

Install dependencies:

python3 -m pip install -U -r requirements.txt

Open settings.py to configure database credentials in DATABASES section.

Activate models for an onlinecourse app wich will be managed by admin site:

python3 manage.py makemigrations
python3 manage.py migrate

To create a superuser for acces admin site:

python3 manage.py createsuperuser

To run the server:

python3 manage.py runserver

Django Administration:

image

Register Models with Admin site

You can add models to the admin site adminsite/admin.py: For example a Instructor model:

# Instructor model
class Instructor(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    full_time = models.BooleanField(default=True)
    total_learners = models.IntegerField()

    def __str__(self):
        return self.user.username

Can be added to the adminsite by registering its model in admin.py:

admin.site.register(Instructor)

image

Customize Admin Site

Adding a new class to adminsite/admin.py you can choose wich fiels to include in the admin site

class Course(models.Model):
    name = models.CharField(null=False, max_length=30, default='online course')
    image = models.ImageField(upload_to='course_images/')
    description = models.CharField(max_length=1000)
    pub_date = models.DateField(null=True)
    instructors = models.ManyToManyField(Instructor)
    users = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Enrollment')
    total_enrollment = models.IntegerField(default=0)
    is_enrolled = False

    def __str__(self):
        return "Name: " + self.name + "," + \
               "Description: " + self.description

    def total_score(self):
        total_score = 0
        for question in self.question_set.all():
            total_score += question.grade
        return total_score

You can include only selected fields in admin.py like:

class CourseAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'name', 'description']

admin.site.register(Course, CourseAdmin)

image

Associate Related Models

Django admin privides also a convinient way to associate related objects on a single model managing page. In adminsite/admin.py lesson models can be managed together with course objects:

class LessonInline(admin.StackedInline):
    model = Lesson 
    extra = 5

class CourseAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'name', 'description']
    inlines = [LessonInline]

admin.site.register(Course, CourseAdmin)

Captura de pantalla 2023-09-16 232837

school_adminsite_django's People

Contributors

josuecross avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.