2

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:

  1. 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)

  2. 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 :)

Iddo Lev
  • 21
  • 4

4 Answers4

0

Assuming floppyforms works just like django forms (and it looks that way), just use initial: https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values

waverider
  • 308
  • 2
  • 10
  • As you can see above, I am not directly creating the form, but telling the view to use the form: `form_class = CreateContactMessageForm` so I don't know if your solutions applies – Iddo Lev Feb 10 '14 at 16:28
0

There is an initial View variable.

Have a look at this site they have all view methods and variables: http://ccbv.co.uk/projects/Django/1.5/django.views.generic.edit/CreateView/

You can also override the method that instantiate the form and add initial

David
  • 373
  • 3
  • 7
0
class ReplyListingView(CreateView):

    model = ContactMessage
    form_class = CreateContactMessageForm

    #add this line to the view
    initial={"field_name":"value"}
boatcoder
  • 17,525
  • 18
  • 114
  • 178
David
  • 373
  • 3
  • 7
0

Regarding the title, you should pass initial data to the form using get_initial.

Regarding other non-form stuff, your view should inject it to the template via get_context_data.

class ReplyListingView(CreateView):
    model = ContactMessage
    form_class = CreateContactMessageForm

    def get_initial(self):
        d = super(ReplyListingView, self).get_initial()
        d['title'] = "blah"
        return d

    def get_context_data(self, **kwargs):
        d = super(ReplyListingView, self).get_context_data(**kwargs)
        d['foo'] = 'bar'
        return d
Udi
  • 29,222
  • 9
  • 96
  • 129