首页 > 其他 > 详细

Django小结

时间:2017-02-03 11:02:15      阅读:196      评论:0      收藏:0      [点我收藏+]
1、app/admin.py
class TestModule(models.Model):
module = models.CharField(max_length=100,unique=True)
path = models.CharField(max_length=100)
file = models.CharField(max_length=100)
status = models.BooleanField(max_length=2)
def __str__(self):
return self.module
 
@permalink
def get_absolute_url(self):
return (‘view_blog_post‘, None, {‘module‘: self.module})
 
2、app/views.py
1)如果方法不加if request.method == ‘POST‘:
时,每次打开页面时,都会自动加载,如果加上,则只有点击提交时,才会加载页面。
 
2)数据库新增数据:
如果有键值唯一时,插入数据时,要先进行判断。
tryTestModule.objects.get(= ‘module‘= ‘the module name already exist ..., and please renew to check and then try it again‘
except :
TestModule.objects.create(item)
 
3)重定向:
‘/admin/‘
 
3、增加checkbox控制:
template:
 
<form action="./case_set/" method="POST">
{% block content %}
<h2>Module List</h2>
{% for mod in Modules %}
<h3><a href="{{ post.get_absolute_url }}">{{ mod.module }}</a></h3>
{% for case in mod.testcase_set.all %}
<p><input type="checkbox" value={{case.id}} name="check_box_list"> {{ case.case }} </input></p>
{% endfor %}
{% endfor %}
{% endblock %}
<p><input type="submit" value="提交" /></p>
<p>{{ check_box }}</p>
</form>
 
app/views.py
def case_set(request):
Modules = TestModule.objects.all()
Cases = TestCase.objects.all()
checked_list = request.POST.getlist(‘check_box_list‘)
TestCase.objects.update(status = 0)
for checked in checked_list:
TestCase.objects.filter(id = int(checked)).update(status = 1)
 
 
4、导入模块:
可以在__init__.py页面中增加自动加载
import dirname0]))

Django小结

原文:http://www.cnblogs.com/marco2017/p/6362123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!