vanilla是django的一个框架,更高效,不过使用思路有点与众不同。
先看官方文档说明:http://django-vanilla-views.org/api/model-views
只有两种模式:base views ,model views
使用思路:
1.url层配置,as_views模式,跟viewlist,generic通用模式类似
urlpatterns = patterns('',
url(r'^$', views.JiraIssueView.as_view(), name='issue-list'),
url(r'^trackTask/$', views.TrackTaskView.as_view(), name='track-task'),
}
class SearchTrackTask(forms.Form):
app_name = forms.CharField(max_length=255, widget=forms.TextInput({'class': 'form-control', 'placeholder': '发布应用名称'}))
app_version = forms.CharField(max_length=255, widget=forms.TextInput({'class': 'form-control', 'placeholder': '发布应用版本'}))
operater = forms.CharField(max_length=255, widget=forms.TextInput({'class': 'form-control', 'placeholder': '发布者'}))
2.views层写法,与前端交互,并返回数据
(1)这里跟普通的框架不同之处在于,content是程序里返回
(2)没有render,httpresponserender这类,也没有指定数据返回的页面。具体原因继续往下看
class TrackTaskView(ListView):
model = models.TrackTask
form_class = forms.SearchTrackTask
queryset = None
lookup_field = 'title'
paginate_by = 15
'''
返回context信息
'''
def get_context_data(self, **kwargs):
context = super(TrackTaskView, self).get_context_data(**kwargs)
# add search form
context['form'] = self.get_form()
context['title'] = u'发布状态跟踪'
context['introduction'] = u'包含信息:发布人,应用名称,应用版本,状态信息,状态更新时间。'
# show maintenance notification. If it's empty, the notification will not be appeared in the page.
context['notification'] = u''
# login partial and commons
context['year'] = datetime.now().year
return context
'''
查询数据返回
'''
def get_queryset(self):
# support search
try:
app_name = self.request.GET['app_name']
app_version = self.request.GET['app_version']
operater = self.request.GET['operater']
except Exception as e:
app_name = ''
if app_name == '':
return models.TrackTask.objects.filter(app_name=app_name).order_by('id')
else:
return models.TrackTask.objects.filter(app_version=app_version).order_by('id')
3.返回数据的页面格式class_list.html
tracktask_list.html
页页调用action
<li><a href="{% url 'track-task' %}" class="navbar-brand">发布状态跟踪 </a></li>
成品:
总结:更高效的编码,但使用方式思路有点异类,看了一个小时官方文档,才明白怎么使用