本文整理汇总了Python中django.views.generic.base.View方法的典型用法代码示例。如果您正苦于以下问题:Python base.View方法的具体用法?Python base.View怎么用?Python base.View使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.views.generic.base
的用法示例。
在下文中一共展示了base.View方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_update_get_object
# 需要导入模块: from django.views.generic import base [as 别名]
# 或者: from django.views.generic.base import View [as 别名]
def test_update_get_object(self):
a = Author.objects.create(
pk=1,
name='Randall Munroe',
slug='randall-munroe',
)
res = self.client.get('/edit/author/update/')
self.assertEqual(res.status_code, 200)
self.assertIsInstance(res.context['form'], forms.ModelForm)
self.assertIsInstance(res.context['view'], View)
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
self.assertTemplateUsed(res, 'generic_views/author_form.html')
# Modification with both POST and PUT (browser compatible)
res = self.client.post('/edit/author/update/', {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/list/authors/')
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
示例2: get
# 需要导入模块: from django.views.generic import base [as 别名]
# 或者: from django.views.generic.base import View [as 别名]
def get(self, request):
# django自带的logout
logout(request)
from django.core.urlresolvers import reverse
# 重定向到首页,
return HttpResponseRedirect(reverse("index"))
# ----------------------登陆业务逻辑---------------------------------
# class LoginView(View):
# def get(self, request):
# return render(request, "login.html", {})
#
# def post(self, request):
# login_form = LoginForm(request.POST)
# if login_form.is_valid():
# user_name = request.POST.get("username", "")
# pass_word = request.POST.get("password", "")
# user = authenticate(username=user_name, password=pass_word)
# if user is not None:
# if user.is_active:
# login(request, user)
# return render(request, "index.html")
# else:
# return render(request, "login.html", {"msg": "用户未激活!"})
# else:
# return render(request, "login.html", {"msg": "用户名或密码错误!"})
# else:
# return render(request, "login.html", {"msg": "用户名或密码错误!", "login_form": login_form})
示例3: test_queryset
# 需要导入模块: from django.views.generic import base [as 别名]
# 或者: from django.views.generic.base import View [as 别名]
def test_queryset(self):
res = self.client.get('/list/authors/')
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'generic_views/author_list.html')
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
self.assertIsInstance(res.context['view'], View)
self.assertIs(res.context['author_list'], res.context['object_list'])
self.assertIsNone(res.context['paginator'])
self.assertIsNone(res.context['page_obj'])
self.assertFalse(res.context['is_paginated'])
示例4: test_simple_object
# 需要导入模块: from django.views.generic import base [as 别名]
# 或者: from django.views.generic.base import View [as 别名]
def test_simple_object(self):
res = self.client.get('/detail/obj/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['object'], {'foo': 'bar'})
self.assertIsInstance(res.context['view'], View)
self.assertTemplateUsed(res, 'generic_views/detail.html')
示例5: test_create
# 需要导入模块: from django.views.generic import base [as 别名]
# 或者: from django.views.generic.base import View [as 别名]
def test_create(self):
res = self.client.get('/edit/authors/create/')
self.assertEqual(res.status_code, 200)
self.assertIsInstance(res.context['form'], forms.ModelForm)
self.assertIsInstance(res.context['view'], View)
self.assertNotIn('object', res.context)
self.assertNotIn('author', res.context)
self.assertTemplateUsed(res, 'generic_views/author_form.html')
res = self.client.post('/edit/authors/create/', {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/list/authors/')
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])