Django 및 ORM 검토

6839 단어
Django 지식 요약
1. 데이터베이스 구성
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '    ',
        'USER': '  ',
        'PASSWORD': '  ',
        'HOST': '   127.0.0.1,       ',
        "PORT": 3306,  #    3306,       
    }


2. 라우팅 배포
from django.conf.urls import url, include
urlpatterns = [
    # ...
    url(r'^api/', include('api.urls'))   # /api/test/
]

   :

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^users/$', views.UserAPIview.as_view())
]

ORM 구성 검토
models.py
from django.db import models

class User(models.Model):
    SEX_CHOICES = ((0, ' '), (1, ' '))  #          
    name = models.CharField(max_length=64, verbose_name='  ')
    age = models.IntegerField()
    height = models.DecimalField(max_digits=5, decimal_places=2, default=0) 
    sex = models.IntegerField(choices=SEX_CHOICES, default=0)  
    # sex = models.CharField(choices=[('0', ' '), ('1', ' ')])  
    icon = models.ImageField(upload_to='icon', default='icon/default.png')

settings.py
# root         os.path 
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
# url      /   
MEDIA_URL = '/media/'

주 경로 맨 아래
from django.views.static import serve
from django.conf import settings

urlpatterns = [

    # icon/default.png => path  
    # media/(?P.*)    media/          
    # document_root      icon/default.png      - media     
    url(r'^media/(?P.*)', serve,{'document_root':settings.MEDIA_ROOT}),
]

좋은 웹페이지 즐겨찾기