def filter_by(request, by, id): """ 按标签或分类筛选博客 """ if by == "tag": tag = Tag.objects.get_by_id(id) if tag is None: raise Http404 return render_and_response(request, "index.html", pager(request, "tag", id)) elif by == "cate": cate = Category.objects.get_by_id(id) if cate is None: raise Http404 return render_and_response(request, "index.html", pager(request, "cate", id)) else: raise Http404
def shorten(req): """ Generate a short url from a long url """ t = 'shorten.html' if req.method == 'GET': return render_and_response(req, t) ori_url = req.POST.get('url') name = req.POST.get('name') if not re.search(URL_REGEX, ori_url): return render_and_response(req, t, {'msg': 'invalid url'}) code = get_short_url(ori_url) obj = models.ShortLink.objects.get_or_create(code=code, defaults={'name': name, 'link': ori_url})[0] return render_and_response(req, t, {'result': obj.get_short_url()})
def search(request): """ 搜索博客 """ return render_and_response( request, 'index.html', pager(request, 'search', request.REQUEST.get('k')))
def filter_by(request, by, id): """ 按标签或分类筛选博客 """ if by == 'tag': tag = Tag.objects.get_by_id(id) if tag is None: raise Http404 return render_and_response(request, 'index.html', pager(request, 'tag', id)) elif by == 'cate': cate = Category.objects.get_by_id(id) if cate is None: raise Http404 return render_and_response(request, 'index.html', pager(request, 'cate', id)) else: raise Http404
def shorten(req): """ Generate a short url from a long url """ t = 'shorten.html' if req.method == 'GET': return render_and_response(req, t) ori_url = req.POST.get('url') name = req.POST.get('name') if not re.search(URL_REGEX, ori_url): return render_and_response(req, t, {'msg': 'invalid url'}) code = get_short_url(ori_url) obj = models.ShortLink.objects.get_or_create(code=code, defaults={ 'name': name, 'link': ori_url })[0] return render_and_response(req, t, {'result': obj.get_short_url()})
def detail(request, blog_id): """ 博客详情 """ blog = Blog.objects.get_by_id(blog_id) if blog is None or blog.is_draft: raise Http404 init = None if request.user.is_superuser: init = {"contact": "*****@*****.**", "name": "tom kliyes"} return render_and_response(request, "blog/detail.html", {"blog": blog, "form": CommentForm(initial=init)})
def detail(request, blog_id): """ 博客详情 """ blog = Blog.objects.get_by_id(blog_id) if blog is None or blog.is_draft: raise Http404 init = None if request.user.is_superuser: init = {'contact': '*****@*****.**', 'name': 'tom kliyes'} return render_and_response(request, 'blog/detail.html', { 'blog': blog, 'form': CommentForm(initial=init) })
def comment(request, blog_id): """ 提交评论 """ blog = Blog.objects.get_by_id(blog_id) if blog is None: return Http404 form = CommentForm(request.POST) if request.method == "POST": if form.is_valid(): cmt = form.post(blog) cmt.ip = get_ip(request) cmt.save() return HttpResponseRedirect("/blog/%s#cmt" % blog.id) return render_and_response(request, "blog/detail.html", {"blog": blog, "form": form})
def comment(request, blog_id): """ 提交评论 """ blog = Blog.objects.get_by_id(blog_id) if blog is None: return Http404 form = CommentForm(request.POST) if request.method == "POST": if form.is_valid(): cmt = form.post(blog) cmt.ip = get_ip(request) cmt.save() return HttpResponseRedirect('/blog/%s#cmt' % blog.id) return render_and_response(request, 'blog/detail.html', {'blog': blog, 'form': form})
def search(request): """ 搜索博客 """ return render_and_response(request, "index.html", pager(request, "search", request.REQUEST.get("k")))
def about_me(request): """ 关于我页面 """ return render_and_response(request, "about.html")
def home_page(request): """ 博客首页 """ return render_and_response(request, "index.html", pager(request, "all"))
def about_me(request): """ 关于我页面 """ return render_and_response(request, 'about.html')
def home_page(request): """ 博客首页 """ return render_and_response(request, 'index.html', pager(request, 'all'))
def catalog(req): return render_and_response(req, 'catalog.html')