我是Django的新手,所以这听起来像是一个愚蠢的问题,但请耐心等待。 我创建了一个表单,用于将记录插入到我的数据库中,现在我正在尝试使用相同的表单来更新记录。但它似乎没有正常工作。 这是我的代码。
action="/task_list/update_task/{{ id }} /"
这不起作用," / update_task //"出现在URL中,导致Django引发错误消息,因为找不到视图。理想情况下,我想传递Django-s嵌入式id,而不是模型中的id,否则我必须在我的模型中手动包含id = models.IntegerField(primary_key=True)
命令,并将其从似乎不是&的表单中排除#34;最佳实践",鉴于Django有自己的ID。这是代码: 网址:
url(r'^task_list/update_task/(?P<task_id>\d+)/$',task_views.updateTask),
查看:
def updateTask(request, task_id):
#cur_usr_sale_point = PersonUnique.objects.filter(employees__employeeuser__auth_user = request.user.id).values_list('agreementemployees__agreement_unique__sale_point_id',flat=True)
selected_task = Tasks.objects.get(id=task_id)
if request.method == 'POST':
task_form = TaskForm(request.POST,instance=task_id)
if task_form.is_valid():
task_form.save();
taskTable = Tasks.objects.all()
return render_to_response('task_management/task_list.html',{'createTask_form':task_form, 'taskTable': taskTable},context_instance=RequestContext(request))
else:
taskTable = Tasks.objects.all()
task_form = TaskForm()
return render_to_response('task_management/task_list.html',{'createTask_form':task_form, 'taskTable': taskTable},context_instance=RequestContext(request))
Django表格
class TaskForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(TaskForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.error_messages={'required': 'Придется не поленится и заполнить это поле...'}
field.widget.attrs['cl
class Meta:
model=Tasks
fields = '__all__'
exclude=['id']
模板:
{% for task in taskTable %}
<tr>
<td onclick = "updateTaskWindow()"> <span style="display:block;" class="glyphicon glyphicon-pencil"></span></a>
</td>
<td>{{task.description_short}}</td>
</tr>
{% endfor %}
Bootstrap模式窗口,应该用于更新:
<form class="modal fade" id="updateTaskWindow" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" action="/task_list/update_task/{{ pk }}/" method="post">{% csrf_token %}
<div class="modal-dialog modal-lg form-horizontal" role="form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Обновить задачу</h4>
</div>
<div class="modal-body" style="font-size: smaller">
{{ createTask_form.as_p }}
</div>
<div class="modal-footer">
<input class="btn btn-default btn-sm" type="submit" value="Обновить"/>
</div>
</div>
</div>
</form>
模特:
class Tasks(models.Model):
id = models.IntegerField(primary_key=True)
description_short = models.CharField(max_length=500)
description_full = models.TextField()
status = models.ForeignKey(TaskStatus, on_delete=models.CASCADE)
type = models.ForeignKey(TaskType, on_delete=models.CASCADE)
date_start = models.DateTimeField(blank=True, null=True)
date_end = models.DateTimeField(blank=True, null=True)
is_important = models.IntegerField(blank=True, null=True)
effective_from = models.DateTimeField(blank=True, null=True)
effective_to = models.DateTimeField()
class Meta:
managed = False
db_table = 'tasks'
和JS:
var updateTaskWindow = function() {
$('#updateTaskWindow').modal('show');
}
我花了好几个小时试图实现这一点,但没有成功。请帮忙 !!!
答案 0 :(得分:0)
首先更改表单以获取模型的每个字段的输入,阅读有关表单here
的内容然后执行以下
之类的操作if request.method == 'POST':
task_form = TaskForm(request.POST,instance=selected_task )
if task_form.is_valid():
# inside your model instance add each field with the wanted value for it
newrecord = Tasks(name=task_form.cleaned_data['name']);
task_form.save();