Exemplo n.º 1
0
def create_blog(request):
    """
    博客内容编辑提交类
    :param request:
    :return:
    """
    if request.method == 'POST':
        # 未使用django自带的表单模式
        # try:
        #     blog = BlogPost.objects.create(
        #         title=request.POST.get('title'),
        #         body=request.POST.get('body'),
        #         timestamp=datetime.now(),
        #     )
        #     print(blog)
        # except:
        #     print("失败")

        # 使用form类创建
        form = BlogPostForm(request.POST)
        if form.is_valid():
            # 由于时间参数,所以save时不能执行
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    # 通过HttpResponseRedirect重定向到指定页面
    return HttpResponseRedirect('/blog/')
Exemplo n.º 2
0
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    return HttpResponseRedirect('/blog/')
Exemplo n.º 3
0
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    return HttpResponseRedirect('/blog/')
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)  # this returns a BlogPost object
            #uncomment next line if timestamp has default value
            #post.timestamp = datetime.now()
            post.save()

    # redirect to parent page
    return HttpResponseRedirect('/blog/')
Exemplo n.º 5
0
def create_blogpost(request):
    if request.method == 'POST':

        # BlogPost(title=request.POST.get('title'), body=request.POST.get('body'), timestamp=datetime.now(),).save()

        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    return redirect('/blog/')
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False) #this returns a BlogPost object
            #uncomment next line if timestamp has default value
            #post.timestamp = datetime.now()
            post.save()

    # redirect to parent page
    return HttpResponseRedirect('/blog/')
Exemplo n.º 7
0
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
        # BlogPost(
        #     title = request.POST.get('title'),
        #     body = request.POST.get('body'),
        #     timestamp = datetime.now()
        # ).save()
        return HttpResponseRedirect('/blog')
Exemplo n.º 8
0
def create_blogpost(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        #form=BlogPostForm(initial={'title':'1'}
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            _saveBody(post.body)
            _compileCpp()
            result = _computeResult("{}result.txt".format(cppPath))
            post.body = "your score is " + str(result)
            post.save()

        return HttpResponseRedirect('/blog/')
def archive(request):
    # get the data from the table
    # posts = BlogPost.objects.all() # query the database for all blog entries
    # posts = BlogPost.objects.all().order_by('-timestamp') # descending timestamp
    # posts = BlogPost.objects.all().order_by('-timestamp')[:3] # only 3 per page
    # posts = BlogPost.objects.all()[:3] # default ordering in models.BlogPost class

    # for pagination
    posts_list = BlogPost.objects.all()
    paginator = Paginator(posts_list, 3)  # Show 3 blogposts per page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # if page is not an integer, deliver first page
        posts = paginator.page(1)
    except EmptyPage:
        # if page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)

    # use "render" over "render_to_response" (not sure why but do it)
    # update: in "render_to_response" we must add RequestContext(request) for CSRF protection
    # eg. render_to_response('archive.html', {'posts': posts,}, RequestContext(request))
    # in "render" we don't have to do this (that's why we always use "render")
    return render(request, 'archive.html', {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 10
0
def archive(request):
    posts = BlogPost.objects.all()[:10]
    #return render(request,'archive.html',{'posts': posts,'form': BlogPostForm()})
    return render(request, 'index.html', {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 11
0
def archive(request):
    # 在timestamp前面加上减号(-)指定按时间逆序排列。正常的升序只需要移除减号
    posts = BlogPost.objects.all()[:10]
    return render(request, 'archive.html', {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 12
0
def create_blogpost(request):
    if request.method == 'POST':
        # 检查POST请求
        # 创建新的BlogPost项,获取表单数据,并用当前时间建立时间戳。
        # BlogPost(
        #     title=request.POST.get('title'),
        #     body=request.POST.get('body'),
        #     timestamp=datetime.now()
        # ).save()
        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    # 重定向会/blog
    return HttpResponseRedirect('/blog')
Exemplo n.º 13
0
def archive(request):
    posts = BlogPost.objects.all().order_by('-timestamp')[:5]
#    t = loader.get_template("index.html")
##  c = Context({'posts': posts})
#    html = t.render({'posts': posts, 'form': BlogPostForm()})
 #   return HttpResponse(html,RequestContext(request))
    return render_to_response('index.html', {'posts': posts, 'form': BlogPostForm()}, RequestContext(request))
Exemplo n.º 14
0
def index(request):
	#post=BlogPost(title="mocktitle" ,body="mockbody",timestamp=datetime.now())
	#return render_to_response("index.html",{'posts':[post]})
	#return HttpResponse("<h1> hbfh xfhdfhsfdbdf </h1>")
	posts=BlogPost.objects.all()
	
	context={'posts':posts,'form':BlogPostForm()}
	return render(request,"blog/index.html",context)
Exemplo n.º 15
0
def archive(request):
    """
    页面显示
    :param request:
    :return:
    """
    posts = BlogPost.objects.all().order_by('-timestamp')
    form = BlogPostForm()
    return render(request, 'archive.html', locals())
Exemplo n.º 16
0
def archive(request):
    posts = BlogPost.objects.all()[:10]
    # return render_to_response('archive.html', {'posts': posts,
    #                             'form': BlogPostForm()})
    return TemplateView.as_view(template_name='archive.html',
                                extra_context={
                                    'posts': posts,
                                    'form': BlogPostForm()
                                })(request)
Exemplo n.º 17
0
def archive(request):
    #post = BlogPost(title='danzhaoxun',body='I LOVE YOU',
    #                timestamp=datetime.now())
    #posts = BlogPost.objects.all().order_by('-timestamp')[:10]
    posts = BlogPost.objects.all()[:10]

    return render_to_response('archive.html', {
        'posts': posts,
        'form': BlogPostForm()
    }, RequestContext(request))
Exemplo n.º 18
0
def archive(request):
    #post=BlogPost(title='mocktitle',body='mockbody',
    #	timestamp=datetime.now())
    posts = BlogPost.objects.all()[:1]
    #t=loader.get_template("archive.html")
    #c=Context({'posts':posts})
    #return HttpResponse(t.render(c))
    return render(request, "archive.html", {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 19
0
def blog_post_form(request):
    form = BlogPostForm()
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return render(request, 'post_success.html', {})
        else:
            print('FORM ERROR')

    return render(request, 'blog_post_form.html', {'form': form})
Exemplo n.º 20
0
def archive(request):
    posts = BlogPost.objects.all()
    return render(request, 'index.html', {
        'posts': posts,
        'form': BlogPostForm()
    }, RequestContext(request))
def archive(request):
    posts = BlogPost.objects.all()[:10]
    c = {'posts': posts,'form':BlogPostForm()}
    c.update(csrf(request))
    return render_to_response('archive.html',c)
Exemplo n.º 22
0
def archive(request):
    posts = BlogPost.objects.all()[:10]
    return render_to_response('archive.html', {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 23
0
def archive(request):
    posts = BlogPost.objects.all()[:10]
    return direct_to_template(request, 'archive.html',
        {'posts': posts, 'form': BlogPostForm()})
Exemplo n.º 24
0
from blog.models import BlogPost, BlogPostForm
from datetime import datetime
import django.http
from django.template import RequestContext

# Create your views here.


def archive(request):
    context = {}
    posts = BlogPost.objects.all().order_by('-timestamp')[:3]
    context['posts'] = posts  # context=Context({'posts':posts})
    return render(request, 'archive.html', context, RequestContext(request))


add = lambda req: render(req, 'add.html')

add_new = lambda req: render(req, 'add_new.html', {'form': BlogPostForm()})


def add_post(request):
    if request.method == 'POST':
        post = BlogPost(title=request.POST.get('title'),
                        body=request.POST.get('body'),
                        timestamp=datetime.now())

        post.save()
        return django.http.HttpResponseRedirect('/blog/')
    else:
        return django.http.HttpResponseForbidden()
Exemplo n.º 25
0
def archive(request):
    posts = BlogPost.objects.all()[:10]  # 倒序排列
    content = {"posts": posts, "form": BlogPostForm()}
    return render(request, 'archive.html', content)
Exemplo n.º 26
0
def foo(request):
    posts = BlogPost.objects.all()
    #t = loader.get_template("foo.html")
    #c = Context({'posts': posts}) 
    #return HttpResponse(t.render(c))
    return render_to_response('foo.html', {'posts': posts, 'form':BlogPostForm()} ,RequestContext(request))
Exemplo n.º 27
0
    posts = BlogPost.objects.all()
    t = loader.get_template('archive.html')
    c = {'posts': posts}
    return HttpResponse(t.render(c))
'''
'''
def archive(request):
    posts = BlogPost.objects.all()[:10]
    return render(request, 'archive.html', {'posts': posts})
'''

# archive = lambda req: render(req, 'archive.html', {'posts': BlogPost.objects.all()[11:20]})

archive = lambda req: render(req, 'archive.html', {
    'posts': BlogPost.objects.all()[:10],
    'form': BlogPostForm()
})


def create_blogpost(request):
    if request.method == 'POST':

        # BlogPost(title=request.POST.get('title'), body=request.POST.get('body'), timestamp=datetime.now(),).save()

        form = BlogPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.timestamp = datetime.now()
            post.save()
    return redirect('/blog/')
Exemplo n.º 28
0
def archive(request):
    posts = BlogPost.objects.all().order_by('-timestamp')[:10]
    return render(request, "archive.html", {
        'posts': posts,
        'form': BlogPostForm()
    })
Exemplo n.º 29
0
 def get_context_data(self, **kwargs):
     context = super(HomePageView, self).get_context_data(**kwargs)
     context['posts'] = BlogPost.objects.all().order_by('-timestamp')[:5]
     context['form'] = BlogPostForm()
     return context
Exemplo n.º 30
0
def archive(request):
    posts = BlogPost.objects.all().order_by('-timestamp')[:10]
    context = {'posts': posts, 'form': BlogPostForm()}
    return render(request, 'archive.html', context)
Exemplo n.º 31
0
def archive(request):
    posts = BlogPost.objects.all()[:10]
    return render(request, BASE_DIR + 'archive.html', {
        'posts': posts,
        'form': BlogPostForm()
    })