def detail(request): # print('数据库', request.get_full_path()) user = get_user(request) article_id = request.GET.get('article_id') article = Article.objects.filter(id=article_id).first() article.read_count += 1 article.save() comments = Comment.objects.filter(article_id=article_id) current_path = request.get_full_path() if user and len( Collect.objects.filter( Q(user_id=user.id) & Q(article_id=article_id))): collect_flag = True else: collect_flag = False context = { 'article': article, 'comments': comments, 'current_path': current_path, 'current_user': get_user(request), 'collect_flag': collect_flag } return render(request, 'detail.html', context)
def post(request): if request.method == 'POST': user = get_user(request) article = Article() article.title = request.POST.get('title') article.author = user.username article.content = request.POST.get('content') article.save() add_message(request, messages.INFO, '帖子发表成功') return HttpResponseRedirect('/') context = {'current_user': get_user(request)} return render(request, 'post.html', context)
def my_collect(request): user = get_user(request) collects = Collect.objects.filter(user_id=user.id).all() article_idlist = [collect.article_id for collect in collects] articles = Article.objects.filter(id__in=article_idlist).all() context = {'current_user': user, 'articles': articles} return render(request, 'my_collect.html', context)
def home(request): articles = Article.objects.all() top10 = get_top10() context = { 'articles': articles, 'current_user': get_user(request), 'top10': top10 } return render(request, 'home.html', context)
def del_collect(request): user = get_user(request) article_id = request.GET.get('article_id') collect = Collect.objects.filter( Q(user_id=user.id) & Q(article_id=article_id)).first() collect.delete() add_message(request, messages.INFO, '删除成功') return HttpResponseRedirect('/my_collect/')
def add_collect(request): user = get_user(request) article_id = request.GET.get('article_id') collect = Collect() collect.user_id = user.id collect.article_id = article_id collect.save() add_message(request, messages.INFO, '收藏成功') return HttpResponseRedirect('/my_collect/')
def comment(request): user = get_user(request) article_id = request.POST.get('article_id') comment = Comment() comment.article_id = article_id comment.author = user.username comment.content = request.POST.get('content') comment.save() article = Article.objects.filter(id=article_id).first() article.comment_count += 1 article.save() message = '评论成功' add_message(request, messages.INFO, message) current_path = request.POST.get('current_path') return HttpResponseRedirect(current_path)
def upload_icon(request): user = get_user(request) if request.method == 'POST': f = request.FILES['icon'] #request.FILES为类字典结构 file_ext = os.path.splitext(f.name)[-1] if file_ext not in ['.png', '.jpg']: add_message(request, messages.INFO, '只能上传png/jpg格式图片,请重新上传!') return HttpResponseRedirect('/upload_icon/') icon_name = user.username + file_ext icon_path = os.path.join(MEDIA_ROOT, icon_name) #接收端的文件路径 with open(icon_path, 'wb') as icon: for part in f.chunks(): #分包发送,分包接收,分包写入,收到一段写入一段 icon.write(part) icon.flush() #刷新缓冲区,防止数据拥堵 try: img = Image.open(icon_path) #打开上传文件 except OSError: os.remove(os.path.join(MEDIA_ROOT, icon_path)) #无效删除 add_message(request, messages.INFO, '上传文件并非图片,请重新上传!') return HttpResponseRedirect('/upload_icon/') img.thumbnail((64, 64)) #把原来的文件压缩成64*64的缩略图 img.save(icon_path) #保存缩略图文件,替代用户上传的图片 if user.icon != 'default.png' and user.icon != icon_name: #删除用户原来的头像文件,使用默认头像的不能删除 os.remove(os.path.join(MEDIA_ROOT, user.icon)) user.icon = icon_name user.save() add_message(request, messages.INFO, '头像修改成功') return HttpResponseRedirect('/user_info/') return render(request, 'upload_icon.html', {'current_user': user})
def process_request(self, request): ip = request.META['REMOTE_ADDR'] user = get_user(request) pass
def my_articles(request): user = get_user(request) articles = Article.objects.filter(author=user.username) context = {'current_user': user, 'articles': articles} return render(request, 'my_articles.html', context)
def user_info(request): user = get_user(request) context = {'current_user': user} return render(request, 'user_info.html', context)