Wrap-up

ํ”„๋กœ์ ํŠธ ๋ฐ APP ์ดˆ๊ธฐ ์„ค์ •

ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

$ django-admin startproject [project name]

settings.py ์„ค์ •

  • ALLOWED_HOST = ['*']

  • Locale

    • Timezone

    • Language

App ๋งŒ๋“ค๊ธฐ

$ python manage.py startapp [app name]

settings.py ์„ค์ •

  • INSTALLED_APPS์— app ๋“ฑ๋ก

Model ์ •์˜

  • models.py

ModelForm ์ •์˜

  • forms.py

์ฝ”๋“œ ์ž‘์„ฑ ํ๋ฆ„

url -> view -> template

views.py

  • ํ•จ์ˆ˜ ( ์ธ์ž -> ๋ฐ˜ํ™˜ return)

OOP๋ž€?

S + V

Import

# urls.py
from django.contrib import admin
from django.urls import path, include

# forms.py
from django import forms

# models.py
from django.db import models

# auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user_model, login, logout
from django.contrib.auth.models import User 

# views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST


from django.conf import settings
from django.contrib import admin

Project setup

$ touch .gitignore

$ python -m venv venv

$ source venv/bin/activate

$ python -m pip install --upgrade pip

$ pip install django==2.1.15 django_extensions django_bootstrap4

$ pip freeze > requirements.txt

$ django-admin startproject django_additional .

Social Login

Installation

$ pip install django-allauth

Google

settings.py

...

INSTALLED_APPS = [
    # pip
    'django_extensions',
    'bootstrap4',
    'bootstrap_pagination',
    'mathfilters',

    # django original
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites', #new
    'storages',

    # my apps
    'articles',
    'accounts',

    # allauth
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.github',
]

...

# allauth setting
AUTHENTICATOIN_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

SITE_ID =1

SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    },
        'github': {
        'SCOPE': [
            'user',
            'repo',
            'read:org',
        ],
    }
}

urls.py

urlpatterns = [
     ...
    path('accounts/', include('accounts.urls')),
    # ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ accounts.url ์•„๋ž˜์—
    path('accounts/', include('allauth.urls')),
    ...
]

GitHub

If you want more than just read-only access to public data, specify the scope as follows. See https://developer.github.com/v3/oauth/#scopes for details.

SOCIALACCOUNT_PROVIDERS = {
    'github': {
        'SCOPE': [
            'user',
            'repo',
            'read:org',
        ],
    }
}

Pagination

django-bootstrap-pagination ์œผ๋กœ Pagination ์˜ˆ์˜๊ฒŒ ํ•˜๊ธฐ

Install

$ pip install django-bootstrap-pagination

settings.py

    INSTALLED_APPS = (
        'bootstrap_pagination',
    )

template

 {% load bootstrap_pagination %}

    ...

{% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}

Last updated