本文整理汇总了Python中models.BlogPost.save方法的典型用法代码示例。如果您正苦于以下问题:Python BlogPost.save方法的具体用法?Python BlogPost.save怎么用?Python BlogPost.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.BlogPost
的用法示例。
在下文中一共展示了BlogPost.save方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_post
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def add_post(request):
if request.method == 'POST':
form = BlogPostForm(request.POST)
if form.is_valid():
new_post = BlogPost(
title=form.cleaned_data['title'],
content=form.cleaned_data['content'],
published=True,
author=request.user
)
new_post.save()
return redirect('view_posts', post_id=new_post.id)
else:
form = BlogPostForm()
return render(
request,
'add_post.html',
{
'form': form,
}
)
示例2: setUp
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def setUp(self):
# Every test needs access to the request factory.
self.user = get_user_model().objects.create_user(username='bmelton', email='[email protected]', password='top_secret')
self.factory = APIRequestFactory()
blog1 = BlogPost(id=1, author=self.user, title="Test Blog Title 1", body="This is a test blog article.")
blog1.save()
blog2 = BlogPost(id=2, author=self.user, title="Test Blog Title 2", body="This is another test blog article.")
blog2.save()
示例3: post
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def post(self):
form = BlogPostForm(self.request.POST)
if form.validate():
post = BlogPost(**form.data)
post.save()
self.redirect_to("admin-blog-post-edit-extra", post_id = post.key().id(), extra="saved")
return {
"admin_section": "admin-blog-posts-new",
"form": form,
}
示例4: add_post
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def add_post(request):
context = RequestContext(request)
if request.method == 'POST':
form = BlogPostForm(request.POST)
if form.is_valid():
blog = BlogPost()
blog.author = request.user
blog.title = request.POST['title']
blog.bodytext = request.POST['bodytext']
blog.save()
messages.add_message(request, SUCCESS, 'Post Added')
return HttpResponseRedirect('/main/blog/')
else:
messages.add_message(request, SUCCESS, 'Enter a valid Post')
return HttpResponseRedirect('/main/add/')
else:
form = BlogPostForm()
return render_to_response('blog/add.html', {'form': form}, context)
示例5: loaddir
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def loaddir(directory, clear=False):
if clear:
BlogPost.objects.all().delete()
queue = os.listdir(directory)
while queue:
next = queue.pop()
if next[0] == '.': continue
if next in ('template.rst', 'template'): continue
next = path.join(directory, next)
if path.isdir(next):
queue.extend([
path.join(next,f) for f in os.listdir(next)
])
continue
filecontent = file(next).read()
parts = filecontent.split('\n---\n', 1)
fields, content = parts
if len(parts) != 2:
raise IOError('gitcms.blog.load: expected "---" separator in file %s' % next)
fields = yaml.load(fields)
fields['timestamp'] = parsedatetime(fields['timestamp'])
fields['year_month_slug'] = time.strftime('%%Y/%%B/%s' % fields['slug'], fields['timestamp'])
fields['timestamp'] = time.strftime('%Y-%m-%d %H:%M', fields['timestamp'])
fields['content'] = preprocess_rst_content(content)
categories = fields.get('categories', '')
if 'categories' in fields: del fields['categories']
ptags = []
if categories:
for c in categories.split():
ptags.append(tag_for(c))
# if we arrived here and no errors, then it is safe
# to add our post.
#
P = BlogPost(**fields)
P.save()
for t in ptags:
P.tags.add(t)
示例6: loaddir
# 需要导入模块: from models import BlogPost [as 别名]
# 或者: from models.BlogPost import save [as 别名]
def loaddir(directory, clear=False):
if clear:
BlogPost.objects.all().delete()
queue = os.listdir(directory)
while queue:
next = queue.pop()
if next[0] == '.':
continue
if next in ('template.rst', 'template'):
continue
next = os.path.join(directory, next)
if os.path.isdir(next):
queue.extend([os.path.join(next, f) for f in os.listdir(next)])
continue
filecontent = file(next).read()
parts = filecontent.split('\n---\n', 1)
fields, content = parts
if len(parts) != 2:
err_msg = 'gitcms.blog.load: expected "---" separator in file %s'
raise IOError(err_msg % next)
fields = yaml.load(fields)
fields['content'] = preprocess_rst_content(content)
categories = fields.get('categories', '')
if 'categories' in fields:
del fields['categories']
ptags = []
if categories:
for c in categories.split():
ptags.append(tag_for(c))
P = BlogPost(**fields)
P.save()
for t in ptags:
P.tags.add(t)