编辑表单中的数据

时间:2012-02-18 13:40:19

标签: html django django-templates

我已经能够使用django表单和模板将数据插入数据库。我还可以在非常基本的HTML页面中显示数据。但是,我完全无法弄清楚如何编辑数据。假设我至少可以重用我用来创建新数据库条目的django模板来编辑条目,从而无需创建“编辑”模板。

请注意,我可以在控制台中编辑条目,但在浏览器中我似乎无法理解如何加载数据,当我点击显示的车辆的链接时,哪些变量带有我的主键以及如何将获取的数据从数据库传递到表单以允许编辑等。我已经检查了管理网站,以获得有关它如何在那一端做到的指导,但仍然......没有运气。

这是我的插入代码:如何修改此代码以允许我编辑vehicle.html下面显示的数据?

views.py

vehicle_add - 插入数据库

def vehicle_add(request):
if request.method == 'POST':
    form = VehicleForm(request.POST)
    if form.is_valid():
        newvehicle = Vehicle()
        vdetails = form.cleaned_data
        newvehicle.reg_number= vdetails['regnumber']
        newvehicle.model= vdetails['model']
        newvehicle.manufacturer= vdetails['manufacturer']
        newvehicle.year= vdetails['year']
        newvehicle.chassis_number= vdetails['chasisnumber']
        Vehicle.save(newvehicle)
        return HttpResponseRedirect('/vehicle')
else:
    form = VehicleForm()
return render_to_response('vehicle_add.html', {'form': form}, context_instance=RequestContext(request))

vehicle.html 以简单的HTML格式加载数据,如下所示:

{% for v in obj %}
            <tr><td><a href="/vehicle/{{v.id}}/">{{ v }}</a></td></tr>
{% endfor %}    

链接显示如下:http://localhost:8000/vehicle_add/2/

更多信息: vehicle_add.html

{% extends "base.html" %}
{% block title %}Add Vehicle{% endblock %}
{% block page %}Add Vehicle{% endblock %}
{% block content %}
<html>
<head>
<style type="text/css">
    ul.errorlist {
    margin: 0;
    padding: 0;}
    .errorlist li {
    background-color: red;
    color: white;
    display: block;
    font-size: 10px;
    margin: 0 0 3px;
    padding: 4px 5px;}
</style>
</head>
<body>
<section id = "mainform">
{% if form.errors %}
    <p style="color: red;">
        Please correct the error{{ form.errors|pluralize }} below.
    </p>
{% endif %}
<form action="" method="post">{% csrf_token %}
    <div class="field">
        {{ form.regnumber.errors }}
        <label for="id_regnumber">Reg #:</label>
        {{ form.regnumber }}</div>
    <div class="field">
        {{ form.model.errors }}
        <label for="id_model">Model:</label>
        {{ form.model }}</div>
    <div class="field">
        {{ form.manufacturer.errors }}
        <label for="id_manufacturer">Manufacturer:</label>
        {{ form.manufacturer }}</div>
    <div class="field">
        {{ form.year.errors }}
        <label for="id_year">Year:</label>
        {{ form.year }}</div>
    <div class="field">
        {{ form.chasisnumber.errors }}
        <label for="id_chasisnumber">Chasis #:</label>
        {{ form.chasisnumber }}</div>
    <div class="field">
        <input type="submit" value="Submit">
        <input type="submit" value="Clear">
    </div></form></section>
</body>
</html>
{% endblock %}

试用和错误:

这是我试图用我的观点做的但没有运气:

def vehicle_add(request):
if request.method == 'POST':
    form = VehicleForm(request.POST)
    if form.is_valid():
        newvehicle = Vehicle()
        vdetails = form.cleaned_data
        newvehicle.reg_number= vdetails['regnumber']
        newvehicle.model= vdetails['model']
        newvehicle.manufacturer= vdetails['manufacturer']
        newvehicle.year= vdetails['year']
        newvehicle.chassis_number= vdetails['chasisnumber']
        Vehicle.save(newvehicle)
        return HttpResponseRedirect('/vehicle')
else:
    #WORKING ON THIS
        form = VehicleForm
        newvehicle = Vehicle.objects.get(pk=9)
        form = VehicleForm(newvehicle)
return render_to_response('vehicle_add.html', {'form': form}, context_instance=RequestContext(request))

但是这样做我得到了不同类型的错误。我甚至不确定这是怎么做的。有人能指出我正确的方向吗?

修改

以下是当前错误:

Caught AttributeError while rendering: 'Vehicle' object has no attribute 'get'

In template d:\dev\workspace\vehicle_request\vehicle_request\mvmanager\templates\vehicle_add.html, error at line 31

然后高亮显示模板的这一部分:{{ form.regnumber }}

forms.py

class VehicleForm(forms.Form):
regnumber = forms.CharField(
                            max_length=7,
                            label='Reg #:',
                            widget = forms.TextInput(attrs={'size':7})    )
model = forms.CharField(
                        label='Model',
                        widget = forms.TextInput(attrs={'size':25})    )
manufacturer = forms.CharField(
                               max_length=25,
                               label='Manufacturer',
                               widget = forms.TextInput(attrs={'size':25})    )
year = forms.IntegerField(
                          label='Year',
                          widget = forms.TextInput(attrs={'size':4})    )
chasisnumber = forms.CharField(
                               required=False, label='Chasis #',
                               widget = forms.TextInput(attrs={'size':25})    )

2 个答案:

答案 0 :(得分:1)

修改url conf line以使用url中的参数

e.g:

(r'^vehicle_add/(\d+)/$','app.views.vehicle_add')

vehicle_add 中,第一个参数(自我参数后的第二个参数)将从网址获取 vehicle_id

您可以根据需要命名, 例如vehicle_id。

使用它:

newvehicle= Vehicle.objects.get(pk=vehicle_id)

答案 1 :(得分:0)

将表格传递给词典:

#WORKING ON THIS
    # form = VehicleForm  <<< THIS LINE IS UNNECESSARY
    newvehicle = Vehicle.objects.get(pk=9)
    form = VehicleForm(newvehicle.__dict__)