I have researched this and many answers out there touch on it, but none is exactly what I'm after...
I have an application that allows users to post cards to a board and each card can have zero or more comments. I have the database relationships set up so if I manually add board, card, or comment objects I can display them in the template just fine.
The card form is also working, so this is not an issue. But now I want to display a comment textarea
with a click
event (which I have working with jQuery). The problem is, the form is not displaying the field. Here is the form code:
class AddCommentForm(ModelForm):
class Meta:
model = Comment
fields = ['message']
message = forms.CharField(
widget=forms.Textarea(
attrs={
'class': 'form-control comment-input'
'name': 'message',
'placeholder': 'Comment away...'
}
),
required=False,
)
Right off the bat, there is a problem in that when Django renders a form, it uses an id
. In this case, it ends up being id_message
. Of course, in HTML, this is no good.
So I thought of using formsets
but it seems not quite right, because as I understand them, they are for several forms inside one form
tag. My case is different, in that each card has zero or more comments and the flow of the HTML dictates that there should be several form
elements.
Another challenge is that I need to have two views: one to process the cards and one to process each comment. I'm currently using different action values to accomplish this and it seems to work.
It seems like my use case is not so strange, and I can think of several cases where this kind of situation would occur: Twitter replies, comments on blog posts, etc. I just can't get Django to work with this.
I should also note that I can manually make this work with this:
<label>
<textarea class="form-control comment-input" name="message"></textarea>
<button type="submit"></button>
</label>
I guess it kinda bugs me that I can't do this with Django so I'm looking for someone who has accomplished this.
Aucun commentaire:
Enregistrer un commentaire