티스토리 뷰
[python]Django 와 bootstrap 연동 (interact bootstrap with Django)
가지가지하는 부부 - do variety 2021. 3. 9. 16:20
Django 설치 후, Bootstrap 연동에 대해서 posting 하겠습니다.
1. Bootstrap Template Download
startbootstrap.com/theme/freelancer/
2.
copy Bootstrap file to app folder
Project > App 을 구성했으면, App의 하위 directory 에 static / templates folder를 생성함.
3.
bootstrap 의 index.html을 templates 에 copy 함.
나머지 assets/css/js 는 static 하위에 copy 함.
4.
Project > settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app', #add app name ] STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
5.
URL 연동 Project > urls.py
from django.urls import path from django.conf.urls import include, url from twojobs.views import * from django.views.generic import RedirectView from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('app/', include('twojobs.urls')), path("", RedirectView.as_view(url="/app/")), #basic URL만 쳐도 app 으로 이동하도록 redirect 설정함. ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
6.
URL 연동 Project > app > urls.py
from django.contrib import admin from django.urls import path from django.conf.urls import include, url# from twojobs.urls import * from twojobs import views# from twojobs.views import CreatememoView, MemolistView, MemodetailView, UpdatememoView, DeletememoView urlpatterns = [ path('', views.index, name='index'), #path('single', views.single, name='index'), ]
7.
Modify view : Project > app > views.py
from django.shortcuts import render from django.views import generic # Create your views here. def index(request): context = { } return render(request, 'index.html', context=context) def single(request): context = { } return render(request, 'single.html', context=context)
8.
modify hyperlink
index.html > project > app > templates > index.html
{% load static %}
<!--link rel="icon" type="image/x-icon" href="static/assets/img/favicon.ico" /-->
<link rel="icon" type="image/x-icon" href="{% static '/assets/img/favicon.ico' %}" />
<!--link rel="icon" type="image/x-icon" href="static/assets/img/favicon.ico" /--> <link rel="icon" type="image/x-icon" href="{% static '/assets/img/favicon.ico' %}" />
9.
excute django server : python3 manage.py runserver 0:8000
[Kill Port]
sudo lsof -t -i tcp:8000 | xargs kill -9
docs.djangoproject.com/ko/3.1/intro/tutorial06/
10.
부팅 후 자동실행 (Auto execute for booting)
OS 마다 방법이 다르니 확인해서 적용하면 됩니다.
1안)
$ vi /etc/rc.local
sudo service smbd restart
2안)
1). Make Script
- 위치 : /etc/init.d/
ex : /etc/init.d/run.sh
python3 /home/mgchild/dev_flask/sideproject/sideproject/manage.py runserver 0:8000
2) Add Permision
$chmod 777 /etc/init.d/run.sh
3) update service
$update-rc.d run.sh defaults
(access deny 발생하면 -> su 로 진입하여 등록함)
삼바서버 동작 확인 - Checking Samba service.
ps -ef | grep smbd
11.
connect Django and apache server : 상용 webserver 에 연결이 필요함.
isolution.pro/ko/t/django/django-apache-setup/django-apache-seoljeong
1) WSGIScriptAlias 설치
$sudo apt install libapache2-mod-wsgi-py3
$sudo a2enmod wsgi
$sudo systemctl start apache2
2) httpd.conf 혹은 /etc/apache2/apache2.conf 수정
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py WSGIPythonPath /var/www/html/myproject/ <Directory /var/www/html/myproject/> <Files wsgi.py> Order deny,allow Allow from all </Files> </Directory>
12.
connect Bootstrap and webserver for static resources.
1) httpd.conf 혹은 /etc/apache2/apache2.conf 수정
Alias /static/ /var/www/html/project/app/static/ <Directory /var/www/html/project/app/static/> Require all granted </Directory>
'Dev > python' 카테고리의 다른 글
[python/php]youtube rss php parser 및 Atom feed parser 구현 (0) | 2021.03.23 |
---|---|
[python]Django 설치 및 기본 세팅 (Django Install and Setting) (0) | 2021.03.04 |
[Python-파이썬]서버 실시간 체크해서 알람 기능 구현 (Server-Slack Notification, Ping) (0) | 2021.02.16 |
[Python-파이썬]SyntaxError: Non-ASCII character 에러/수정방법 (0) | 2021.02.04 |
[PHP]PHP에서 json 파일로 저장하기 (Save to json file in PHP) (0) | 2021.01.28 |