Exemplo n.º 1
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    #ensure only the author can update the post
    if post.author != current_user:
        abort(403)

    form = PostForm()

    #validate form on submit
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('post', post_id=post.id))

    #populate when it is a GET request
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content

    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Exemplo n.º 2
0
def add_post(request):
	context = RequestContext(request)
	if request.method == 'POST':
		form = PostForm(request.POST, request.FILES)
		if form.is_valid():
			form.save(commit = True)
			return HttpResponse(post(request, Post.objects.order_by('-created_at')[0].slug))
		else:
			print form.errors
	else:
		form = PostForm()
	return render_to_response('blog/add_post.html', {'form': form},
		context)
Exemplo n.º 3
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')
Exemplo n.º 4
0
def form_name_view(request):
    form=PostForm()
    if request.method=='POST':
        form=PostForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            # return index(request)
        else:
            print('Error')

    return render(request, 'blog_app/blog_form.html',{'form':form})
def create_post(request):
    page_title = "Create Post"
    form = PostForm(request.POST or None, request.FILES or None)
    if request.method == "POST":
        if form.is_valid():
            myfrom = form.save(commit=False)
            myfrom.author = request.user
            myfrom.save()
            form.save_m2m()  # save many 2 many field must be like that
            return redirect(reverse('blog:posts'))
        else:
            form = PostForm()

    context = {
        'create_or_update_post_form': form,
        'page_title': page_title,
    }
    return render(request, 'post/post_create_or_update.html', context)
Exemplo n.º 6
0
def add_post(request):
    form = PostForm()
    msg = ""
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            if 'poster' in request.FILES:
                obj.poster = request.FILES['poster']
            obj.author = request.user.username
            obj.organisation = request.user.profile.organisation
            obj.author_image = request.user.profile.profile_pic
            obj.save()
            msg = 'created'
        else:
            msg = "Not Submitted"
    else:
        form = PostForm()
        msg = ''
    return render(request, 'blog_app/add_post.html', {
        'form': form,
        'msg': msg
    })
Exemplo n.º 7
0
def index(request, post_form=None, comment_form=None, username=''):
    post_form = post_form or PostForm()
    comment_form = comment_form or CommentForm()
    post = Post.objects.all().filter(author__exact=request.user)
    comments = Comment.objects.all()
    context = {
        'post_form': post_form,
        'comment_form': comment_form,
        'post_list': post,
        'comments_list': comments,
    }
    if username:
        user = get_object_or_404(User, username=username)
        post = Post.objects.all().filter(author__exact=user)
        context = {
            'post_form': post_form,
            'comment_form': comment_form,
            'post_list': post,
            'comments_list': comments,
        }
        return render(request, template_name='index.html', context=context)
    return render(request, template_name='index.html', context=context)
def post_update(
    request, slug
):  #the same view and template create_post with minor change "instance=post_details"
    page_title = "Edit Post"
    post_details = Post.objects.get(slug=slug)
    form = PostForm(
        request.POST or None, request.FILES or None, instance=post_details
    )  # use "instance=" to give the form the current data in the form
    if request.method == "POST":
        if form.is_valid():
            myfrom = form.save(commit=False)
            myfrom.save()
            form.save_m2m()  # save many 2 many field must be like that
            return redirect(reverse('blog:post_details',
                                    args=(slug, )))  # the way to redirect slug
        else:
            form = PostForm()

    context = {
        'create_or_update_post_form': form,
        'page_title': page_title,
    }
    return render(request, 'post/post_create_or_update.html',
                  context)  # different urls for the same html page