I'am new to Python programming and Django Framework.
I have this app named Devices with multiple tables related with each other (Brand, Version, Location etc) and has crud. What I would like to do is to reuse my functions(list,create,detail etc.) instead of creating individual functions for each table.
Right now I'am unable to change BrandForm dynamically to example VersionForm. Is this possible? and am I doing the right thing?
urls.py
from django.urls import path
from .views import *
app_name = 'devices'
urlpatterns = [
# BRAND
path('brand/create', create_view, name='brand-create'),
# VERSION
path('version/create', create_view, name='version-create'),
]
views.py
from .models import *
from .forms import BrandForm, VersionForm
# GET PAGE NAME TO GET MODEL NAME
def get_page_name(request):
currentUrl = request.get_full_path()
page = currentUrl.split('/')[-2].split('.')[0] # Example /devices/brand/list; Get 2nd URL
return page
def create_view(request):
page = get_page_name(request) # GET PAGE MODEL NAME
page_title = 'Create ' + page.capitalize()
model = apps.get_model('devices', page.capitalize())
status = "created"
if request.method == 'POST':
form = BrandForm(request.POST)
else:
form = BrandForm()
return save_brand_form(request, form, 'devices/brand/create.html', page_title, status)