Django + HTMX - create/update parent model with child models at once - django

I found a great tutorial about dynamically saving models in django using HTMX:
Video: https://www.youtube.com/watch?v=KVq_DjIfnBo
Text version: https://justdjango.com/blog/dynamic-forms-in-django-htmx
My problem is that I want to create author, add books and save author with books at once - using HTMX on one page.
Could you please explain, how to do it?
Thank you

Related

How can include a regular Django view content in a Wagtail page?

I have a website running on Django that uses Wagtail for most of the pages (that are simply text content, editable through the Wagtail Admin UI). However, I have some content that needs to be dynamically rendered from my own Django model (let's say it's a list of handouts). I'd like that content to be rendered on a Wagtail page that would also have some Wagtail models (e.g., top menu, some introductory text). What's the best way to do this? I thought about making the list of handouts an API endpoint, and rendering it with JavaScript + XHR, but it seems like there should be a good way to do it server-side.
Assuming the introductionary text is a block in a Wagtail StreamField, you could also define a Wagtail Block that links to a Django model. https://pypi.org/project/wagtail-modelchooser/ is a useful extension that provides this functionality. This makes it possible to render things from Django model instances in your Wagtail stream content.
One option would be to create a property on your Page model that retrieves whatever you are looking for from this other model
# rest of your imports
...
from handouts import Handouts
class MyPage(Page):
# rest of your page's fields
...
#property
def get_handouts(self):
handouts = Handouts.objects.all()
return handouts

inserting data in a DateTimeField() in Django by using datetime-local

Please, I am creating a to-do list app as my first django project.
My form is not valid because I am trying to insert DateTimeField in django using datetime-local.
I did research, I discovered I need to do a custom model so I parse the datetime-local field.
Pls guide me on how to create a custom model field.

django url parameter has space,How to solve it?

I'm a newbie for program and study django now. I have one question.
example:
my product title: classic photo frame
my url: url(r'^(.+)/$', products, name='products'),
I open through url with www.xxx.com/classic photo frame/
it works,but the this url has space, I know it's not correct.
I add - to parameter,such as classic-photo-frame. It works too.
But this way, - will show in front-end page.
How can I resolve?
url shows /classic-photo-frame but the front-end page is classic photo frame
Thank you.
the best way use SlugField .
For more information check this link. https://docs.djangoproject.com/en/1.10/ref/models/fields/#slugfield
and you can add this line prepopulated_fields = {'slug': ('title',), } to your custom django admin class for auto generate slug in admin.
after this you can use slug in your urls

In django, can I show post's permalink in admin page?

I have a model called Post for my Blog application in Django. I have a slug field that is prepopulated by the title field. (I also use the get_absolute_url method, if that matters). What I want is to be able to see the permalink, of course without being able to edit it! Just readonly. I also believe that this field will be empty when I create a post, so only visible-active when I update a post.
Thanks in advance.

Django FormWizard prefill on edit

I'm using a FormWizard to edit objects and I want to be able to prefill the fields from database when the edit page is loaded.
For example if my URL is 'category_edit_wizard/5', the field 'title' should have the value of the category with the ID 5 when the page is loaded.
Any way to do that?
Thank you
If you're using django-formwizard or the new formwizard implementation introduced in Django 1.4 you can use ModelForm's as a step form and pass an instance_dict when creating the WizardView instance.
instance_dict works the same way initial_dict works - see
https://django-formtools.readthedocs.io/en/latest/wizard.html#formtools.wizard.views.WizardView.initial_dict
If you're using the old formwizard implementation from Django 1.3 and lower, you can pass initial as described here: https://django-formtools.readthedocs.io/en/latest/wizard.html#providing-initial-data-for-the-forms