本文整理汇总了Python中blog.forms.PostForm.save_m2m方法的典型用法代码示例。如果您正苦于以下问题:Python PostForm.save_m2m方法的具体用法?Python PostForm.save_m2m怎么用?Python PostForm.save_m2m使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blog.forms.PostForm
的用法示例。
在下文中一共展示了PostForm.save_m2m方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_new
# 需要导入模块: from blog.forms import PostForm [as 别名]
# 或者: from blog.forms.PostForm import save_m2m [as 别名]
def post_new(request):
args = {}
if request.method == 'POST':
form = PostForm(request.POST or None, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
i=0
ingredients1 = Ingredient.objects.all()
post.published_date = timezone.now()
print(request.POST.getlist('quantity'))
for item in request.POST.getlist('quantity'):
i=i+1
if int(item) !=0:
w = Wasted.objects.all().get(pk=i)
w.weight = int(item)
w.save()
if Ingredient.objects.all().get(name=w.name, pk=i).weight >0:
ingr = Ingredient.objects.all().get(name=w.name, pk=i)
ingr.weight = ingr.weight - w.weight
ingr.save()
else:
args['login_error'] = "нет на складе продукта"
args['login_error'] = args['login_error']+w.name
return render_to_response('post_new', args)
post.save()
form.save_m2m()
# post.ingredients()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
posts = Ingredient.objects.all()
return render(request, 'blog/post_edit.html', {'form': form, 'posts': posts})
示例2: add_edit_post
# 需要导入模块: from blog.forms import PostForm [as 别名]
# 或者: from blog.forms.PostForm import save_m2m [as 别名]
def add_edit_post(request, slug=None):
if request.method == 'GET':
form = PostForm()
if slug: # If editing
post = get_object_or_404(Post, slug=slug)
if request.user.is_staff or request.user == post.author:
form = PostForm(instance=post)
else:
raise PermissionDenied
else: # POST
if not slug:
form = PostForm(request.POST)
else:
form = PostForm(request.POST, instance=get_object_or_404(Post, slug=slug))
if form.is_valid():
instance = form.save(commit=False)
if not getattr(instance, 'author'):
instance.author = request.user
if not request.user.is_staff:
instance.approved = False
instance.text = clean_untrusted_tags(instance.text)
instance.save()
form.save_m2m()
return render(request, 'add_post_success.html', {})
return render(request, 'add_edit_post.html', {
'form': form,
'edit': slug is not None
})
示例3: post_edit
# 需要导入模块: from blog.forms import PostForm [as 别名]
# 或者: from blog.forms.PostForm import save_m2m [as 别名]
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST or None, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
posts = Ingredient.objects.all()
post_numder = post.pk
for ing in request.POST.getlist('ingredients'):
theing = Ingredient.objects.get(pk=ing)
post.ingredients.add(theing.id)
post.save()
form.save_m2m()
# messages.success(request, "<a href='#'>Item</a> Saved", extra_tags='html_safe')
# return HttpResponseRedirect(instance.get_absolute_url())
context = {
"title": post.title,
"instance": post,
"form": form,
"posts":posts,
}
# return render(request, 'blog/post_edit.html', context)
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
posts = Ingredient.objects.all()
return render(request, "blog/post_edit.html", {'form': form, 'posts': posts})