本文整理汇总了Python中blog.forms.PostForm类的典型用法代码示例。如果您正苦于以下问题:Python PostForm类的具体用法?Python PostForm怎么用?Python PostForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PostForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edit_blog_post
def edit_blog_post(request, post_id, post_slug):
"""Render the page edit an existing post."""
post = get_object_or_404(Post, id=post_id, slug=post_slug)
current_user = request.user
# Only the author can edit the post, and the author must still be listed
# as a contributor.
if (current_user != post.author or
current_user not in post.blog.authors.all()):
raise Http404
if request.method == 'POST':
form = PostForm(request.POST, instance=post)
if form.is_valid():
return save_post_and_redirect(form, request)
else:
form = PostForm(instance=post)
form.fields['author'].widget = forms.HiddenInput()
# Limit blogs dropdown to those the current user can edit
blogs = Blog.objects.filter(authors__in=[current_user])
form.fields['blog'].queryset = blogs
context = {'form': form, 'post': post}
return render(request, 'edit_post.html', context)
示例2: new_blog_post
def new_blog_post(request):
"""Render the page to create a new post."""
current_user = request.user
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
# TODO: I think this check is overkill, but maybe good to have
if (current_user != form.cleaned_data['author'] or
current_user not in
form.cleaned_data['blog'].authors.all()):
raise Http404
return save_post_and_redirect(form, request)
else:
blogs = Blog.objects.filter(authors__in=[current_user])
if 'blog' in request.GET:
blog = get_object_or_404(Blog, slug=request.GET.get('blog'))
if blog not in blogs:
blog = None
else:
blog = None
form = PostForm(initial={'blog': blog, 'author': current_user})
form.fields['author'].widget = forms.HiddenInput()
form.fields['blog'].queryset = blogs
context = {'form': form}
return render(request, 'new_post.html', context)
示例3: post_edit
def post_edit(request, pk):
# To check that the post is existing or not
post = get_object_or_404(Post, pk=pk)
# post = Post.objects.get(pk=pk)
# To check about user
if request.user != post.author:
return redirect('blog.views.errors')
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
# post.published_date = timezone.now()
post.save()
if request.FILES.getlist("images", []):
for resource in post.resources.all():
resource.delete()
for image in request.FILES.getlist("images", []):
photo = Resource(post=post, image_file=image)
photo.save()
return redirect('blog.views.post_detail', pk=post.pk)
else:
return redirect('blog.views.post_detail', pk=post.pk)
else:
postForm = PostForm(instance=post)
formset = ResourceForm(instance=post)
return render(request, 'blog/post_edit.html', {'postForm': postForm, 'formset': formset}, context_instance=RequestContext(request))
示例4: view_post
def view_post(request, post_id):
form = PostForm()
blog_post = BlogPost.get_by_id(int(post_id))
if request.method == 'POST':
submitted_form = PostForm(request.POST)
if submitted_form.is_valid():
title = submitted_form.cleaned_data['title']
body = submitted_form.cleaned_data['body']
comment = Comment(title=title, body=body,
parent=blog_post)
comment.put()
return redirect(request.path)
else:
form = submitted_form
comments = Comment.all()
comments.ancestor(blog_post.key())
comments.order('-created')
return render(request, 'post_detail.html', {
'post': blog_post,
'comments': comments,
'form': form,
'auth_string': _get_auth_string(request.path),
'current_user': users.get_current_user(),
'is_admin': users.is_current_user_admin(),
})
示例5: update
def update(request, id):
form = PostForm(request.POST, instance=Post.objects.get(pk=id))
if form.is_valid():
post = form.save()
post.add_tags(request.POST['tags'])
return redirect(post.uri())
return render(request, 'blog/edit.html', {'post':post})
示例6: create_post
def create_post(request, slug):
blog = get_object_or_404(Blog, slug=slug)
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
slug = generate_slug(post.title)
conflicts = Post.objects.filter(slug__startswith=slug)
if conflicts:
slug = get_unique_slug(slug, conflicts)
post.slug = slug
post.author = request.user
post.blog = blog
post.save()
# We don't want to start sending garbage requests while testing,
# and we only want to do this on the first save of a published post,
# not every update, otherwise we might get IP banned.
if not settings.DEBUG:
post.send_pingbacks()
return redirect('view_post', post.created.year, post.get_formatted_month(), post.slug)
else:
form = PostForm()
context = {
'blog': blog,
'post_form': form,
}
return render_to_response('blog/create_post.html', context, RequestContext(request))
示例7: post_new
def post_new(request):
# ImageFormSet = modelformset_factory(Resource, form=ResourceForm, extra=3)
if request.method == "POST":
postForm = PostForm(request.POST)
formset = ResourceForm(request.POST, request.FILES)
if postForm.is_valid():
post = postForm.save(commit=False)
post.author = request.user
post.save()
for image in request.FILES.getlist("images", []):
# img = image_resizing(image)
photo = Resource(post=post, image_file=image)
photo.save()
return redirect('blog.views.post_detail', pk=post.pk)
# for form in formset.cleaned_data:
# image = form['image_file']
# photo = Resource(post=post, image_file=image)
# photo.save()
else:
postForm = PostForm()
formset = ResourceForm()
return render(request, 'blog/post_edit.html', {'postForm': postForm, 'formset': formset}, context_instance=RequestContext(request))
示例8: new
def new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.ip = request.META['REMOTE_ADDR']
# AWS Load Balancer 를 쓸 경우
# settings.USE_X_FORWARDED_HOST = True 여야,
# HTTP_X_FORWARDED_HOST META 정보에 값이 설정됩니다.
# post.ip = request.META['HTTP_X_FORWARDED_HOST']
post.save()
# post.author = request.user
# post = form.save(commit=False)
# post.category = get_object_or_404(Category, pk=1)
# post.save()
return redirect(post)
else:
form = PostForm()
return render(request, 'blog/form.html', {
'form': form,
})
示例9: show_index
def show_index(request):
user = User.objects.get(username='kamal')
context = {}
if request.method == 'POST':
form = PostForm(request.POST)
context['form'] = form
#import pdb;pdb.set_trace()
if form.is_valid():
title = form.cleaned_data['title']
text = form.cleaned_data['text']
post = Post(title=title, text=text)
post.author = user
post.save()
return redirect(reverse('posts-index'))
else:
form = PostForm()
context['form'] = form
posts = Post.objects.all()
context['posts'] = posts
#return HttpResponse(json.dumps(data), content_type='application/json')
return render(request, 'posts.html', context)
示例10: post
def post(self, request, *args, **kwargs):
print "post"
create_post = PostForm(request.POST)
if create_post.is_valid():
print "valid"
create_post.save()
return HttpResponseRedirect("/")
示例11: post
def post(self, request, slug=None, *args, **kwargs):
if not users.is_current_user_admin():
return HttpResponseForbidden()
post = self.get_object()
if not post and slug:
raise Http404()
form = PostForm(request.POST)
if not form.validate():
error_msg = [
"There are problems with the form:",
]
error_msg = itertools.chain(error_msg, *form.errors.values())
return HttpResponseBadRequest("<br/>".join(error_msg))
# title has to exit at that point due to the validators
new_slug = slugify(form.data["title"])
if slug is None and Post.get_by_slug(new_slug):
return HttpResponseBadRequest("Post with this title alread exit")
created = False
if not post:
created = True
post = Post()
form.populate_obj(post)
post.author = users.get_current_user().nickname()
post.put()
context = self.get_context_data(post=post, short=created)
return self.render_to_response(context)
示例12: post_update
def post_update(request, post_id=None):
if not post_id:
return redirect('blog:index')
title = 'DevInCafe | UPDATE'
context = {
'WEB_TITLE': title,
}
try:
post = Post.objects.get(id=post_id, author=request.user)
except ObjectDoesNotExist:
return redirect('blog:index')
if request.method == 'POST':
form = PostForm(request.POST, instance=post)
if form.is_valid():
form.save()
return redirect('blog:index')
else:
form = PostForm(instance=post)
context.update({'form': form})
return render(request, 'blog/pages/add_post.html', context)
示例13: edit
def edit(request, pk):
post = get_object_or_404(Post, pk=pk)
# 수정하려는 사용자와 작성한 유저가 다르면 에러
if post.author != request.user:
pass
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save()
# post = form.save(commit=False)
# post.author = request.user
# post.save()
# 만약 폼에서 지정하지 못한 필수 필드가 있다면 아래와 같이
# 서버딴에서 필수 필드의 기본값을 설정하여 주는 방법으로 해결
# post = form.save(commit=False)
# post.category = get_object_or_404(Category, pk=1)
# post.save()
# return redirect('blog:detail', post.pk)
# models.py에서 get_absolute_url 함수가 정의된 경우 post만 넘겨도 가능하다.
return redirect(post)
else:
form = PostForm(instance=post)
return render(request, "blog/form.html", {"form": form})
示例14: add
def add():
form = PostForm(request.form)
if request.method == "POST" and form.validate():
post = Post(title=form.title.data, slug=form.slug.data, content=form.content.data)
post.put()
return redirect(post.get_url())
return render_template("form.html", form=form, section="add")
示例15: admin_edit_post
def admin_edit_post(request, post_id):
if users.is_current_user_admin():
post = Post.get_by_id(int(post_id))
if not post:
raise Http404
if request.method == 'GET':
tp = Tag_Post.all().filter('post', post)
tags = ''
# Get all tags
for tag in tp:
tags += tag.tag.title + ','
form = PostForm({'title':post.title, 'category':post.category.key(), 'content':post.content, 'tags':tags})
elif request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
# delete related tag_post
for tp in post.tags:
tp.delete()
p = form.save(commit=False)
post.author = users.get_current_user()
post.category = p.category
post.content = p.content
post.put()
# add tag_post
tagText = request.POST['tags']
if tagText:
tags = tagText.split(',')
for tag in tags:
if tag:
tag = string.lower(string.strip(tag))
t = Tag.all().filter("title = ", unicode(tag, "utf-8")).get()
if not t:
t = Tag(title=unicode(tag, "utf-8"))
t.put()
Tag_Post(tag=t, post=post).put()
return HttpResponseRedirect('/admin')
return render_to_response('admin/edit.html',
dictionary={ 'form':form,
'type': 'Edit Post',
},
context_instance=RequestContext(request)
)
else:
return HttpResponseRedirect('/')