当管理员登录要编辑的页面时,我正在尝试编辑特定用户的用户详细信息,但是当我按下更新按钮时,它没有更新,没有显示任何错误消息。 (如果我写错了请指正)。
这是我的edit.html
{% extends 'base.html' %}
{% block title %}edit details{% endblock %}
{% block content %}
{% if emp %}
<form method="POST" class="form-inline my-2 my-lg-0" ">
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-1 col-form-label"></label>
<div class="col-sm-4">
<h3>Update Details</h3>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee User:</label>
<div class="col-sm-4">
<input type="text" id="id_user" required maxlength="20" value="{{
emp.user }}"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee first_name:</label>
<div class="col-sm-4">
<input type="search" id="id_first_name" required maxlength="100"
value="{{ emp.first_name }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee last_name:</label>
<div class="col-sm-4">
<input type="text" id="id_last_name" required maxlength="100" value="{{
emp.last_name }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee Email:</label>
<div class="col-sm-4">
<input type="email" id="id_email" required maxlength="254" value="{{
emp.email }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee Gender:</label>
<div class="col-sm-4">
<input type="text" id="id_gender" required maxlength="15" value="{{
emp.gender }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee Age:</label>
<div class="col-sm-4">
<input type="text" id="id_age" required maxlength="100" value="{{
emp.age }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Employee salary:</label>
<div class="col-sm-4">
<input type="text" name="ename" id="id_salary" required maxlength="100"
value="{{ emp.salary }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-1 col-form-label"></label>
<div class="col-sm-4">
<button type="submit" class="btn btn-success">Update</button>
</div>
</div>
</div>
</form>
{% endif %}
{% endblock %}
这是我的profile.html页面
{% if user.is_authenticated %}
{% if user.is_superuser %}
<table class="table table-striped table-bordered table-sm">
<thead class="thead-dark">
<tr>
<th>Employee User</th>
<th>Employee first_name</th>
<th>Employee last_name</th>
<th>Employee Email</th>
<th>Employee Gender</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for employee in employees %}
<tr>
<td>{{ employee.user }}</td>
<td>{{ employee.first_name }}</td>
<td>{{ employee.last_name }}</td>
<td>{{ employee.email }}</td>
<td>{{ employee.gender }}</td>
<td>{{ employee.age }}</td>
<td>{{ employee.salary }}</td>
<td>
<a href="{% url 'edit' employee.id %}"><span class="glyphicon
glyphicon-pencil" >Edit</span></a>
<a href="/delete/{{ employee.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<br>
<center><a href="{% url 'signup' %}" class="btn btn-primary">Add New
Record</a></center>
{% else %}
<p>username : {{ user }}</p>
<p>firstname : {{ user.first_name }}</p>
<p>lastname : {{ user.last_name }}</p>
<p>{{user.employee.age}}</p>
<p>
<a href="{% url 'edit_profile' %}">Edit details</a>
</p>
<p>
<a href="{% url 'change_password' %}">Change password</a>
</p>
{% endif %}
{% endif %}
这是编辑方法的view.py
def edit(request, emp_id):
if request.method == 'POST':
emp = employee.objects.get(pk=emp_id)
form = EmployeForm(request.POST or None, instance=emp)
if form.is_valid():
form.save()
messages.success(request, ('Item has been edited'))
return HttpResponseRedirect('view_profile')
else:
emp = employee.objects.get(pk=emp_id)
return render(request, 'edit.html', {'emp': emp})
def view_profile(request):
employees = employee.objects.all()
args= {'user':request.user,'employees':employees}
return render(request,'profile.html',args)
这是我的模型页面
class employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(max_length=50)
gender = models.CharField(max_length=1,choices=(('M','Male'),
('F','Female')),blank=True)
age = models.PositiveIntegerField(blank=True, null=True)
salary = models.CharField(blank = True,max_length=50
def __str__(self):
return self.user.username
请提供有关如何进行故障排除的建议或我可以阅读的任何文档。
答案 0 :(得分:1)
您没有正确使用表格。表单不仅用于验证,而且还负责呈现错误消息。您想要更改视图以将表单传递给模板:
def edit(request, emp_id):
emp = employee.objects.get(pk=emp_id)
if request.method == 'POST':
form = EmployeForm(request.POST, instance=emp)
if form.is_valid():
form.save()
messages.success(request, ('Item has been edited'))
# this one is probably not going to work
# as expected - you may want to check the
# doc for the `redirect` shortcut instead
return HttpResponseRedirect('view_profile')
else:
form = EmployeForm(instance=emp)
return render(request, 'edit.html', {'emp': emp, 'form':form})
然后在您的模板中,而不是手动编写整个表单标记use the form for rendering,以便正确显示验证错误。