def search(request): try: keyword = request.GET.get('keyword', None) catagory = request.GET.get('catagory', None) tag = request.GET.get('tag', None) if keyword: search_article = Article.objects.filter(title__icontains=keyword).order_by('-publish_time') elif catagory: catagory = Catagory.objects.get(name=catagory) search_article = Article.objects.filter(catagory=catagory).order_by('-publish_time') elif tag: tag = Tag.objects.get(name=tag) search_article = Article.objects.filter(tags=tag) else: return HttpResponseRedirect('/blog/') page = int(request.GET.get('page', 1)) articles = Paginator(search_article, max_article_per_page) try: articles = articles.page(page) except PageNotAnInteger: articles = articles.page(1) except EmptyPage: articles = articles.page(articles.num_pages) result = { 'keyword': keyword, 'catagory': catagory, 'tag': tag, 'articles': articles, 'cur_page': page, } return cus_render(request, 'Search_result.html', result) except Exception as e: return HttpResponseRedirect('/blog/')
def get(self, request): if request.user.is_authenticated(): user = request.user else: user = UserProfile.objects.get(id=2) result = { 'user': user, } return cus_render(request, self.template, result)
def get(self, request): if not request.user.is_authenticated(): return HttpResponseRedirect('/blog/') catagorys = Catagory.objects.all() tags = Tag.objects.all() form = ArticleForm() result = { 'catagorys': catagorys, 'tags': tags, 'form': form, } return cus_render(request, self.template, result)
def index(request): articles = Article.objects.all().order_by('-publish_time') articles = Paginator(articles, max_article_per_page) page = int(request.GET.get('page', 1)) try: articles = articles.page(page) except PageNotAnInteger: articles = articles.page(1) except EmptyPage: articles = articles.page(articles.num_pages) result = { 'cur_page': page, 'articles': articles, } return cus_render(request, 'Index.html', result)
def get(self, request): try: article_id = int(request.GET.get(u'id', 1)) article = Article.objects.filter(id=article_id) if article[0]: next_post = Article.objects.filter(id=article_id+1) if next_post: next_post = next_post[0] previous_post = Article.objects.filter(id=article_id-1) if previous_post: previous_post = previous_post[0] data = { 'article': article[0], 'next_post': next_post, 'previous_post': previous_post, } return cus_render(request, self.template, data) else: return render(request, '404.html') except Exception as e: return HttpResponseRedirect('/blog/')
def about(request): return cus_render(request, 'About.html')