Python 如何在django中编辑数据?
我已从数据库中检索数据并将其显示在表中。现在点击编辑按钮我想编辑表单。我已经成功地从数据库中插入和检索数据,但我不知道如何编辑并保存到数据库中 viwes.pyPython 如何在django中编辑数据?,python,django,Python,Django,我已从数据库中检索数据并将其显示在表中。现在点击编辑按钮我想编辑表单。我已经成功地从数据库中插入和检索数据,但我不知道如何编辑并保存到数据库中 viwes.py def pramod(request): p1 = request.POST.get('name',' ') p2 = request.POST.get('address',' ') p3 = request.POST.get('city',' ') p4 = request.POST.get(
def pramod(request):
p1 = request.POST.get('name',' ')
p2 = request.POST.get('address',' ')
p3 = request.POST.get('city',' ')
p4 = request.POST.get('sp',' ')
p5 = request.POST.get('country',' ')
p6 = request.POST.getlist('chk1[]',' ')
p7 = request.POST.get('sex',' ')
books = Publisher(name=p1,address=p2,city=p3,state_province=p4,country=p5,course=p6,Gender=p7)
books.save()
dataset=Publisher.objects.all()
data={
'dataset':dataset,
}
return render(request,'Publisher.html',data)
models.py
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
course = models.CharField(max_length=50)
Gender = models.CharField(max_length=50)
Publisher.html
<div class="col-lg-4" style="margin-top: 100px">
<form action="/pramod/" method="POST">
{% csrf_token %}
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" />
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" />
</div>
<div class="form-group">
<label>city</label>
<input type="text" name="city" class="form-control" />
</div>
<div class="form-group">
<label>state_province</label>
<input type="text" name="sp" class="form-control" />
</div>
<div class="form-group">
<label>country</label>
<input type="text" name="country" class="form-control" />
</div>
<div class="form-group">
<label>Course</label>
<input type="checkbox" name="chk1[]" value="Dot.NET" > Dot.NET
<input type="checkbox" name="chk1[]" value="Python" > Python
<input type="checkbox" name="chk1[]" value="Django" > Django
</div>
<div class="form-group">
<label>Sex</label>
<input type="radio" name="sex" checked="checked" value="Male" >Male
<input type="radio" name="sex" checked="checked" value="Female" >Female
</div>
<button type="submit" class="btn bg-olive btn-block">save</button>
</form>
</div>
<div class="col-lg-8" style="margin-top: 100px">
<table class="table table-striped table-condensed table-bordered ">
<B class="btn-success">Data</B> <thead class="btn-primary">
<tr>
<th>Name</th>
<th>Address</th>
<th>city</th>
<th>country</th>
</tr>
</thead>
<tbody>
{% for p1 in dataset %}
<tr>
<td>{{ p1.name }}</td>
<td >{{ p1.address }}</td>
<td >{{ p1.city }}</td>
<td >{{ p1.country }}</td>
<td><a href="/pramod1/ pk=p1.id %}">edit</a></td>
<td><a>Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
从您的代码来看,它看起来像是一个非常原始的代码,您试图自己完成所有事情。 您可以尝试以下任一方法来解决此问题: A.基于类的视图更新视图: 在Django中,引入了基于类的视图,这些视图比基于函数的视图更受欢迎 因此,使用Django中的UpdateView可以轻松实现上面编写的功能: B.Django格式的模型表格: 但是,如果不想更改当前视图,则应尝试Django ModelForms: 以下是模型表单的示例用法:
我强烈建议您使用UpdateView,因为它已经在移动设备上使用了幕后的ModelFormIm,因此我无法轻松找到链接,但您要寻找的起点是通用更新视图