本文整理匯總了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)