How to structure template libraries in a Django project? [closed]

2024/10/5 15:10:59

I'm in the early innings of the development process and am trying to figure out best practices for structuring the template libraries for a website.

I plan to have a "base" template to extend across the site. I have seen some examples where a template directory is created at the project level to house this file, and others where a "base" file is created in one of the app folders and then extended to other apps as needed. Is there a correct way this should be done?

Thank you in advance for humoring me on this extremely basic question. Trying to get my feet under me for the first time.

Answer

For site projects I tend to make the project module itself (as created by startproject) an app too.

That way base templates, base models (such as a custom user model!), helper utility functions, etc. live in there, and one doesn't have to futz around with the filesystem template loaders at all - the default app-based template loader works fine.

That is, if my project is, say, grocerystore and it has two apps, drinks and foods (silly example but bear with me), the structure would be approximately

manage.py
grocerystore/__init__.pysettings/__init__.pymodels/__init__.pyuser.pyviews/__init__.pytemplates/base.html
foods/__init__.pyapps.pymodels/__init__.pyfood.pyviews/__init__.pyfood/__init__.pyfood_list_view.py__init__.pytemplates/foods/food_list.html
drinks/__init__.pyapps.pymodels/__init__.pydrink.py# ... etc ...

and INSTALLED_APPS would contain (along Django defaults) ('grocerystore', 'foods', 'drinks').

You can then simply {% extends "base.html" %} in your apps.

(Note, btw, that to be able to split models.py into a package, you have to make sure models/__init__.py imports the modules holding each model, so they're registered by Django!)

https://en.xdnf.cn/q/119959.html

Related Q&A

Not able to click button with Selenium (Python)

Im trying to click a button using Selenium, but everytime I get the message that it cant find the element. This happens even when I put a time.sleep() in front of it. time.sleep(5)#Click on downloaddow…

how to aproximate shapes height and width for image detection using opencv and python

i was following a tutorial about shapes detection using opencv ,numpy and python ,and it was this function i know the reason from it but i do not know how to modify it so i can use it as i want the to…

Using str.replace in a for loop

I am working on an assignment that is asking me to change the below code so that line 4 uses str.isalnum and lines 5-7 become uses only one line using str.replace.s = p55w-r@d result = for c in s:if(c…

extract all vertical slices from numpy array

I want to extract a complete slice from a 3D numpy array using ndeumerate or something similar. arr = np.random.rand(4, 3, 3)I want to extract all possible arr[:, x, y] where x, y range from 0 to 2

Output an OrderedDict to CSV

I read a CSV file and use the usaddress library to parse an address field. How do I write the resulting OrderedDicts to another CSV file?import usaddress import csvwith open(output.csv) as csvfile:re…

min() arg is an empty sequence

I created a function that consumes a list of values and produces the average. However it only works when I use integer values. I get the following error when I make the values into floats:min() arg is …

Removing last words in each row in pandas dataframe

A dataframe contains a column named full_name and the rows look like this: full_name Peter Eli Smith Vanessa Mary Ellen Raul Gonzales Kristine S Lee How do I remove the last words and add an additi…

Object of type function has no len() in python

I have been searching for a solution for this error for a while but the solutions that have helped others have not been much help for me.Here is the code that Ive wrote.def main():while True:userInput(…

My If condition within a while loop doesnt break the loop

Struggling to get my code for the final room to finish the text-based game assigned to me. I essentially want the player to be forced to get all 6 items prior to entry. Any help is greatly appreciated.…

Adding strings, first, first + second, etc

Program has to be able adding strings from the list and output them in sequence but in the way: 1 string 1 + 2 string 1 + 2 + 3 string ...def spacey(array):passe = ""i = ""m = []for…