새기능 #14
minkyu park이(가) 4달 전에 변경
{{>toc}} # Django Framework + mkdoc 연동 > 참고 : https://www.hacksoft.io/blog/integrating-a-password-protected-mkdocs-in-django?ht-comment-id=6897299 > 예전 버전이라 새로운 버전으로 다시 연동 ## 1. docs 앱 생성 ```bash python manage.py startapp docs ``` ## 2. mkdocs 설치 및 초기화 ```bash pip install mkdocs mkdocs new mkdocs ``` ## 3. 디렉토리 구조 정리 > 디렉토리 구조 정리 MkDocs 관련 파일을 Django 루트로 옮겨서 mkdocs.yml과 가 index.md와 manage.py같은 위치에 있도록 정리 ```bash mv mkdocs/mkdocs.yml . mv mkdocs/docs/index.md mkdocs/ rm -r mkdocs/docs ``` * mkdocs.yml 설정 ```yaml site_name: My Docs theme: name: mkdocs docs_dir: 'mkdocs' site_dir: 'docs/static/mkdocs_build' nav: - Home: index.md ``` ## 4. Django에서 mkdocs 서빙 ### 1. docs/views.py 작성 ```python import os from django.conf import settings from django.contrib.auth.decorators import login_required from django.http import FileResponse, Http404 # @login_required def serve_docs(request, path='index.html'): build_root = os.path.join(settings.BASE_DIR, 'docs', 'static', 'mkdocs_build') full_path = os.path.join(build_root, path) if os.path.isdir(full_path): full_path = os.path.join(full_path, 'index.html') if not os.path.exists(full_path): raise Http404(f"File not found: {path}") print(f"Serving file: {full_path}") return FileResponse(open(full_path, 'rb')) ``` ### 2. docs/urls.py ```python from django.urls import re_path from .views import serve_docs urlpatterns = [ re_path(r'^(?P<path>.*)$', serve_docs), ] ``` ### 3. settings/urls.py 수정 ```python from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('docs/', include('docs.urls')), ] ``` ## 5. mkdocs 빌드 ```bash mkdocs build ```