def create(request, template_name='main/form.html'): form = BlogForm(request.POST or None) if form.is_valid(): form.save() messages.success(request, 'Post has been created') return redirect('index') return render(request, template_name, {'form': form})
def blog_edit_formset(request,id): blog = Blog.objects.get(id=id) form = BlogForm(request.POST if request.method =="POST" else None , instance=blog,prefix='blog') medias = media_formset(request,blog.medias.all()) if request.method == "POST": if form.is_valid() : form.save() if medias.is_valid(): for media in medias.forms: if media.is_valid() and media.instance.data: if media.cleaned_data.get('removing',False): blog.medias.remove(media.instance) else: media.instance.user = request.user if request.user.is_authenticated() else None media.save() blog.medias.add(media.instance) else: #: error handling print medias.errors #: for increase medias.extra_forms after adding new mediafile medias = media_formset(None,blog.medias.all()) return render_to_response('blogs/blog/edit_formset.html', {'form': form,'medias':medias, }, context_instance=template.RequestContext(request))
def edit(request, id=None): if not request.user.is_staff or not request.user.is_superuser: raise Http404 instance = get_object_or_404(Blog, id=id) # check just owner and superuser can edit current post if not request.user.is_superuser and request.user != instance.owner: raise PermissionDenied form = BlogForm(request.POST or None, request.FILES or None, instance=instance) if form.is_valid(): instance = form.save(commit=False) instance.owner = request.user form.save() messages.success(request, _('updated successfully!!'), extra_tags='alert alert-success') return HttpResponseRedirect(instance.get_absolute_url()) context = { 'form': form, } return render(request, 'edit.html', context)
def update(request, blog_id, template_name='main/form.html'): post = get_object_or_404(Blog, pk=blog_id) form = BlogForm(request.POST or None, instance=post) if form.is_valid(): form.save() messages.success(request, 'Post has been updated') return redirect('index') return render(request, template_name, {'form': form})
def add_blog(request): if 'POST' == request.method: form = BlogForm(request.POST) if form.is_valid(): # save the data form.save() messages.success(request, 'Blog details saved successfully.') return redirect('home') else: # error message messages.error(request, 'Sumbitted form is invalid.') else: form = BlogForm() return render(request, 'blog/form.html', { 'form': form, 'action_title': 'Add Blog'})
def create(request, template_name='main/form.html'): """ View to create a new blog post. Just for the SuperUser. """ if request.user.is_superuser: form = BlogForm(request.POST or None) if form.is_valid(): form.save() messages.success(request, 'Post has been created') return redirect('index') return render(request, template_name, {'form': form}) else: messages.success(request, 'You are not authorized') return redirect('index')
def update(request, blog_id, template_name='main/form.html'): """ View to update a particular blog post. Just for the SuperUser. """ if request.user.is_superuser: post = get_object_or_404(Blog, pk=blog_id) form = BlogForm(request.POST or None, instance=post) if form.is_valid(): form.save() messages.success(request, 'Post has been updated') return redirect('index') return render(request, template_name, {'form': form}) else: messages.success(request, 'You are not authorized') return redirect('index')
def new_blog(request): if request.method == "POST": form = BlogForm(request.POST) if form.is_valid(): _blog = form.save(commit=False) req_url = BASE_URL % (_blog.name, TEST_API_KEY) # parse json req = requests.get(req_url) jsonlist = json.loads(req.content) response = jsonlist['response'] posts = response['posts'] blog = response['blog'] # for post in posts: # print post['body'] _blog.title = blog['title'] _blog.url = blog['url'] _blog.save() for post in posts: article = Article() article.title = post['title'] article.url = post['post_url'] article.description = post['body'] # article.published_date = # datetime.datetime.fromtimestamp(float(post['timestamp'])) # print article.published_date article.blog = _blog article.save() return redirect('blogs.views.articles_list') else: form = BlogForm() return render(request, "new_blog.html", {"form": form})
def add_blog(request): if request.method == 'POST': form = BlogForm(request.POST) if form.is_valid(): instance = form.save(commit=False) instance.author = request.user instance.save() return HttpResponseRedirect('/blogs') else: return HttpResponse('Some error occured please try again later.')
def blog_edit_simple(request,id): blog = Blog.objects.get(id=id) if request.method == "GET": form = BlogForm(instance=blog,prefix='blog') media_form = MediaFileForm(prefix='media') else: form = BlogForm(request.POST,instance=blog,prefix='blog') media_form = MediaFileForm( request.POST,request.FILES,prefix='media') if form.is_valid() : form.save() if media_form.is_valid(): media_form.instance.user = request.user media_form.instance.slug = uuid.uuid1().hex media_form.save() blog.medias.add(media_form.instance) media_form = MediaFileForm(prefix='media') return render_to_response('blogs/blog/edit_simple.html', {'form': form,'media_form':media_form, }, context_instance=template.RequestContext(request))
def create(request): if not request.user.is_staff or not request.user.is_superuser: raise Http404 form = BlogForm( request.POST or None, request.FILES, ) if form.is_valid(): instance = form.save(commit=False) instance.owner = request.user instance.save() messages.success( request, _('created successfully! <a href="/">See the item</a>'), extra_tags='alert alert-success safe') context = { 'form': form, } return render(request, 'create.html', context)
def blog(request): latest_blog_list = Blog.objects.order_by('-created_time')[:5] # latest_comment_list = Comment.objects.order_by('-comment_date')[:5] template = loader.get_template('blogs.html') context = RequestContext( request, { 'latest_blog_list': latest_blog_list, # 'latest_comment_list':latest_comment_list, 'template_name': 'blogs.html', 'form_blog': BlogForm, 'form_comment': CommentForm, 'success_url': '/thanks/', }) if request.method == 'POST': if 'blog_body' in request.POST: form = BlogForm(request.POST) elif 'comment_data' in request.POST: form = CommentForm(request.POST) if form.is_valid(): blog_form = form.save(commit=False) if 'comment_data' in request.POST: blog = Blog.objects.get(id=request.POST['blog']) comment_count = blog.comment_set.aggregate( Count('comment_data')).values() if comment_count[0] != None: comment_count = blog.comment_set.aggregate( Count('comment_data')).values()[0] else: comment_count = int(request.POST['comment_data']) blog.overall_comment = comment_count blog.save() # commit=False tells Django that "Don't send this to database yet. # I have more things I want to do with it." blog_form.user = User.objects.get(username=request.user) # Set the user object here blog_form.save() # Now you can send it to DB return HttpResponse(template.render(context))
def blog(request, id=None): if id != None: latest_blog_list = Blog.objects.filter(id=id) else: latest_blog_list = Blog.objects.order_by('-created_date')[:5] # latest_comment_list = Comment.objects.order_by('-comment_date')[:5] template = loader.get_template('blogs.html') context = RequestContext(request, { 'latest_blog_list': latest_blog_list, # 'latest_comment_list':latest_comment_list, 'template_name': 'blogs.html', 'form_blog': BlogForm, 'form_comment': CommentForm, 'success_url': '/thanks/', }) if request.method == 'POST': if 'blog_body' in request.POST: form = BlogForm(request.POST) elif 'comment_data' in request.POST: form = CommentForm(request.POST) if form.is_valid(): blog_form = form.save(commit=False) if 'comment_data' in request.POST: blog = Blog.objects.get(id=request.POST['blog']) comment_count = blog.comment_set.aggregate(Count('comment_data')).values() if comment_count[0] != None: comment_count = blog.comment_set.aggregate(Count('comment_data')).values()[0] else: comment_count = int(request.POST['comment_data']) blog.overall_comment = comment_count blog.save() # commit=False tells Django that "Don't send this to database yet. # I have more things I want to do with it." blog_form.user = User.objects.get(username=request.user) # Set the user object here blog_form.save() # Now you can send it to DB return HttpResponse(template.render(context))