コード例 #1
0
ファイル: views.py プロジェクト: dasmall/simple-django-blog
def update_post(request, slug):
	# Check if a user is logged in.
	if not request.user.is_authenticated():
		# No user is logged in. Must be logged in to create/update a post.
		return HttpResponseRedirect('/login/?next=%s' % request.path)
	else:
		# User is logged in.
		# Check whether a slug has been provided to the view.
		if slug == '':
			# No slug has been provided. Instantiate a blank form object.
			form = PostForm()
		else:
			# A slug exists. Display relevant post object in a form for editing.
			post = get_object_or_404(Post, slug=slug)
			form = PostForm(instance=post)

		# Check whether the form has been submitted.
		if request.method == 'POST':
			# Check whether or not form submission happened with a pre-existing slug.
			if slug !='':
				# Pre-existing post slug exists.
				form = PostForm(request.POST, instance=post)
			elif slug =='':
				# No pre-existing slug. 
				form = PostForm(request.POST)	
			if form.is_valid():
				link = form.save(commit=False)
    			# Explicitly associate current user with post object form update.
				link.user = request.user
				link.save()
			return HttpResponseRedirect('../')

		csrfContext = RequestContext(request,{
		"form": form})
		return render_to_response('update_post.html', csrfContext)
コード例 #2
0
def post(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('blog-home')
    else:
        form = PostForm()
コード例 #3
0
ファイル: views.py プロジェクト: osufitchi/Blog-App
def create_post(request):
    if request.method == 'POST':
       post = PostForm(request.POST)
       if post.is_valid():
           post.save()
           return HttpResponseRedirect('/blog')
    else:
        post = PostForm()
    return render_to_response('blog/create_post.html',{'post':post})
コード例 #4
0
ファイル: views.py プロジェクト: tmyyss/Blog
def update(request,pk):
	mblogs=Blog.objects.all().order_by('-visitors')[:5]
	post=get_object_or_404(Blog,pk=pk)
	if request.method=="POST":
                form=PostForm(request.POST,instance=post)
		if form.is_valid():
			post=form.save(commit=False)
			post.user=request.user
			post.save()
			return render(request,'blog/detail.html',{'blog':post,'mblogs':mblogs})
	else:
		form=PostForm(instance=post)
        return render(request,'blog/update.html',{'form':form,'mblogs':mblogs})
コード例 #5
0
ファイル: views.py プロジェクト: mccormickt12/django_blog
def added(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            t = form.cleaned_data['text']
            p = Post.objects.create(text=t)
            p.save()
            return redirect("/" + str(p.id),
                            context_instance=RequestContext(request))
        else:
            raise Http404
    else:
        raise Http404
コード例 #6
0
ファイル: views.py プロジェクト: mccormickt12/django_blog
def added(request):
	if request.method == 'POST':
		form = PostForm(request.POST)
		if form.is_valid():
			t = form.cleaned_data['text']
			p = Post.objects.create(text=t)
			p.save()
			return redirect("/" + str(p.id),
			 context_instance=RequestContext(request))
		else:
			raise Http404
	else:
		raise Http404
コード例 #7
0
ファイル: views.py プロジェクト: tmyyss/Blog
def edit(request):
	mblogs=Blog.objects.all().order_by('-visitors')[:5]
	if request.method=='POST':
		form=PostForm(request.POST)
		if form.is_valid():
			post=form.save(commit=False)
			post.user=request.user
			post.created_time=datetime.datetime.now()
			post.published_time=datetime.datetime.now()
			post.save()
			return render(request,'blog/detail.html',{'blog':post,'mblogs':mblogs})
	else:
		form=PostForm()
	return render(request,'blog/edit.html',{'form':form,'mblogs':mblogs})
コード例 #8
0
ファイル: views.py プロジェクト: encima/potato-blog
def edit_post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    if(request.method == 'POST'):
        form = PostForm(request.POST)
        if(form.is_valid()):
            post.title = form.cleaned_data['title']
            post.body = form.cleaned_data['body']
            post.slug = urllib.quote_plus(form.cleaned_data['title'])
            post.tag = form.cleaned_data['tag']
            post.save()
            return HttpResponseRedirect('/blog/view/' + post.slug)

    print post
    res_dict = {'title': 'Edit Post', 'form': PostForm(instance=post), 'submit': '/blog/edit/' + slug}
    return render(request, 'new_post.html', res_dict) 
コード例 #9
0
ファイル: views.py プロジェクト: nidstang/examen-verificacion
def create_post(request):
	if request.method == "POST":
		postForm = PostForm(request.POST)

		if postForm.is_valid():
			title = postForm.cleaned_data['title']
			content = postForm.cleaned_data['content']

			new_post = Post.objects.create_post(title, content)
			new_post_form = PostForm()
			if new_post:
				return render(request, "create.html", {"post_form": new_post_form, 'success': True})
			else:
				return render(request, "create.html", {"post_form": new_post_form, 'error': True})
	else:
		postForm = PostForm()
		return render(request, "create.html", {"post_form": postForm})
コード例 #10
0
ファイル: views.py プロジェクト: crodjer/oldies
def new(request):
    if not request.user.is_staff:
        messages.error(request, _('You dont have the permissions to create new posts.'))
        return redirect(index)
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid(): 
            post = form.save(commit=False)
            post.user = request.user
            post.save()
            post.subscribers.add(request.user)
            messages.success(request, 'Your post "%s" was published' % (post.title))
            return redirect(post)
        messages.error(request, _('Some errors were found in your post.'))
    else:
        form = PostForm()
    return render_to_response('blog/post_new.html', {'form': form, 'blog':True, 'django_comments':DJANGO_COMMENTS}, context_instance=RequestContext(request))
コード例 #11
0
ファイル: views.py プロジェクト: crodjer/oldies
def edit(request, post_slug):
    post = get_object_or_404(Post, slug=post_slug)
    redirect_url = request.GET.get('next', post)
    if not request.user.has_perm('blog.change_post'):
        messages.error(request, _('You dont have the permissions to edit this post.'))
        return redirect(post)
    if request.method == 'POST':
        if request.POST['submit'] == "Save":
            redirect_url = request.META['PATH_INFO']
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save()
            post.subscribers.add(request.user)
            messages.success(request, 'Your post "%s" was updated.' % (post.title))
            return redirect(redirect_url)
        messages.error(request, _('Some errors were found in your post.'))
    else:
        form = PostForm(instance=post)
    return render_to_response('blog/post_edit.html', {'form': form, 'post':post, 'blog':True, 'django_comments':DJANGO_COMMENTS}, context_instance=RequestContext(request))
コード例 #12
0
def edit_post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    if (request.method == 'POST'):
        form = PostForm(request.POST)
        if (form.is_valid()):
            post.title = form.cleaned_data['title']
            post.body = form.cleaned_data['body']
            post.slug = urllib.quote_plus(form.cleaned_data['title'])
            post.tag = form.cleaned_data['tag']
            post.save()
            return HttpResponseRedirect('/blog/view/' + post.slug)

    print post
    res_dict = {
        'title': 'Edit Post',
        'form': PostForm(instance=post),
        'submit': '/blog/edit/' + slug
    }
    return render(request, 'new_post.html', res_dict)
コード例 #13
0
ファイル: views.py プロジェクト: encima/potato-blog
def new_post(request):
    res_dict = {'title': 'New Post', 'form': PostForm(), 'submit': '/blog/new/'}

    if(request.method == 'POST'):
        form = PostForm(request.POST)
        res_dict['form'] = form
        if(form.is_valid()):
            new = form.save(commit=False)
            new.slug = urllib.quote_plus(new.title)
            new.pub_date = datetime.datetime.now() 
            try:
                new.save()
            except IntegrityError as e:
                res_dict['error'] = 'Sorry, blog title already exists and must be unique'
                return render(request, 'new_post.html', res_dict)
            return HttpResponseRedirect('/blog/view/' + new.slug)
        else:
            res_dict['error'] = 'Sorry, fields cannot be blank'
    return render(request, 'new_post.html', res_dict)
コード例 #14
0
ファイル: views.py プロジェクト: joskid/mldata
def new(request):
    """View to make a new blog post.

    @param request: request data
    @type request: Django request
    @return a view page to make a new blog post
    @rtype: Django response
    """
    if not request.user.is_authenticated():
        redirect_to = reverse('user_signin') + '?next=' + reverse(new)
        return HttpResponseRedirect(redirect_to)

    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            if request.POST.has_key('preview'):
                preview = {
                    'headline': form.cleaned_data['headline'],
                    'summary': form.cleaned_data['summary'],
                    'body': form.cleaned_data['body'],
                }
            else:
                post = Post()
                post.pub_date = datetime.datetime.now()
                post.headline = form.cleaned_data['headline']
                post.summary = form.cleaned_data['summary']
                post.body = form.cleaned_data['body']
                post.author_id = request.user.id
                post.save()
                return HttpResponseRedirect(post.get_absolute_url())
    else:
        preview = False
        form = PostForm()

    info_dict = {
        'request': request,
        'form': form,
        'preview': preview,
        'section': 'blog',
        'user': request.user,
    }
    return render_to_response('blog/new.html', info_dict)
コード例 #15
0
ファイル: views.py プロジェクト: silky/mldata
def new(request):
    """View to make a new blog post.

    @param request: request data
    @type request: Django request
    @return a view page to make a new blog post
    @rtype: Django response
    """
    if not request.user.is_authenticated():
        redirect_to = reverse("user_signin") + "?next=" + reverse(new)
        return HttpResponseRedirect(redirect_to)

    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            if request.POST.has_key("preview"):
                preview = {
                    "headline": form.cleaned_data["headline"],
                    "summary": form.cleaned_data["summary"],
                    "body": form.cleaned_data["body"],
                }
            else:
                post = Post()
                post.pub_date = datetime.datetime.now()
                post.headline = form.cleaned_data["headline"]
                post.summary = form.cleaned_data["summary"]
                post.body = form.cleaned_data["body"]
                post.author_id = request.user.id
                post.save()
                return HttpResponseRedirect(post.get_absolute_url())
    else:
        preview = False
        form = PostForm()

    info_dict = {"request": request, "form": form, "preview": preview, "section": "blog", "user": request.user}
    return render_to_response("blog/new.html", info_dict)
コード例 #16
0
def new_post(request):
    res_dict = {
        'title': 'New Post',
        'form': PostForm(),
        'submit': '/blog/new/'
    }

    if (request.method == 'POST'):
        form = PostForm(request.POST)
        res_dict['form'] = form
        if (form.is_valid()):
            new = form.save(commit=False)
            new.slug = urllib.quote_plus(new.title)
            new.pub_date = datetime.datetime.now()
            try:
                new.save()
            except IntegrityError as e:
                res_dict[
                    'error'] = 'Sorry, blog title already exists and must be unique'
                return render(request, 'new_post.html', res_dict)
            return HttpResponseRedirect('/blog/view/' + new.slug)
        else:
            res_dict['error'] = 'Sorry, fields cannot be blank'
    return render(request, 'new_post.html', res_dict)