【发布时间】:2019-03-12 03:42:57
【问题描述】:
我创建了一个包含用户详细信息的表单,并编写了一个类视图来更新我的 django aapp 中的用户详细信息,一切正常,但是,我通过该表单在页面中手动编码表单,而不是表单小部件,我正在更新详细信息。我不明白如何将表单错误从类视图重定向到有错误的表单页面,在 django 错误中它显示 不包含 aapp/user_form.html 模板。
“profile.html”页面中包含表单的 HTML 代码
<div class="row">
<div class="col-md-12">
<form action="{% url 'updatedprofile' user.id %}" method="post">
{% csrf_token %}
<div class="modalBox">
<div class="row">
<div class="col-md-11">
<h4>Update Profile</h4>
</div>
<div class="col-md-1 icon">
<p><a href="{% url 'mprofile' %}"><i class="fas fa-times"></i></a></p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<thead>
<tr>
<td>First Name:</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="first_name" value="{{user.first_name}}" class="form-control no-border simplebox" placeholder="Enter Your First Name Here...">
</td>
</tr>
</tbody>
<thead>
<tr>
<td>Last Name:</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="last_name" value="{{user.last_name}}" class="form-control no-border simplebox" placeholder="Enter Your Last Name Here...">
</td>
</tr>
</tbody>
</table>
<table class="table table-striped">
<thead>
<tr>
<td>Mobile: </td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" name="mobile" value="{{user.mobile}}" class="form-control no-border simplebox" placeholder="Enter Mobile Number Here..."></td>
</tr>
</tbody>
</table>
<table class="table table-striped">
<thead>
<tr>
<td>Profile Pic: </td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="file" name="avatar" value="{{user.avatar}}" class="form-control no-border simplebox"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<center><button type="submit" class="btn btn-success">Add Services</button></center>
</form>
</div>
</div>
查看代码
class UpdateMprofile(LoginRequiredMixin, LogoutIfNotSuperuserMixin, UpdateView):
login_url = reverse_lazy('mlogin')
model = User
fields = ['first_name', 'last_name', 'mobile', 'avatar']
success_url = reverse_lazy('mprofile')
【问题讨论】:
标签: django django-forms django-2.0