通过页面点击修改或者添加,并进入同一个模板,可直接修改数据库中的数据,以下为heroinfo_edit.html中的内容。
注:点击修改和添加进入同一模板,需要用到if语句来判断,可通过模板render中的传入的字典是否有渲染heroinfo的信息来决定进入添加页面还是修改页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<form action="{% if heroinfo.id %}/heroinfo_edit_handler/{{ heroinfo.id }}{% else %}/heroinfo_add_handler{% endif %}" method="post">
<!--{% csrf_token %}-->
<table>
<tr>
<td>名字:</td>
<td><input type="text" name="hname" value="{{ heroinfo.hname }}"></td>
</tr>
<tr>
<td>性别:</td>
<td>
{% if heroinfo.hgender == True %}
<input type="radio" name="hgender" value="1" checked>男
<input type="radio" name="hgender" value="0">女
{% else %}
<input type="radio" name="hgender" value="1" >男
<input type="radio" name="hgender" value="0" checked>女
{% endif %}
</td>
<!--{% if heroinfo.hgender == True %}-->
<!--<td>男</td>-->
<!--{% else %}-->
<!--<td>女</td>-->
<!--{% endif %}-->
</tr>
<tr>
<td>简介:</td>
<td>
<textarea name="hcontent">{{ heroinfo.hcontent|safe }}</textarea>
</td>
</tr>
<tr>
<td>所属书的名字:</td>
<td>
<select name="hbookinfo_id" id="hbookinfo_id">
<option value="0">------</option>
{% for bookinfo in bookinfo_list %}
{% if heroinfo.hbookinfo.id == bookinfo.id %}
<option value="{{ bookinfo.id }}" selected>{{ bookinfo.btitle }}</option>
{% else %}
<option value="{{ bookinfo.id }}">{{ bookinfo.btitle }}</option>
{% endif %}
{% endfor %}
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="{% if heroinfo.id %}修改{% else %}添加{% endif %}"></td>
</tr>
</table>
</form>
</body>
</html>
urls.py中添加的代码如下:
url(r'heroinfo_edit/(\d+)$',views.heroinfo_edit),
url(r'heroinfo_edit_handler/(\d+)$',views.heroinfo_edit_handler),
url(r'heroinfo_add_handler$',views.heroinfo_add_handler),
url(r'heroinfo_add$',views.heroinfo_add)
]
views.py中的代码:
from django.shortcuts import redirect
def heroinfo_delete(request,hid):
heroinfo = HeroInfo.objects.get(id=hid)
heroinfo.delete()
return redirect('/heroinfos_list')
def heroinfo_edit(request,hid):
heroinfo = HeroInfo.objects.get(id=hid)
bookinfo_list = BookInfo.objects.all()
return render(request=request,template_name='book/heroinfo_edit.html',context={'heroinfo':heroinfo,'title':'编辑页','bookinfo_list':bookinfo_list})
def heroinfo_edit_handler(request,hid):
#准备数据
heroinfo = HeroInfo.objects.get(id=hid)
#接收属性,并设置
request_post = request.POST
heroinfo.hname = request_post.get('hname')
heroinfo.hgender = int(request_post.get('hgender'))
heroinfo.hcontent = request_post.get('hcontent')
hbookinfo_id = request_post.get('hbookinfo_id')
# print(hbookinfo_id)
heroinfo.hbookinfo = BookInfo.objects.get(id = hbookinfo_id)
#数据库中已经自动写好hbookinfo_id,可直接调用,相当于上面两句的简写
# heroinfo.hbookinfo_id = request_post.get('hbookinfo_id')
heroinfo.save()
return redirect('/heroinfos_list')
def heroinfo_add_handler(request):
heroinfo = HeroInfo()
request_post = request.POST
heroinfo.hname = request_post.get('hname')
heroinfo.hgender = int(request_post.get('hgender'))
heroinfo.hcontent = request_post.get('hcontent')
hbookinfo_id = request_post.get('hbookinfo_id')
heroinfo.hbookinfo = BookInfo.objects.get(id=hbookinfo_id)
heroinfo.save()
return redirect('/heroinfos_list')
def heroinfo_add(request):
bookinfo_list = BookInfo.objects.all()
return render(request=request,template_name='book/heroinfo_edit.html',context={'title':'添加页','bookinfo_list':bookinfo_list})
heroinfos_list代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
heroinfo_list.html<hr/>
<ul>
{% for heroinfo in heroinfos %}
<li><a href="{% url 'book:details' heroinfo.id %}">{{ forloop.counter }}-{{heroinfo.hname}}</a>
<a href="/heroinfo_delete/{{ heroinfo.id }}">删除</a>
<a href="/heroinfo_edit/{{ heroinfo.id }}">编辑</a>
</li>
{% endfor %}
<li><a href="/heroinfo_add">添加</a></li>
</ul>
</body>
</html>
页面效果如下:
![](https://img.haomeiwen.com/i11842233/970343518079fd24.png)
![](https://img.haomeiwen.com/i11842233/366129cb7ee75894.png)
![](https://img.haomeiwen.com/i11842233/571d6c625b8b4749.png)
网友评论