HI, I am somewhat of a newbie to coding and certainly more so to Django and was wondering if anyone could provide any advice.
I am building a web app to use at work, which so far although challenging I have enjoyed, however there is one element I am struggling to work out how to approach:
I have a template within which it displays information about assets, however because there are many different types of assets the context can vary a lot.
I could write some code which has a lot of condition checking to provide the correct information to the template but this doesn’t seem like a DRY way of writing code and wouldn’t be very flexible.
The initial route I have thought about taking is to create a JSON field against each type of asset, within which the idea would be to store a reference to the different objects and required model fields that would make up the template, then build a function to build queries based on supplied JSON information.
As an example I was hoping to store a reference to which table(s) ( as in a tables.Table class) should be returned to the template - this is where i am stuck. As obviously the stored information is a string and I cant seem to see a way to get this string data to reference an class within my tables.py file.
Does anyone have any ideas or advice on how I could approach this and if my initial thoughts on a solution could work or not?
Keep in mind that contexts don’t need to provide every variable used by a template. It’s perfectly valid to reference a variable that doesn’t exist - Django emits blank in that situation.
This lets you test or render elements cleanly in the template.
If you’re looking for more specific information, it’s a lot easier if you provide a more specific example of what it is that you’re trying to achieve. It’s really difficult to address this in the abstract in the absence of any context.
Thanks for coming back to me.
To add some context to what I was looking to achieve, as a test I have the following JSON data saved in a JSONField:
{
"Data": {
"ITR_Data_1":"Supply_Voltage", "ITR_Data_2":"Supplier",
"ITR_Data_3":"Manufacturer",
"ITR_Data_4":"Instrument_Model_No",
"ITR_Data_5":"PLC_No",
"ITR_Data_6":"Instrument_Range",
"ITR_Data_7":"Technology_Type",
"ITR_Data_8":"Profibus_Node_Address",
"ITR_Data_9":"Supplier",
"ITR_Data_10":"Test"
},
"Tables": {
"Table_1": "InstrumentTable"
}
}
from my Tables.py file:
class InstrumentTable(tables.Table):
Edit = TemplateColumn(template_code='<button class="btn btn-info btn-sm" hx-target="#table" hx-confirm="unset" hx-get="{% url "edit_instrument" record.id %}"> Edit </button>')
Delete = TemplateColumn(template_code= '<button class="btn btn-danger btn-sm" hx-target="#table" hx-delete="{% url "delete_instrument" record.id %}"> Delete </button>')
class Meta:
model = Instrument
attrs = {"class": "table"}
template_name = "tables/bootstrap_htmx.html"
fields = ('Asset_Number','Description', 'Drawing_Document_Number','Comments')
The bit I’m not sure will work is where i would have to convert a string into an instance of a Django table.Table class
I’m sorry, I’m not following you.
I understand your JSON data.
What is your objective with this data? (Or, to phrase it another way, what is the desired end result of what you want to achieve?) I’m not seeing the purpose behind this “InstrumentTable” class. What is tables.Table
, and how does it relate to this?
For perhaps a little more clarity, what I’m really trying to get at is a more fundamental understanding of the objective - not what you’re currently trying to do as your solution for that objective. I’d like to understand the basic requirements, not your proposed implementation.
One of the best methods to getting everything started, if you haven’t already – is-to get editing the Data into Markup. Once that part of the puzzle is complete. It is far easier to move on: creating, editing, updating that new instance of the Django App with your Views. It is easier for browsing when seeing the App from the jigsaw perspective… each part is connected-to-a-different-part…
Hope that’s somewhat helpful.
To add some extra context on the project that may help clarify my objectives:
The template i am looking to make is one that will display an installation check sheet for equipment that has been installed on a project.
There is Circa 400 different types of equipment that will slot into around 50 types of check sheets.
So my issue is how to display the potential number of data variants into a single template .i.e. if the check sheet is a type of “a” then the data to be passed to the template needs to be “x, y and z”.
I appreciate my scenario is somewhat bespoke but I think I am missing something fundamental as there will be millions of applications with much more complex needs than mine!
So some of this would be able to be addressed by the appropriate structuring of your data. Other parts of this might be able to be addressed by a proper set of model methods. Yet more could be addressed by more generic handling of the data within the templates.
As just one example, the for
tag can iterate over all the elements of a dict without being directly aware of either the key or values involved.
If you really want a good example of generic template handling, take a look at what happens when you render a form. Look at all the template fragments involved and the relationships among them.
Bottom line is then that yes - this can all be done more generically than you may be currently aware. It might just take some time for you to wrap your head around the possibilities. Looking at existing samples will help.