问题
Is it possible to modify data through custom template tag in Django? More specifically, I have a model named Shift whose data I want to display in a calendar form. I figured using a custom inclusion tag is the best way to go about it, but I also want users to be able to click on a shift and buy/sell the shift (thus modifying the database). My guess is that you can't do this with an inclusion tag, but if I were to write a different type of custom template tag from the ground up, would this be possible? If so, can you direct me to a few resources that address how to write such a tag?
Thank you in advance.
回答1:
This type of logic does not belong in a template tag. It belongs in a view that will respond to AJAX requests and return a JSONResponse. You'll need some javascript to handle making the request based on the input as well.
来源:https://stackoverflow.com/questions/31281119/django-modifying-data-with-user-input-through-custom-template-tag