Skip to content

imvickykumar999/Django-Khata-Book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KhataBook Admin Dashboard

unzip /home/adminkhatadashboard/app.zip
find /home/adminkhatadashboard -type d -exec chmod 755 {} +

pip install django-jazzmin
python manage.py collectstatic
zip -r project.zip .

image image image image image

settings.py

INSTALLED_APPS = [
    'customers', # app name
    'jazzmin', # write it here

    'django.contrib.admin',
    ...
]

TIME_ZONE = 'Asia/Kolkata'
LOGIN_URL = '/login/'

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

urls.py

from django.conf.urls.static import static
from django.urls import path, include
from django.conf import settings
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('customers.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

admin.py

from django.contrib import admin
from .models import Customer, Purchase
from django.db.models import Sum

class PurchaseInline(admin.TabularInline):
    model = Purchase
    extra = 1  # Number of extra blank forms

class CustomerAdmin(admin.ModelAdmin):
    list_display = ('name', 'latest_purchase_date', 'total_amount_spent')  # Display total amount spent
    inlines = [PurchaseInline]  # Shows purchases inline on the customer page

    def latest_purchase_date(self, obj):
        latest_purchase = obj.purchases.order_by('-purchase_date').first()  # Get the latest purchase by date
        return latest_purchase.purchase_date if latest_purchase else "No purchases"

    def total_amount_spent(self, obj):
        total = obj.purchases.aggregate(total=Sum('price'))['total']
        return total if total else 0

    latest_purchase_date.short_description = 'Latest Purchase Date'  # Column name in the admin interface
    total_amount_spent.short_description = 'Total Amount Spent'  # Column name for total amount

class PurchaseAdmin(admin.ModelAdmin):
    list_display = ('item_name', 'price', 'customer', 'purchase_date')
    list_filter = ('purchase_date', 'customer')  # Filters by date and customer
    search_fields = ('item_name', 'customer__name')  # Search by item and customer name

# Register the models with custom admin configurations
admin.site.register(Customer, CustomerAdmin)
admin.site.register(Purchase, PurchaseAdmin)