1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

django3 議事録1

Posted at

djangoとは

Pythonで実装されたwebアプリフレームワーク

ざっくりな流れ

image.png

アプリの構成(ベストプラクティス?)

image.png

APIを持つ場合は、こう

image.png

setting.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'], ←ここを修正
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

BASE_DIRはmanage.pyがある場所を指す。この場合manage.pyのある階層のtemplatesを指す

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todo.apps.TodoConfig', ←追加
]

新しいアプリケーションを使う際に記載が必要
今回だと、以下
image.png

models.py

データを効率的の扱えるようにする仕組み(DB)

from django.db import models

# Create your models here.

class TodoModel(models.Model):
    title = models.CharField(max_length=100)
    memo = models.TextField()

    def __str__(self):
        return self.title

○○fieldは種類が結構あるので他で参照

mikemigrationsとmigrate

image.png

mikemigrations
image.png

migrate
urlのadminで入れる
ユーザー作成は以下
python3 manage.py createsuperuser
image.png

html
image.png

どういうwebアプリに何に向いているか(触った感触)

  • 管理機能系(付属DBでいいなら小さい機能なら直良)
  • ヘルパー関数でユーザー単位に権限がつけれる、またログインチェックなど用意されており容易に実装可能
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login


def loginfunc(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return render(request, 'login.html', {'context': 'logged in'})
        else:
            return render(request, 'login.html', {'context': 'not logged in'})
    return render(request, 'login.html', {'context': 'get method'})
    

djangoでCRUDwebアプリ作りたいんだ!って人向けな気がする。またはモノレポ向け

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?