I’m writing my first Django app and want to create a header model which has several different classes in it.
Code:
class Meta(models.Model):
meta_keywords = models.CharField(max_length=500)
meta_description = models.CharField(max_length=500)
page_title = models.CharField(max_length=200)
page_id = models.CharField(max_length=100)
class Javascript(models.Model):
page_id = models.CharField(max_length=100)
status = models.BooleanField()
class Javascript_resources(models.Model):
page_id = models.CharField(max_length=100)
status = models.BooleanField() #whether or not these are live/local
class Style_sheets(models.Model):
page_id = models.CharField(max_length = 100)
resource_type = models.CharField(max_length = 100)
status = models.BooleanField()
How could I create a function in the model to return an object that has the correct meta object, style_sheets object, javascript object etc..
The best solution for this problem, in my case, ended up looking like this:
I like to store the related files in a database for several reasons. In my settings file I have a status setting that allows me to control the entire site’s header. I have certain files that are live/local only and some that are resources. For instance, I used less css locally but compile it for the live site. When I change this variable, this automatically changes my headers by querying the database for the proper files. I also check the files exist because it can be very frustrating to debug when a css/js file doesn’t load properly! Its a bit overkill but will be used for many sites.
from django.conf import settings
from os import path # could also be written as import os.path
class Resources(object):