编辑和更新 HTML 数据表 Django
Edit and update HTML datatable Django
我刚刚在 Django 1.10 中构建了一个系统,基本上是一个表单,用户可以填写并根据订单生成技术问题。如下图所示:
问题提交到数据库后,下面是 table 所有问题都出现的地方:
这是我的模型:
class TechnicalValidationSystem(models.Model):
user = models.ForeignKey(User)
author = models.CharField(max_length=30)
soss = models.CharField(max_length=30)
case_opened = models.CharField(max_length=30)
cause_reason = models.CharField(max_length=35)
date_created = models.DateTimeField(blank=True, null=True)
comments = models.CharField(max_length=500)
issue_solved = models.BooleanField(default=False)
这是我的 HTML table:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>SO-SS</th>
<th>Case Opened</th>
<th>Cause Reason</th>
<th>Comments</th>
<th>Date Created</th>
<th>Created by:</th>
<th>Issue Solved?</th>
</tr>
</thead>
<tbody>
{% for linea in lineas_de_reporte %}
<tr>
<td>{{ linea.soss }}</td>
<td>{{ linea.case_opened }}</td>
<td>{{ linea.cause_reason }}</td>
<td>{{ linea.comments }}</td>
<td>{{ linea.date_created }}</td>
<td>{{ linea.author }}</td>
<td>{{ linea.issue_solved }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
我现在需要的是找到一种用户可以修改此 table 的方法,主要是在问题是否已解决的情况下,并用该响应更新我的数据库。任何人都知道如何用 django 做到这一点?如果不是,可以使用 Jquery 或其他任何东西。
如果你们需要其他东西或者我遗漏了什么,请告诉我,谢谢您的宝贵时间。
[更新]:您的 HTML
中有类似内容
<div class="table-responsive">
<table class="..." >
<thead>
...
</thead>
<tbody>
{% for linea in lineas_de_reporte %}
<tr>
...
<td>
<form action="{% url 'view_name' %}" method="GET">
<input type="hidden" name="linea-id" value="{{ linea.id }}" />
<input type="submit" name="submit-issue-solved" value="Submit" />
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
是的,这只能在 Django 中发生(如果您愿意,也可以在 jQuery 中发生)。
您应该将 ISSUE RESOLVED?
列更改为每个单元格(当然使用 {% for %}
循环)将包含两个单选按钮(Yes
和 No
),一个隐藏的(具有唯一键的值。我猜 SOSS
在您的数据库中是唯一的 table?)和单选按钮旁边的提交按钮。
然后,该表单应将自身(通过 action={% url 'a_view_name_here' %}
属性确定)提交给 url
并由 view
处理。在该视图中,您可以获得用户选择的值(取决于使用的方法,即 request.GET.get('issue_resolved')
或 request.POST.get('issue_resolved')
),并且以相同的方式获取唯一值的值(即 SOSS
) .
最后,您将查询数据库以获取带有 SOSS
的 TechnicalValidationSystem
并更新其 issue_resolved
布尔值(如下所示:
def issue_resolved(request):
# if GET data contain a key named 'linea-id'...
if request.GET.get('linea-id'):
# ... this means that user clicked on the submit button
# get the id of linea
linea_id = request.GET.get('linea-id')
# fetch object from db, based on that id (aka pk)
linea = TechnicalValidationSystem.objects.get(id=linea_id)
# change its attribute to True
linea.issue_resolved = True
# update db with the new value
linea.save(update_fields=['issue_resolved'])
return render(request, 'template/to/the/table.html', context_here)
我刚刚在 Django 1.10 中构建了一个系统,基本上是一个表单,用户可以填写并根据订单生成技术问题。如下图所示:
问题提交到数据库后,下面是 table 所有问题都出现的地方:
这是我的模型:
class TechnicalValidationSystem(models.Model):
user = models.ForeignKey(User)
author = models.CharField(max_length=30)
soss = models.CharField(max_length=30)
case_opened = models.CharField(max_length=30)
cause_reason = models.CharField(max_length=35)
date_created = models.DateTimeField(blank=True, null=True)
comments = models.CharField(max_length=500)
issue_solved = models.BooleanField(default=False)
这是我的 HTML table:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>SO-SS</th>
<th>Case Opened</th>
<th>Cause Reason</th>
<th>Comments</th>
<th>Date Created</th>
<th>Created by:</th>
<th>Issue Solved?</th>
</tr>
</thead>
<tbody>
{% for linea in lineas_de_reporte %}
<tr>
<td>{{ linea.soss }}</td>
<td>{{ linea.case_opened }}</td>
<td>{{ linea.cause_reason }}</td>
<td>{{ linea.comments }}</td>
<td>{{ linea.date_created }}</td>
<td>{{ linea.author }}</td>
<td>{{ linea.issue_solved }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
我现在需要的是找到一种用户可以修改此 table 的方法,主要是在问题是否已解决的情况下,并用该响应更新我的数据库。任何人都知道如何用 django 做到这一点?如果不是,可以使用 Jquery 或其他任何东西。
如果你们需要其他东西或者我遗漏了什么,请告诉我,谢谢您的宝贵时间。
[更新]:您的 HTML
中有类似内容<div class="table-responsive">
<table class="..." >
<thead>
...
</thead>
<tbody>
{% for linea in lineas_de_reporte %}
<tr>
...
<td>
<form action="{% url 'view_name' %}" method="GET">
<input type="hidden" name="linea-id" value="{{ linea.id }}" />
<input type="submit" name="submit-issue-solved" value="Submit" />
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
是的,这只能在 Django 中发生(如果您愿意,也可以在 jQuery 中发生)。
您应该将 ISSUE RESOLVED?
列更改为每个单元格(当然使用 {% for %}
循环)将包含两个单选按钮(Yes
和 No
),一个隐藏的(具有唯一键的值。我猜 SOSS
在您的数据库中是唯一的 table?)和单选按钮旁边的提交按钮。
然后,该表单应将自身(通过 action={% url 'a_view_name_here' %}
属性确定)提交给 url
并由 view
处理。在该视图中,您可以获得用户选择的值(取决于使用的方法,即 request.GET.get('issue_resolved')
或 request.POST.get('issue_resolved')
),并且以相同的方式获取唯一值的值(即 SOSS
) .
最后,您将查询数据库以获取带有 SOSS
的 TechnicalValidationSystem
并更新其 issue_resolved
布尔值(如下所示:
def issue_resolved(request):
# if GET data contain a key named 'linea-id'...
if request.GET.get('linea-id'):
# ... this means that user clicked on the submit button
# get the id of linea
linea_id = request.GET.get('linea-id')
# fetch object from db, based on that id (aka pk)
linea = TechnicalValidationSystem.objects.get(id=linea_id)
# change its attribute to True
linea.issue_resolved = True
# update db with the new value
linea.save(update_fields=['issue_resolved'])
return render(request, 'template/to/the/table.html', context_here)