示例#1
0
def new_post(request):
    categories = Category.objects.all()
    profile = ProfileModel.objects.get(id_user=request.user)
    if request.method == "POST":
        text = request.POST.get('text')
        published_date = timezone.now()
        id_category = request.POST.get('id_category')
        id_user = request.user.id
        image_url = ''
        if request.FILES:
            if request.FILES.get('image_url'):
                url = 'static/image/' + request.FILES.get('image_url').name
                handle_uploaded_file(request.FILES.get('image_url'), url)
                image_url = 'image/' + request.FILES.get('image_url').name
        newPost = Post()
        newPost.text = text
        newPost.published_date = published_date
        newPost.id_category = Category.objects.get(id=id_category)
        newPost.id_user = User.objects.get(id=id_user)
        newPost.image_url = image_url
        newPost.save()
        profile.count_post += 1
        profile.save()
        return redirect("/profile")
    return render(request, 'myUser/profile.html', locals())
示例#2
0
文件: views.py 项目: gdludan/blog
def NewVisew(request):
    '''
    新建文章
    :param request: 客户端请求头
    :return: html页面或者跳转到主页
    '''
    form = PostForm()
    title__post = '发布文章'  # 按钮名称
    if request.method == 'POST':
        user = User.objects.get(id=request.user.id)
        if re.match(r'<script>.*?</script>', request.POST.get('content', '')):
            messages.error(request, '发布文章失败,检测提交js脚本')
            return redirect('/user/home')
        post = Post(title=request.POST.get('title', ''),
                    content=request.POST.get('content', ''),
                    user=user,
                    time=datetime.datetime.now())
        post.save()
        t = threading.Thread(target=sendNotify, args=(request, user, post))
        t.start()
        messages.error(request, '发布文章成功,检测提交js脚本')
        return redirect('/user/home')
    return render(request, 'post_file.html', locals(), RequestContext(request))