Hello,

I am trying to create a form equivalent to the edit inline that is
implemented in the admin interface.
I have a models with "Survey", "Poll", "Choice" as describe below and
I would like to have a single page to create my Survey with multiple
questions (*n) and multiple (*n) choices.

==============models.py=============
class Survey(models.Model):
    name = models.CharField(maxlength=20)
    description= models.CharField(maxlength=200)
    start_date=models.DateField("Effective from")
    end_date=models.DateField("Effective to")
class Poll(models.Model):
    survey=models.ForeignKey(Survey)
    question = models.CharField(maxlength=200,core=True)
    pub_date = models.DateField('date published',auto_now=True)
class Choice(models.Model):
    poll = models.ForeignKey(Poll, edit_inline=models.TABULAR,
num_in_admin=5)
    choice = models.CharField(maxlength=200,core=True)
    votes = models.IntegerField(core=True)
=====================================

I do not understand yet how I can create such from with the newform
library.
so far I have the following views.py

=====================views.py============
def survey_cud(request):
    # initialize variables to sent to template
    message = ''
    submit_action = 'Add'
    edit_id = ''
    # generate default form
    SurveyForm = forms.form_for_model(Survey)
    f = SurveyForm()
    # handle edit and delete events
    if request.method=="GET":
        if request.has_key("edit_id"):
            # replace default form with form based on row to edit
            survey = Survey.objects.get(pk=request.GET['edit_id'])
            SurveyForm = forms.form_for_instance(survey)
            f = SurveyForm()
            submit_action = 'Update'
            edit_id = request.GET['edit_id']
            message = 'Editing survey ID ' + request.GET['edit_id']

        if request.has_key('delete_id'):
            Survey.objects.get(pk=request.GET['delete_id']).delete()
            message = 'Survey deleted.'

    # handle add and update events
    if request.method == 'POST':
        if request.POST['submit_action'] == 'Add':
            # attempt to do add
            add_f = SurveyForm(request.POST)
            if add_f.is_valid():
                add_f.save()
                message = 'Contact added.'
            else:
                # validation failed: show submitted values in form
                f = add_f

        if request.POST['submit_action'] == 'Update':
            # attempt to do update
            survey = Survey.objects.get(pk=request.POST['edit_id'])
            SurveyForm = forms.form_for_instance(survey)
            update_f = SurveyForm(request.POST.copy())
            if update_f.is_valid():
                update_f.save()
                message = 'Survey updated.'
            else:
                # validation failed: prepare form for new update
attempt
                submit_action = 'Update'
                edit_id = request.POST['edit_id']
                f = update_f

    # get existing surveys
    survey_list = Survey.objects.all()

    # return rendered HTML page
    return render_to_response('dj_survey/template/cud_survey.html',
                            { 'request': request,
                              'message': message,
                              'survey_list': survey_list,
                              'form': f.as_table(),
                              'submit_action': submit_action,
                              'edit_id': edit_id
                            })
====================================

and the following template

==================template=================
<html>
<head>
  <title>Surveys</title>
</head>

<body>
{% if message %}
  <b>{{ message }}</ b>
  <p />
{% endif %}

{% if survey_list %}
<table>
  {% for survey in survey_list %}
  <tr bgcolor='{% cycle FFFFFF,EEEEEE as rowcolor %}'>
    <td>{{ survey.name }}</td>
    <td><a href='{{ request.path }}?edit_id={{ survey.id }}'>Edit</a></
td>
    <td><a href='{{ request.path }}?delete_id={{ survey.id }}'>Delete</
a></td>
  </tr>
  {% endfor %}
</table>
<p />
{% endif %}

<form action='{{ request.path }}' method='POST'>
<input type='hidden' name='edit_id' value='{{ edit_id }}'>
<table>
{{ form }}
<tr>
  <td colspan=2 align=right>
    <input type=submit name='submit_action'
value='{{ submit_action }}'>
  </td>
</tr>
</table>
</form>

{% ifnotequal submit_action 'Add' %}
  <p />
  <a href='{{ request.path }}'>Add New Survey</a>
{% endifnotequal %}
</html>
=============================================

Thank you very much for your help.

Yann


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to