添加新项目向导(分两步)

时间:2012-03-26 08:43:32

标签: django django-admin django-templates django-views

我想在Django管理页面中设置Save按钮,不会重定向回我点击Add按钮的页面,而是转到我的模板。我的模板中有一些字段应该填写,结果必须发送回服务器。

admin.py我重新定义response_add时,在DB中创建新条目时调用该文件。

admin.py

def response_add(self, request, obj, post_url_continue='../%s/'):
    #save the entry in DB
    super(MyModelAdmin, self).response_add(request, obj, post_url_continue)

    #To second step -- template 
    return self.second_step(request, obj)

views.second_step

def second_step(self, request, obj):
    #make something with obj
    #get some obj fields as params (list)

#When cancel button clicked in template's form
if 'cancel' in request.POST:
    self.message_user(request, _(u'Canceled'))
    return

#When saveparams button clicked in template's form
elif 'saveparams' in request.POST:
    for p in params:
        #p.value is models.CharField
        #request.POST['p.name'] -- inputted by user in template's form
        p.value = request.POST[p.name]
        p.save()

    msg = "%s. Success;" % (obj)
    self.message_user(request, "%s" % msg)
    return HttpResponseRedirect(request.get_full_path())

return render_to_response('template.html', {'obj': obj, 'params': params, \
                                                    'path':  request.get_full_path()})

template.html

{% extends "admin/base_site.html" %}
{% block content %}
    <form method="post" action="{{ path }}"> 
        <p>
            {% for p in params %}
                <input type="text" name="{{ p.name }}" />
            {% endfor %}

            <input type="hidden" name="action" value="second_step" />
            <input type="submit" name="cancel" value="Cancel" />
            <input type="submit" name="saveparams" value="Save" />
        </p>
    </form>

{% endblock %}

问题
我不能“抓住”表格的提交POST 如何在服务器端获取用户提交的(<input type="text" name="{{ p.name }}" />)值?

0 个答案:

没有答案