I'm using django-floppyforms. How do I pass some values to the html template? (Both for initializing a field, and for simple printing). The code below explains the setting:
models.py:
class ContactMessage(models.Model):
msg_sender = models.ForeignKey(User, related_name="sent_messages")
msg_receiver = models.ForeignKey(User, related_name="received_messages")
listing = models.ForeignKey(Listing)
msg_title = models.CharField(max_length=200)
msg_body = models.TextField()
urls.py:
url(r'^listing/(?P<listing_id>\d+)/reply/$', views.ReplyListingView.as_view(), name='reply_listing'),
views.py:
class ReplyListingView(CreateView):
model = ContactMessage
form_class = CreateContactMessageForm
forms.py:
class CreateContactMessageForm(forms.ModelForm):
class Meta:
model = ContactMessage
exclude = ['msg_sender',
'msg_receiver',
'listing',
]
widgets = {
'msg_title': forms.TextInput({"value": ????}),
'msg_body': forms.Textarea,
}
contactmessage_form.html:
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col-md-6">
<p>USER: {{request.user}}
SENDER: {{ form.sender }}
RECEIVER: {{ form.receiver }}
LISTING: {{ form.listing }}
</p>
<form method="post">
{% csrf_token %}
{{form}}
<input class="btn btn-success" type="submit" value="Send" />
<button type="button" class="btn btn-default" onClick="window.history.back();">
Cancel
</button>
</form>
</div>
</div>
{% endblock %}
So:
Suppose in a certain context, I know the listing, the sender, and the receiver (background explanation: The URL has the listing_id, so the listing's title and owner are known; and furthermore the sender should be the current logged-in user).
Now I want to display a ReplyListingView, pass those three values to it, then have it show the CreateContactMessageForm such that:
The msg_title widget is pre-filled with the value of the "title" field of the given listing (i.e. instead of the "????" part in the code above)
I can access the values of the sender, receiver, and listing fields in the html (the
<p>USER:...</p>
part).
How can I do that?
I hope the question is clear enough.
Thanks :)