ジャンゴベースの個人的なブログサイトの構築(5)

ジャンゴベースの個人的なブログサイトの構築(5)

序文

ピジョン二日前、私たちは今日のポイントを書き続け

主なコンテンツ

今日はその彼の人生の記録と呼ばれる、フォトショーの機能を追加しました

まず、構築されたテーブル


class Record(models.Model):
    title = models.CharField(max_length=128)
    content = models.TextField()
    picture = models.CharField(max_length=128)
    creationTime = models.DateTimeField(auto_now_add=True)

主な機能は、画像をアップロードし、タイトルを追加し、何か面白いものを記録するコンテンツです

だから、バックグラウンドでアップロードする写真を追加します

他の誰かがアップロード画像は画像コードを表示]をクリック借ります


<input type="file" id="chooseImage" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" name="picture" class="form-control" aria-invalid="false">
<!-- 保存用户自定义的背景图片 -->
<img id="cropedBigImg" value='custom' alt="请添加图片" data-address='' title="我的图片"/>

<script>

$('#chooseImage').on('change',function(){
       var filePath = $(this).val(),         //获取到input的value,里面是文件的路径
          fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase()
          src = window.URL.createObjectURL(this.files[0]); //转成可以在本地预览的格式

       // 检查是否是图片
       if( !fileFormat.match(/.png|.jpg|.jpeg/) ) {
          alert('请选择图片');
           return;
        }

        $('#cropedBigImg').attr('src',src);
});

</script>

効果:

治療は、ビューの関数です。


@auth
def publish_record(request):
    if request.method == 'GET':
        return render(request, 'backend/publish_record.html')
    if request.method == 'POST':
        if request.FILES:
            myFile = None
            for i in request.FILES:
                myFile = request.FILES[i]
            if myFile:
                dir = os.path.join(
                    os.path.join(
                    os.path.join(
                    os.path.join(
                        BASE_DIR,
                        'statics'),'assets'),'images'),
                    'record')
                destination = open(os.path.join(dir, myFile.name),
                                   'wb+')
                for chunk in myFile.chunks():
                    destination.write(chunk)
                destination.close()

            title = request.POST.get('title')
            content = request.POST.get('content')
            models.Record.objects.create(title=title,content=content,picture=myFile.name)
        else:
            messages.error(request,'输入信息有误')
        return redirect('/backend/publish_record')

メイン画面は、固定されたフォルダに入れ、その後、ページが表示されているプレフィックスパスは固定されています

フロントエンドで表示されます

次に、デフォルトのアクセスエラーページを設定します。

url.pyに追加


handler403 = views.permission_denied
handler404 = views.page_not_found
handler500 = views.page_error

そして、対応するビューハンドラを作成


def permission_denied(request):
    return render(request,'show/403.html')

def page_not_found(request):
    return render(request, 'show/404.html')

def page_error(request):
    return render(request, 'show/500.html')

そして、存在しないURLを訪問すること自由に感じ:

最後に、マークダウンエディタでも、簡単に紹介され、画面上のコンテンツを追加

まず、背景には、ページの内容の入力を追加します

背景た.mdファイルにコンテンツを保存


def about_edit(request):
    if request.method == 'GET':
        dir = os.path.join(
            os.path.join(
                os.path.join(
                    os.path.join(
                        BASE_DIR,
                        'statics'), 'assets'), 'about'),
            'about.md')
        with open(dir,'r+') as f:
            content = f.read()

        return render(request,'backend/about_edit.html',{'content':content})
    if request.method == 'POST':
        dir = os.path.join(
            os.path.join(
                os.path.join(
                    os.path.join(
                        BASE_DIR,
                        'statics'), 'assets'), 'about'),
            'about.md')
        content = request.POST.get('content')
        with open(dir,'w+') as f:
            f.write(content)

        return redirect('/backend/about_edit')

これは、目の前のページに内容が表示されます


def about(request):
    if request.method == 'GET':
        all_type = models.ArticleType.objects.all()
        dir = os.path.join(
            os.path.join(
                os.path.join(
                    os.path.join(
                        BASE_DIR,
                        'statics'), 'assets'), 'about'),
            'about.md')
        with open(dir, 'r+') as f:
            content = f.read()
        content = markdown(content)
        return render(request, 'show/about.html', {'all_type': all_type,'content':content })

概要

基本的な内容は今日の履歴監査のためのサイトを提出しただけ置くテンセントクラウドを完了した、このプロジェクトは、サーバー上の効果を見るために明日展開することが期待されています

おすすめ

転載: www.cnblogs.com/sfencs-hcy/p/10958209.html