社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何在django中更新表单和表单元格

Kevin • 7 年前 • 2702 次点击  

我是非常新的django和python,试图学习和构建一个webapp。

我想以表格格式向用户显示一些数据。用户应该能够在表视图中添加、删除和更新记录。

我可以实现添加,删除部分,但是,不能让我的头周围更新现有的记录。

理想情况下,当单击特定行的“编辑”按钮时,我希望将行数据作为django表单填充到模式视图中。但甚至无法从可编辑的表行获取基本更新。这是我的示例代码。

我在下面的链接中尝试了这个,但是,没有帮助..或者是我不明白。 Django: updating database row via table cell

模特儿


# Create your models here.

class customerDataModel(models.Model):
    customerName = models.CharField(max_length=200)
    custormerLocation = models.CharField(max_length=200)
    custormerAge = models.CharField(max_length=200)
    custormerLanguage = models.CharField(max_length=200)

    def __str__(self):
        return self.customerName

表单

from django import forms
from . models import customerDataModel


class addCustomerForm(forms.ModelForm):

    class Meta:
        model = customerDataModel
        fields = ('customerName', 'custormerLocation', 'custormerAge', 'custormerLanguage')

        widgets = {
            'customerName': forms.TextInput(
                attrs={
                    'class': 'form-control'
                    }
                ),
            'custormerLocation': forms.TextInput(
                attrs={
                    'class': 'form-control'
                    }
                ),
            'custormerAge': forms.TextInput(
                attrs={
                    'class': 'form-control'
                    }
                ),

            'custormerLanguage': forms.TextInput(
                attrs={
                    'class': 'form-control'
                    }
                ),


            }

VIEW

from django.shortcuts import render
from . forms import addCustomerForm
from . models import customerDataModel
from django.http import HttpResponseRedirect
from django.urls import reverse

# Create your views here.

#from django.http import HttpResponse


def index(request):
    if request.method == 'POST':
        form = addCustomerForm(request.POST)
        if form.is_valid():
            # print("VALID")
            form.save()

    customer_table_data = customerDataModel.objects.all()
    form = addCustomerForm
    return render(request, 'index.html', {'customer_table_data' : customer_table_data ,'form' : form})
    #return HttpResponse("Hello, world. You're at the polls index.")


def delete_customer_row(request, id):
    customerDataModel.objects.filter(id=id).delete()
    return HttpResponseRedirect(reverse('index'))


def edit_customer_row(request, id):

    print('The value in ID is ', id)

    instance = customerDataModel.objects.get(id=id)
    if request.method == "POST":
        form = addCustomerForm(request.POST, instance=instance)
        if form.is_valid():
             print("VALID")
             form.save()
    return HttpResponseRedirect(reverse('index'))

模板.html

<!DOCTYPE html>
<html>
<head>
<style>
table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {background-color: #f2f2f2;}
</style>
</head>
<body>

<h2>Customer Information</h2>

<form method="POST">

    {% csrf_token %}
     {{ form }}

     <br>
     <button type="submit">Submit</button>
</form>



<div style="overflow-x:auto;">
  <table>
    <tr>
      <th>Customer Name</th>
      <th>Location</th>
      <th>Age</th>
      <th>Laungage</th>
      <th>Action</th>
    </tr>



    {% for rowitem in customer_table_data %}

    <tr>
        <form action="{% url 'edit_customer_row' rowitem.id %}" method="post">
                {% csrf_token %}
        <td>{{rowitem.customerName}}</td>
        <td><div contenteditable>{{rowitem.custormerLocation}}</div></td>
        <td>{{rowitem.custormerAge}}</td>
        <td>{{rowitem.custormerLanguage}}</td>
        <!-- <td><a href="{% url 'edit_customer_row' rowitem.id %}" <button>edit</button></a></td> -->
        <td><input type="submit" value="edit"/></td>
        <td><a href="{% url 'delete_customer_row' rowitem.id %}" <button>delete</button></a></td>
        </form>
    </tr>

    {% endfor %}


  </table>
</div>

</body>
</html>

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/45450
文章 [ 1 ]  |  最新文章 7 年前