python.django - k821209/pipelines GitHub Wiki
django react, ssl
server{
# listen 8888;
listen 8889 ssl;
# proxy_ssl_server_name on;
#ssl_password_file /etc/nginx/ssl/pw.txt;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/cert.key;
ssl_session_timeout 5m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
server_name tools.gnu.ac.kr;
client_max_body_size 5000M;
root /data;
location /api {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://0.0.0.0:5000;
break;
}
}
location /static_django/ {
alias /data/static/;
#proxy_pass http://0.0.0.0:5000; # debug = False ํ๊ณ ๋์๋ proxy_pass๋ฅผ ์์ ์ฃผ์. ์ฅ๊ณ ๋ฅผ ํตํด์ static์ ์๋น ์ํ๊ธฐ ๋๋ฌธ์ด๋ค
}
location /media/ {
alias /data/media/;
#proxy_pass http://0.0.0.0:5000; #
}
location / {
root /data/gnurdb/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
# SSL ์ค์ ํ๊ณ server nginx restart ์์ ๋ค์์ด ๋ฌ๋ค. ๋น๋ฒ์ ์
์ฒด์ ๋ฌธ์ !!!
Enter PEM pass phrase:
django, react, ํ๋์ ํฌํธ
server{
listen 8888;
server_name thisApp;
client_max_body_size 5000M;
root /data;
location /api {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://0.0.0.0:5000;
break;
}
}
location /static_django {
root /data/static;
proxy_pass http://0.0.0.0:5000; #
}
location /media {
root /data/media;
proxy_pass http://0.0.0.0:5000; #
}
location / {
root /data/reactapp/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
# /data/omics-react/build
#server {
# listen 8888;
# location / {
# root /data/gnurdb/build;
# index index.html index.htm;
# try_files $uri $uri/ /index.html;
# }
#}
# setting.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880000 # 5 MB
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# STATIC_ROOT = '/static/'
STATIC_URL = '/static_django/'
print(BASE_DIR)
# main:urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from app.views import Researcher_view
from rest_framework import routers
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
router = routers.DefaultRouter()
router.register('Researcher', Researcher_view)
urlpatterns = [
path('api/admin/', admin.site.urls),
# url(r'^', include(router.urls)),
path('api/', include(router.urls)),
path('markdownx/', include('markdownx.urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
print(urlpatterns)
login ๊ตฌํ
# https://developer0809.tistory.com/98
conda install -yc conda-forge django-allauth
conda install -yc conda-forge django-rest-auth
conda install -yc conda-forge django-cors-headers
conda install -yc conda-forge django-filter
conda install -yc conda-forge djangorestframework
error
# ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'recommend.authuser', but app 'recommend' doesn't provide model 'authuser'
# https://stackoverflow.com/questions/50324561/valueerror-the-field-admin-logentry-user-was-declared-with-a-lazy-reference/65112242
# ์ฐ๊ด๋ admin์ด๋ ํผ์ ๋ชจ๋ ์ฝ๋ฉํธ ์์ ํ๊ณ
# comment AUTH_USER_MODEL in settings.py so it points to default User model
python manage.py migrate auth zero
# uncomment to be AUTH_USER_MODEL='recommend.AuthUser'
python manage.py migrate auth
python manage.py migrate appname
python manage.py migrate
patch
์๋
ํ์ธ์. ์ฌ๊ธฐ์ ๊ธฐ ํค๋งค๋ค๊ฐ ์ง๋ฌธ์ ์ฌ๋ฆฝ๋๋ค.
DRF๋ก API๋ง๋ค๊ณ React๋ก API๋ฅผ ๋ถ๋ฌ์ ํ๋ฉด์ ๋์ฐ๊ณ ์
๋ ฅ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ DRF์ axios๋ก ์
๋ฐ์ดํธ๋ฅผ ํ๋ ๊ตฌ์ฑ์ ๋ง๋ค๊ณ ์์ต๋๋ค. ModelViewSet์ ์จ์ view๋ฅผ ๊ตฌ์ฑํ๋๋ ๋ณ ๋ฌด๋ฆฌ์์ด axios.patch๋ก ์
๋ ฅ๋ฐ์ ๊ฐ์ ๋๋น์ ์
๋ฐ์ดํธํ๋๊ฒ ๊ฐ๋ฅํ์ต๋๋ค.
์กฐ๊ธ ๋ค๋ฅธ ๋์ฆ๊ฐ ์์ด์ ์น์์ axios๋ก ์
๋ฐ์ดํธ ํ๋๊ฒ ์๋๋ผ, ํ์ด์ฌ์์ requests ๋ชจ๋์ ์จ์ ๋๊ฐ์ด patch๋ก ๋๋น๊ฐ์ ์
๋ฐ์ดํธ ํ๋ ค๋ Method Not Allowed ๊ฐ ๋น๋๋ค.
ํ์ด์ฌ์์ ์ฐ๋ requests๋ชจ๋๊ณผ ๋ฆฌ์ํธ์ axios๊ฐ ํ๋์ผ์ด ๋ค๋ฅธ๊ฑด์ง์. ํ์ด์ฌ์์ patch๋ฅผ ํตํด ์
๋ฐ์ดํธ๋ฅผ ํ๋ ค๋ฉด ์ด๋ค ๋ฐฉ๋ฒ์ ์จ์ผํ ๊น์
๋ฏธ๋ฆฌ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
์์๋์ต๋๋ค. ;; axios์์๋ url์ ~~~~/?auto_id=1์ด๋ ๊ฒ ์ฃผ๊ณ param์ ์ค๋ ํฐ ๋ฌธ์ ๊ฐ ์์๋๋ฐ python requests์์๋ url ์ ~~~~/1/ ์ด๋ฐ๋ชจ์์ผ๋ก ์ฃผ๊ณ param์ ์ค์ผํ๋๊ฒ์ด๊ตฐ์! https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/?fbclid=IwAR2DthW_8vfuzb3WZXcePoQcuqtkw5eDaY8YCxykZONrjg5AfKxWtjOeDDk
django debug=false ์ค์ ์ ์ํ static ์ธํ ํ์ธ
django api setting
https://www.django-rest-framework.org/#installation
# cors
conda install -yc conda-forge django-cors-headers
INSTALLED_APPS = (
...
'corsheaders',
...
)
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
django 3 suit install
> pip install https://github.com/darklow/django-suit/tarball/v2
๋์ฉ๋ํ์ผํ์ฉ
# nginx.config
...
client_max_body_size 5000M;
...
# django setting.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880000 # 5 MB
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880000 # 5 MB
$ service nginx reload
์ฅ๊ณ ,gunicorn,nginx
project๋ช : RNAseqDB
# urls.py
# static ์ gunicorn์ด ๋ถ๋ฌ์ค๋ ค๋ฉด ์ด๋ฐ ์ค์ ์ด ํ์
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
...
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
# settings.py
# wsgi ํ ๋ ค๋ฉด static ์ค์ ํด์ผํจ
echo "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> RNAseqDB/settings.py
python manage.py collectstatic
drwxr-xr-x 5 root root 160 Jul 1 03:31 ./
drwxr-xr-x 6 root root 149 Jul 1 03:22 ../
drwxr-xr-x 2 root root 6 Jul 1 03:31 .empty/
drwxr-xr-x 4 root root 203 Jul 1 03:19 RNAseqAPI/
drwxr-xr-x 3 root root 117 Jul 1 03:19 RNAseqDB/
-rw-r--r-- 1 root root 1045 Jul 1 03:19 csv2sql.py
-rw-r--r-- 1 root root 3760128 Jul 1 03:19 db.sqlite3
-rw-r--r-- 1 root root 540 Jul 1 03:19 manage.py
-rw-r--r-- 1 root root 4986701 Jul 1 03:19 sarco_count.pk
/data/RNAseqDB/RNAseqDB# gunicorn --bind 0.0.0.0:8000 RNAseqDB.wsgi
- nginx ๋ flask ๋ฐฉ์์ ๋ฐ๋ฅด๋ฉด ๋๋ค.
server{
listen 8888;
server_name thisApp;
client_max_body_size 5000M;
root /data/forestDB;
location / {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://0.0.0.0:5000;
break;
}
}
location /static{
alias /data/forestDB/static;
proxy_pass http://0.0.0.0:5000; #
}
location /media{
alias /data/forestDB/media;
proxy_pass http://0.0.0.0:5000; #
}
}
์ฅ๊ณ ์ํ์น์ฐ๋
- ์ฅ๊ณ view ์์ ๋ถ๋ฌ์ค๋ ๊ธฐ๋ณธ ๋ฐ์ดํฐ๋ค์ (csv๊ฐ์๊ฒ) ์ ์ ๋๊ฒฝ๋ก๋ก ๋ถ๋ฌ์์ผํจ.
- upload ํ๋ static folder๊ฐ์๊ฑด chmod 777 ํด์ฃผ์ผ
- ํนํ apache ๊ฐ ์ ๊ทผํ staticํด๋์ ๊ผญ chmod 777
- DB ํ์ผ๋ค์ด ์๋ basedir ๋ ๊ผญ chmod 777, ํ์ผ๋ค๋ ํด์ผํ๋์ง๋ ์ ๋ชจ๋ฅด์ง๋ง ์๋ฌ๋๋ฉด ํด๋ณด๊ธธ.
- apache2 ์ค์น
- ์ํ์น ์คํํ๋ฉด์ ๋๋ ์๋ฌ๋ค์ cat /var/log/apache2/error.log
- apt-get install libapache2-mod-wsgi-py3
- ์ ์๋๋์ง ํ์ธ. ์ผ๋จ 80 ํฌํธ ๊ธฐ๋ณธ์ผ๋ก ์ธ ๊ฑฐ๊ธฐ ๋๋ฌธ์ 8888์ผ๋ก ๋ฐ๊พธ๋ ์ง์ ํ์ง๋ง์
- ๊ธฐ๋ณธ ์ ์ ํฌํธ ๋ฐ๊พธ๋ ๊ฒ์ /etc/apache2/ports.conf์ ์๋ค. Listen ์ด์ฉ๊ณ ..
- /etc/apache2/sites-available/ ์ ์ํ์น ํธ์คํ ์ ๊ธฐ๋ณธ์ค์ ์ด ์๋ค.
- pglhome.conf ๊ฐ์๊ฑธ ๋ง๋ค์ด๋๊ณ
> a2dissite 000-default # ๊ธฐ๋ณธ ์ ์ ํ๋ฉด์ ๊ธ์ง ์ํด.
> a2ensite pglhome # ์ฐ๋ฆฌ๊ฐ ์ํ๋ ์ฅ๊ณ ๋ฅผ ๋งตํ ์ํฌ ์ฝํผ๊ทธ
- ํ๋ฉด pglhome.conf์ ์ค์ ์ผ๋ก ์ํ์น๊ฐ ์์๋๊ฒ ๋๋ค.
- ๊ทธ๋ฌ๋ฏ๋ก pglhome.conf๋ฅผ ์ค์ ํด์ผํ๋ค.
# pglhome.conf
<VirtualHost *:80>
WSGIDaemonProcess pgl.gnu.ac.kr threads=10 python-home=/home/k821209/PGL_page/:/usr/local/lib/python3.5/dist-packages/
WSGIProcessGroup pgl.gnu.ac.kr
WSGIScriptAlias / /home/k821209/PGL_page/pgl/wsgi.py process-group=pgl.gnu.ac.kr
<Directory /home/k821209/PGL_page/pgl/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /files/ /home/k821209/PGL_page/files/
Alias /static/ /home/k821209/PGL_page/static/
<Directory /home/k821209/PGL_page/files>
Require all granted # ์ต์ ๋ฒ์ ์ ๊ผญ ์ด๋ ๊ฒ ํด์ค์ผํจ allow ์ด์ฉ๊ณ ํ๋๊ฑด ์๋ฒ
</Directory>
<Directory /home/k821209/PGL_page/static> # ์์ผ๋ก ์ฌ๊ธฐ๋ค ๊ฒฐ๊ณผํ์ผ์ ์ง์ ํ๊ณ ๋ถ๋ฌ์จ๋ค.
Require all granted
</Directory>
</VirtualHost>
- static์ BASE_DIR์ ์ ๋ถ์ฌ๋๊ณ ์ํ์น๋ก ์ง์ ํด์คฌ๋ค๋ฉด, python3 manage.py collectstatic ์คํ. ๊ทธ๋ผ admin๋ static์ผ๋ก ๋ค์ด๊ฐ
- ์ฐธ๊ณ : https://django.readthedocs.io/en/1.3.X/howto/deployment/modwsgi.html#serving-the-admin-files
**Link to self **
<a class="item" href="{% url 'network:index' %}" target="_blank"> # network ๋ผ๋ ์ด๋ฆ์ url์ ์ ์ ๋์ด ์์
<button class="ui fluid button">
LINK
</button>
</a>
Multiple DB
# setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'soyped': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'soyped.sqlite3'),
},
'lemna_tandemDB': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'lemna_tandemDB.sqlite3'),
},
}
DATABASE_ROUTERS = ['lemna_tandem.router.lemna_Router','SoyPed.router.SP_Router']
# router.py # in app directory
class lemna_Router(object):
def db_for_read(self, model, **hints):
"Point all operations on SoyPed models to 'soyped'"
if model._meta.app_label == 'lemna_tandem':
return 'lemna_tandemDB'
return None # ์ด๊ฒ ๊ผญ ๋์ด ๋์ด์ค์ผํ๋ค.. ๊ทธ๋์ผ ์๋๊ฒฝ์ฐ ๋ค์ ํญ๋ชฉ์ผ๋ก ๋์ด๊ฐ๋ค... ์ด๋ฐ์ ์ฅํ ์ด๊ฒ๋๋ฌธ์ ๋ด๊ฐ
def db_for_write(self, model, **hints):
"Point all operations on SoyPed models to 'soyped'"
if model._meta.app_label == 'lemna_tandem':
return 'lemna_tandemDB'
return None # None ์ผ๋ก ์ํด์ฃผ๊ณ default๋ก ๋๋ฆฌ๋ฉด ๋ค์ ๋ผ์ฐํฐ๊ฐ ์ผ์ ๋ชปํ๋ค. ๋ฌด์กฐ๊ฑด None์ ํ๊ณ setting.py ์์์ ๋ง์ง๋ง router์๋ default return์ ํด์ค๋ ๋๋ค
def allow_relation(self, obj1, obj2, **hints):
"Allow any relation if a both models in chinook app"
if obj1._meta.app_label == 'lemna_tandem' and obj2._meta.app_label == 'lemna_tandem':
return True
# Allow if neither is chinook app
elif 'lemna_tandem' not in [obj1._meta.app_label, obj2._meta.app_label]:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'lemna_tandem':
return db == 'lemna_tandemDB'
return None
def allow_syncdb(self, db, model):
if db == 'lemna_tandemDB' or model._meta.app_label == "lemna_tandem":
return False # we're not using syncdb on our legacy database
else: # but all other models/databases are fine
return True
https://beomi.github.io/python/2017/02/08/Setup-SocialAuth-for-Django.html
Build DB
django-admin startproject mysite .
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
manage.py flush --database DB_NAME # ๋๋น ๋ ๋ฆฌ๊ธฐ
# DB์์ฑ์์.
python manage.py startapp blog # ์ดํ mysite/setting.py INSTALLED_APP ์ blog ์ถ๊ฐ
urls
mysite/urls.py ์ ๋ค์์ ์ถ๊ฐ
url(r'^blog/', include('blog.urls'))
path('kkd2/',include('kkd2.urls'))
blog/urls.py ์ ๋ค์์ ์ถ๊ฐ
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
view
blog/view.py ์ ๋ค์์ ์ถ๊ฐ.
def post_list(request):
return render(request, 'blog/post_list.html', {})
# url์์ view.post_list๋ฅผ ๋ถ๋ฅด๋ฉด blog/post_list.html์ ๋ถ๋ฅธ๋ค. ๋ถ๋ฅผ๋ {}์์ ๋ณ์๋ฅผ ๋ณด๋ด์ค๋ค. ํด๋น ๋ณ์๋ model.py ์์ ๊ฐ์ ธ์ค๊ฒ๋จ.
from django.shortcuts import render
from .models import DBmodel
def index(request):
DBex = DBmodel.objects.order_by('?')[0:2] # ๋ ๋ค์ผ๋ก ํด๋ ์ค๋ฅผ ๊ฐ์ ธ์จ๋ค.
return render(request, 'CreDB/index.html', {'DBex' : DBex}) # ๋ ๋ค์ผ๋ก ๊ฐ์ ธ์จ ํด๋ ์ค๋ฅผ html๋ก ๋ณด๋ธ๋ค.
model class field๊ฐ ๊ฐ์ ธ์ค๊ธฐ.
...
DBex = DBmodel.objects.order_by('?')[0] # ํ๋๋ง ์ ํํด๋จ์.
final_comp_ex = final_comp.objects.get(transcriptname=DBex.genename) # DBex ๋ผ๋ ๋ชจ๋ธ ํด๋ ์ค์ genename ํ๋์ ๊ฐ์ ๊ฐ์ ธ์๋ผ.
...
form class field๊ฐ ๊ฐ์ ธ์ค๊ธฐ
class NameForm_plain(forms.Form): # view ์์์ ๊ทธ๋ฅ ์ ์ธ์ ํจ.
transcript_name = forms.CharField(label='transcript name', max_length=50)
def index(request):
DBex = DBmodel.objects.order_by('?')[0]
final_comp_ex = final_comp.objects.get(transcriptname=DBex.genename)
if request.method=='POST':
form = NameForm_plain(request.POST)
if form.is_valid():
query = form.cleaned_data['transcript_name'] # ์ฟผ๋ฆฌ๋ผ๋ ๋ณ์์ ํผ์์ ๋ฐ์ ๊ฐ์ ๋ฃ์ด์ค๋ค. ํด๋ ์ค์ ๋ณ์๋ช
์ด ๋์
๋๋ฆฌ์ ํค๊ฐ์ผ๋ก ๋ณ์ ํ๊ฒ์ ์ฃผ๋ชฉ. ๋ชจ๋ธ ํด๋ ์ค์ ๋ค๋ฅด๋ค.
...
๊ฒ์๋ฒํผ ๋ฃ๊ธฐ
from django.shortcuts import render
from django import forms
from .models import DBmodel
class NameForm(forms.ModelForm):
class Meta:
model = DBmodel
fields = ('genename',)
def index(request):
DBex = DBmodel.objects.order_by('?')[0]
if request.method=='POST':
form = NameForm(request.POST)
if form.is_valid():
query = form.cleaned_data['genename']
DBex = DBmodel.objects.get(genename=query)
form = NameForm()
return render(request, 'CreDB/index.html', {'DBex' : DBex, 'form': form})
#return redirect('',query=genename)
else:
form = NameForm()
return render(request, 'CreDB/index.html', {'DBex' : DBex, 'form': form})
#๊ฒ์๋ฒํผ์ ๋ฃ์์๋ฆฌ์
<form method=POST>
{% csrf_token %}
{{form}}
<input type="submit" value="Submit"/>
</form>
model
blog/model.py ์ ๋ค์์ ์ถ๊ฐ
# model.py
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True
def publish(self):
self.published_date = timezone.now()
self.save()
def __unicode__(self): # __str__ ๋์ ์ ์ด๋ ๊ฒ ํด์ค๋ค.
return self.title
๋ชจ๋ธ์ ์์ฑํ๊ณ ๋ ๊ผญ
# ์ฌํ์ ๋ชจ๋ธ์ field๋ฅผ ์ถ๊ฐํ๋๋ผ๋, ๋ค์์ ์คํํด์ฃผ๋ฉด๋๋ค. ํ๋๋ฅผ ์ถ๊ฐํ์ ๋ ๋ํดํธ ๊ฐ์ ์ฃผ๋ผ๊ณ ๊ฒฝ๊ณ ๋ฅผ ์ค๊ฒ์ธ๋ฐ, ์๊ฒ์ด๋์ฃผ๋ฉด๋๋ค.
python manage.py makemigrations blog
python manage.py migrate blog
# app/admin.py
from django.contrib import admin
from .models import DBmodel
admin.site.register(DBmodel)
์ํ๋ ํ ์ด๋ธ์ ๋ชจ๋ธ์ ์ค์ ๋ฃ์๋ ๊ฐ์ธ์ ์ผ๋ก๋ ์คํฌ๋ฆฝํธ๋ฅผ ์ง๋๊ฒ ํธํ๋ฏ.
# django script running
./manage.py shell < myscript.py
# ํน์
./manage.py shell
>>> execfile('myscript.py')
myscript.py
from CreDB.models import DBmodel
file_in = 'all.merged.bam.transcripts.all.txt'
for line in open(file_in):
cell = line.strip().split('\t')
DBmodel.objects.create(genename=cell[0], cov_1x=cell[1], cov_10x=cell[2], cov_30x=cell[3],depth_ratio=cell[4],depth=cell[5],match=cell[6],match_ratio=cell[7])
posts = DBmodel.objects.all()
for post in posts:
post.publish()
์ฌํ์ ํ๋๋ฅผ ์ถ๊ฐํ์๋
# ๋ค์์ ์์์ ๋ฃจํ๋ฅผ ๋๋ฆฌ๋ ํจ.
DBmodel.object.filter(genename="name").update(newfield="ATGC")
html
css #~/static/css/CreDB.css ๋ผ๋ ํ์ผ์ ๋ง๋ ๋ค.
#table ํ์ ํ
์ด๋ธ์ ๋ฃ์๋๋ ์ด๊ฑธ๋ก ๊ณ ์ ํ๋๊ฒ ์ข์๋ฏ.
table {
width: 60%;
}
td, th {
border: 2px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
#body ํฐํธ ๋ฐ ๊ธฐํ๋ฑ๋ฑ
body {
font-family: "Times New Roman", Times, serif;
padding-left: 15px;
}
# ๊ธฐํ๋ฑ๋ฑ ๋ง์๊ฒ๋ค์ w3school๊ฐ๋ฉด ๋ง์.
# css๋ฅผ ์ ์ฉํ๋ ค๋ฉด ๋ค์ ํญ๋ชฉ์ด ๋ค์ด๊ฐ์ผํ๋ค.
{% load staticfiles %}
<html>
<head>
<link rel="stylesheet" href="{% static 'css/CreDB.css' %}">
</head>
#๊ฐ๋ก์ค ๋ฃ์๋
<hr style="border: dotted 2px gray;">
#ํ
์ด๋ธ๋ง๋ค๋
<table>
<tr>
<th>Cov.1x</th>
<th>Cov.10x</th>
<th>Cov.30x</th>
<th>Depth.ratio</th>
<th>Cov.1x.match</th> # table header
</tr>
<tr>
<td>{{DBex.cov_1x}}</td>
<td>{{DBex.cov_10x}}</td>
<td>{{DBex.cov_30x}}</td>
<td>{{DBex.depth_ratio}}</td>
<td>{{DBex.match_ratio}}</td> # table values
</table>
# ๊ธ์์ (์ํ์ค ์์)
<textarea cols="100" rows="5">
{{DBex.CDS}}
</textarea>
# ์์ ์ฌํญ ๋ฐ์
python manage.py makemigrations blog
# ์์ ์ฌํญ ์ ์ฉ
python manage.py migrate blog
# ๊ด๋ฆฌ์ ์์ด๋ ํ๊ธฐ.
python manage.py createsuperuser
ํ๊ธ ์ ๋ ฅ๋ฌธ์ (python2)
# model.py
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __unicode__(self): # __str__ ๋์ ์ ์ด๋ ๊ฒ ํด์ค๋ค.
return self.title
basic commands
# run server
~/django/mysite$ python manage.py runserver 0.0.0.0:8000
# build webapp named "polls"
~/django/mysite$ python manage.py startapp polls
# configure webapp
~/django/mysite/polls$ vi models.py
# migration of webapp
~/django/mysite$ python manage.py makemigrations polls
# migration
~/django/mysite$ python manage.py sqlmigrate polls 0001
~/django/mysite$ python manage.py migrate
# edit the "poll/model.py"
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
- model.Model์ ํด๋ ์ค ๋ณ์๋ค์ ๊ฐ์ง๊ณ ์์, ๊ฐ ๋ณ์๋ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ Field์ ํด๋นํจ.
- model.CharField : ๋ฌธ์๋ก ๊ตฌ์ฑ๋ ํ๋
- model.DateTimeField : ๋ ์ง ์๊ฐ ํ๋
- ๋ณ์๋ช ์ด ์ฃผ๋ก ๊ฐ ํ๋๋ช ์ด ๋จ. ํ๋๋ช ์ ์ง์ ํด์ฃผ๋ ค๋ฉด models.DateTimeField('date published') ๊ณผ ๊ฐ์ด ์ฒซ๋ฒ์งธ ์ ๋ ฅ๊ฐ์ผ๋ก ์ง์ ํด์ค
- models.CharField(max_length=200) ์ ๊ฒฝ์ฐ๋ max_length๊ฐ์ ์ง์ ํด์ค์ผํจ (ํ์).
- models.DecimalField(max_digits=4, decimal_places=3) : ์ซ์ ํ๋. ํด๋น ์ผ์ด์ค๋ 1.000 ๊ฐ์ ์๊ฐ ํด๋น
- models.TextField() : ๊ธ๋ฅ. ์ํ์ค ๋ฐ์ผ๋ฉด ๋ฑ.
# app migration (make)
~/django/mysite$ python manage.py makemigrations polls
# migration run
~/django/mysite$ python manage.py migrate
django์์๋ class myname(model.Model): ๊ณผ ๊ฐ์ ๋ชจ์์ ๋ง์ด ์ฐ๊ฒ ๋๋๋ฐ model.Model์ด๋ผ๋ class๋ฅผ ๋ด๊ฐ ์ง์ ํ ํด๋ ์ค ์ด๋ฆ์ผ๋ก ๋ถ๋ฌ์จ๋ค์ model.Model์ ์๋ ๋ณ์๋ค์ ์ค์ ํ๋ ์์ ์ ํ๋ ๊ฒ์ด๋ผ๊ณ ์ดํดํ๋ ๊ฒ์ด ํธํ๋ค.