开发者

Multiple text inputs in django form via Google App Engine

开发者 https://www.devze.com 2023-03-03 20:42 出处:网络
Goal: to create a question where a user creates a poll question that is \"mad-libs\" style (i.e. \"Would you fool around with _(text)__ if she wasnt dating _(text)_?\").

Goal: to create a question where a user creates a poll question that is "mad-libs" style (i.e. "Would you fool around with _(text)__ if she wasnt dating _(text)_?").

Code: This file creates a django form corresponding to an appengine entity.

from django import newforms as forms
import models
from google.appengine.ext.db import djangoforms


class PollForm(djangoforms.ModelForm):
    class Meta:
        model = models.Poll

This is an excerpt from the models.py file

from google.appengine.ext import db
from django import newforms as forms

class Poll(db.Model):
    question = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = 1)
    created_by = db.UserProperty()

def __str__(self):
    return '%s' %self.question

def get_absolute_url(self):
    return '/poll/%s/开发者_如何学运维' % self.key()

here is the html for this section

<form action="." method="post">

{{pollform.as_p}}

{% for form in choiceforms %}
    {{form.as_p}}
{% endfor %}

<input type="submit" name="createpoll" value="createpoll" />
</form>

Is there a fairly straightforward way to create a question like this with some pre-coded text and some input text? Can I harcode it in the HTML?

Any direction is greatly appreciated!


I still don't totally understand your question. If you post your full source and an example of the result you are trying to get, then you will get a better response.

Here's an example of constructing a form for someone to answer the question. I hard coded the question, but you could easily retrieve that dynamically.

class Answer(models.Model):
    answer1 = models.CharField(max_length=100)
    answer2 = models.CharField(max_length=100)

class AnswerForm(forms.ModelForm):
    class Meta:
        model = Answer

def test(request):
    if request.method == 'GET':
        form = AnswerForm()
        question_template = 'Would you fool around with %s if she wasn\'t dating %s?'
        html = question_template % (form['answer1'], form['answer2'])
        params = {'form': form,
                  'html': html}
        return render_to_response('test.html',
                                  params,
                                  context_instance=RequestContext(request))

Template:

<form method="post" action="">
    {% csrf_token %}
    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}

    {{ html|safe }}

    <input type="submit" value="Save"/>
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号