Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:49:20+00:00 2026-06-09T15:49:20+00:00

I’m writing my first Django app and want to create a header model which

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:49:22+00:00Added an answer on June 9, 2026 at 3:49 pm

    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 models import *
    

    from django.conf import settings
    from os import path # could also be written as import os.path

    class Resources(object):

    status = True
    page_id = ""
    
    
    # META INFORMATION
    meta_keywords = ""
    meta_description = ""
    page_title = ""
    favicon = ""
    
    # RESOURCES
    javascript_resources = []
    javascript_files = []
    style_sheets = []
    
    def __init__(self, page_id = "home"):
        self.page_id = page_id.lower()
        self.get_status()
        self.get_meta()
        self.get_javascript()
        self.get_style_sheets()
    
    def get_status(self):
        status = settings.SITE_STATUS
    
        if(status != "live"):
            self.status = False
        else:
            self.status = True
    
    
    def get_meta(self):
        current = Meta.objects.get(page_id = self.page_id)
        self.meta_keywords = current.meta_keywords
        self.meta_description = current.meta_description
        self.page_title = current.page_title
        self.favicon = current.favicon
    
    # this function will set all of the proper javascript lists and validate all files!
    def get_javascript(self):
    
    
        javascript_list = Javascript.objects.filter(Q(page_id = "all") | Q(page_id = self.page_id)).filter(status = self.status)
        javascript_resources_list = Javascript_resources.objects.filter(Q(page_id = "all") | Q(page_id = self.page_id)).filter(status = self.status)
    
        #now evaulate the javascript
        if len(javascript_list) > 0:
            for resource in javascript_list:
                file_name = resource.url
    
                if self.exists(file_name):
                    self.javascript_files.append(file_name)
    
        if len(javascript_resources_list) > 0:
            for resource in javascript_resources_list:
                file_name = resource.url
    
                if self.exists(file_name):
                    self.javascript_resources.append(file_name)
    
    
    def get_style_sheets(self):
    
        style_sheet_list = Style_sheets.objects.filter(Q(page_id = "all") | Q(page_id = self.page_id)).filter(status = self.status)
    
    
        if len(style_sheet_list) > 0:
            for resource in style_sheet_list:
                file_name = resource.url
                file_type = resource.file_type
    
                if self.exists(file_name):
                    style_sheet = []
                    style_sheet.append(file_type)
                    style_sheet.append(file_name)
    
                    self.style_sheets.append(style_sheet)
    
    def exists(self, file_name):
    
        url = settings.STATIC_URL + file_name
    
        status = False
    
        if  path.exists(url):
            status = True
    
        return status
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.