def blog_add(request): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] for taglist in tagname.split(): Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['title'] content = cd['content'] user = request.user blog = Blog(title=title, user=user, content=content) blog.save() for taglist in tagname.split(): blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() id = Blog.objects.order_by('-published_time')[0].id return HttpResponseRedirect('/newblog/%s' % id) else: return render(request, 'newblog/blog_add.html', {'form': form, 'tag':tag}) else: form = BlogForm() tag = TagForm(initial={'tag_name': 'Notags'}) return render(request, 'newblog/blog_add.html', {'form': form, 'tag':tag})
def blog_update(request, blog_id=""): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] tagnamelist = tagname.split() for taglist in tagnamelist: Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['title'] content = cd['content'] blog = Blog.objects.get(pk=blog_id) user = blog.user if blog: blog.title = title blog.content = content blog.save() for taglist in tagnamelist: blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() tags = blog.tags.all() for tagname in tags: tagname = unicode(str(tagname), "utf-8") if tagname not in tagnamelist: notag = blog.tags.get(tag_name=tagname) blog.tags.remove(notag) else: blog = Blog(title=blog.title, content=blog.content, user=user) blog.save() return HttpResponseRedirect('/newblog/%s' % blog_id) else: return render(request, 'newblog/blog_add.html', {'form': form,'tag': tag}) else: try: blog = Blog.objects.get(id=blog_id) except Exception: raise Http404 form = BlogForm(initial={'title': blog.title, 'content': blog.content, 'user': blog.user}, auto_id=False) tags = blog.tags.all() if tags: taginit = '' for x in tags: taginit += str(x) + ' ' tag = TagForm(initial={'tag_name': taginit}) else: tag = TagForm(initial={'tag_name': 'Notags'}) return render(request, 'newblog/blog_add.html', {'blog': blog, 'form': form, 'blog_id': id, 'tag': tag})
def blog_add(request): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] for taglist in tagname.split(): Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['title'] content = cd['content'] user = request.user blog = Blog(title=title, user=user, content=content) blog.save() for taglist in tagname.split(): blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() id = Blog.objects.order_by('-published_time')[0].id return HttpResponseRedirect('/newblog/%s' % id) else: return render(request, 'newblog/blog_add.html', { 'form': form, 'tag': tag }) else: form = BlogForm() tag = TagForm(initial={'tag_name': 'Notags'}) return render(request, 'newblog/blog_add.html', { 'form': form, 'tag': tag })
def blog_update(request, blog_id=""): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] tagnamelist = tagname.split() for taglist in tagnamelist: Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['title'] content = cd['content'] blog = Blog.objects.get(pk=blog_id) user = blog.user if blog: blog.title = title blog.content = content blog.save() for taglist in tagnamelist: blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() tags = blog.tags.all() for tagname in tags: tagname = unicode(str(tagname), "utf-8") if tagname not in tagnamelist: notag = blog.tags.get(tag_name=tagname) blog.tags.remove(notag) else: blog = Blog(title=blog.title, content=blog.content, user=user) blog.save() return HttpResponseRedirect('/newblog/%s' % blog_id) else: return render(request, 'newblog/blog_add.html', { 'form': form, 'tag': tag }) else: try: blog = Blog.objects.get(id=blog_id) except Exception: raise Http404 form = BlogForm(initial={ 'title': blog.title, 'content': blog.content, 'user': blog.user }, auto_id=False) tags = blog.tags.all() if tags: taginit = '' for x in tags: taginit += str(x) + ' ' tag = TagForm(initial={'tag_name': taginit}) else: tag = TagForm(initial={'tag_name': 'Notags'}) return render(request, 'newblog/blog_add.html', { 'blog': blog, 'form': form, 'blog_id': id, 'tag': tag })