Django模板和视图解析表单

时间:2013-03-25 09:01:29

标签: django django-templates django-views

<form action="" method="post" id='editing_form'>{% csrf_token %}
            <label for='first_name'> First name: </label>
            <input type='text' id='first_name' value='{{user.first_name}}'/> <br/>
            <label for='first_name'> Last name: </label>
            <input type='text' id='last_name' value='{{user.last_name}}'/> <br/>
            <label for='first_name'> Email: </label>
            <input type='text' id='email' value='{{user.email}}'/> <br/>

            {% for key,value in prefs.items %}
                <label for='prefs_{{ key }}'> {{ key }}: </label>
                <input type='text' name = 'prefs' id='prefs_{{ key }}' value='{{value}}'/> <br/>
            {% endfor %}
        <input type="submit" value="Submit">
</form>

我想从我的视图中获取此输入的值和ID

{% for key,value in prefs.items %}
                <label for='prefs_{{ key }}'> {{ key }}: </label>
                <input type='text' name = 'prefs' id='prefs_{{ key }}' value='{{value}}'/> <br/>
{% endfor %}

我该怎么办?

P.S。我不允许使用表格。

THX

1 个答案:

答案 0 :(得分:0)

我会将您的首选项更改为pre_

[........]

{% for key,value in prefs.items %}
    <label for='pre_{{ key }}'> {{ key }}: </label>
    <input type='text' name = 'pre_{{ key }}' 
        id='pre_{{ key }}' value='{{value}}'/> <br/>
{% endfor %}

[.........]

获取值和ID

def view_name(request):
    if request.method == 'POST':
        for val in request.POST:
            if val[0:4] == "pre_":
                id = int(val[4:])
                value = request.POST[val]

                #do save action here

    [..........]