本文整理汇总了Python中blog.models.Post方法的典型用法代码示例。如果您正苦于以下问题:Python models.Post方法的具体用法?Python models.Post怎么用?Python models.Post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blog.models
的用法示例。
在下文中一共展示了models.Post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
author = User.objects.create_user(
username="user1", email="test1@example.com", password="pwd1"
)
self.tags = [
Tag.objects.create(name='tag_%s' % i) for i in range(10)
]
for i in range(10):
p = Post.objects.create(
title='Post %s' % i,
author=author,
content_raw="Post *1*",
)
p.tag.add(random.choice(self.tags))
示例2: setUp
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def setUp(self):
"""初期化。各モデルを作成する."""
tag = Tag.objects.create(name='Django')
category = Category.objects.create(name='Programing')
post = Post.objects.create(
title='Hello', text='World', category=category)
post.tag.add(tag)
示例3: test_index_get
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_index_get(self):
"""/ アクセスのテスト."""
response = self.client.get(reverse('blog:index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Hello')
self.assertQuerysetEqual(
response.context['post_list'], ['<Post: Hello>'])
示例4: test_category_get_1
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_category_get_1(self):
"""/category/Programing アクセスのテスト."""
response = self.client.get(
reverse('blog:category', kwargs={'category': 'Programing'}))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Hello')
self.assertQuerysetEqual(
response.context['post_list'], ['<Post: Hello>'])
示例5: test_tag_get_1
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_tag_get_1(self):
"""/tag/Django アクセスのテスト."""
response = self.client.get(
reverse('blog:tag', kwargs={'tag': 'Django'}))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Hello')
self.assertQuerysetEqual(
response.context['post_list'], ['<Post: Hello>'])
示例6: test_hidden_posts
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_hidden_posts(self):
post = Post.objects.latest('pub_date')
self.browser.get(self.live_server_url+reverse('index'))
body = self.browser.find_element_by_tag_name('body')
self.assertIn(post.title, body.text)
self.browser.get(self.live_server_url+reverse('archive'))
body = self.browser.find_element_by_tag_name('body')
self.assertIn(post.title, body.text)
self.browser.get(self.live_server_url+reverse(
'tag', kwargs={'slug': post.tag.first().name}
))
body = self.browser.find_element_by_tag_name('body')
self.assertIn(post.title, body.text)
post.hidden = True
post.save()
self.browser.get(self.live_server_url+reverse('index'))
body = self.browser.find_element_by_tag_name('body')
self.assertNotIn(post.title, body.text)
self.browser.get(self.live_server_url+reverse('archive'))
body = self.browser.find_element_by_tag_name('body')
self.assertNotIn(post.title, body.text)
self.browser.get(self.live_server_url+reverse(
'tag', kwargs={'slug': post.tag.first().name}
))
body = self.browser.find_element_by_tag_name('body')
self.assertNotIn(post.title, body.text)
示例7: test_index
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_index(self):
self.browser.get(self.live_server_url+reverse('index'))
body = self.browser.find_element_by_tag_name('body')
for post in Post.objects.all()[:3]:
self.assertIn(post.title, body.text)
示例8: test_tag
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_tag(self):
tag = random.choice(Tag.objects.all())
self.browser.get(self.live_server_url+reverse(
'tag', kwargs={'slug': tag.name}
))
body = self.browser.find_element_by_tag_name('body')
for post in Post.objects.filter(tag__name=tag.name)[:3]:
self.assertIn(post.title, body.text)
示例9: test_archive
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def test_archive(self):
self.browser.get(self.live_server_url+reverse('archive'))
body = self.browser.find_element_by_tag_name('body')
for post in Post.objects.all()[:3]:
self.assertIn(post.title, body.text)
示例10: archive
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def archive(request):
posts = Post.objects.filter(hidden=False).all()
return render(request, 'blog/archive.html', {'posts': posts, 'title': 'Archive'})
示例11: editor
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def editor(request, slug=None):
if slug:
post = get_object_or_404(Post, slug=slug)
else:
post = Post()
if request.method == 'GET':
if request.user.is_authenticated():
return render(
request,
'blog/editor.html',
{'post': post, 'user': request.user}
)
else:
return HttpResponseForbidden('Authenticated only')
if request.is_ajax():
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
post.title = data['title']
post.content_raw = data['content']
post.author = request.user
post.slug = data['slug']
tags = data['tags']
post.save()
for tag in tags:
if tag:
t, created = Tag.objects.get_or_create(name=tag.strip())
post.tag.add(t)
post.save()
return HttpResponse(
json.dumps(
{'slug': post.slug}
)
)
示例12: get_queryset
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def get_queryset(self, **kwargs):
return Post.objects.filter(hidden=False).all()
示例13: get
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=Post.objects.all())
self.object_list = self.get_queryset()
context = self.get_context_data()
page_obj = context['page_obj']
html = ''
for comment in context['object_list']:
html += comment.to_html()
return JsonResponse({'html': html})
示例14: post
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def post(self, request, post_id):
post = get_object_or_404(Post, id=post_id)
post_type = ContentType.objects.get_for_model(post)
object, created = Favour.objects.get_or_create(content_type=post_type, object_id=post_id, user=request.user)
action = request.POST.get('action')
update = 1
if not created:
if action == 'like':
object.liked = True
else:
object.liked = False
update = -1
object.save()
count = post.favour_count(update=update)
return JsonResponse({'count': count})
示例15: comment
# 需要导入模块: from blog import models [as 别名]
# 或者: from blog.models import Post [as 别名]
def comment(request, post_pk):
# 先获取被评论的文章,因为后面需要把评论和被评论的文章关联起来。
# 这里我们使用了 Django 提供的一个快捷函数 get_object_or_404,
# 这个函数的作用是当获取的文章(Post)存在时,则获取;否则返回 404 页面给用户。
post = get_object_or_404(Post, pk=post_pk)
# django 将用户提交的数据封装在 request.POST 中,这是一个类字典对象。
# 我们利用这些数据构造了 CommentForm 的实例,这样就生成了一个绑定了用户提交数据的表单。
form = CommentForm(request.POST)
# 当调用 form.is_valid() 方法时,Django 自动帮我们检查表单的数据是否符合格式要求。
if form.is_valid():
# 检查到数据是合法的,调用表单的 save 方法保存数据到数据库,
# commit=False 的作用是仅仅利用表单的数据生成 Comment 模型类的实例,但还不保存评论数据到数据库。
comment = form.save(commit=False)
# 将评论和被评论的文章关联起来。
comment.post = post
# 最终将评论数据保存进数据库,调用模型实例的 save 方法
comment.save()
messages.add_message(request, messages.SUCCESS, '评论发表成功!', extra_tags='success')
# 重定向到 post 的详情页,实际上当 redirect 函数接收一个模型的实例时,它会调用这个模型实例的 get_absolute_url 方法,
# 然后重定向到 get_absolute_url 方法返回的 URL。
return redirect(post)
# 检查到数据不合法,我们渲染一个预览页面,用于展示表单的错误。
# 注意这里被评论的文章 post 也传给了模板,因为我们需要根据 post 来生成表单的提交地址。
context = {
'post': post,
'form': form,
}
messages.add_message(request, messages.ERROR, '评论发表失败!请修改表单中的错误后重新提交。', extra_tags='danger')
return render(request, 'comments/preview.html', context=context)