django

cd deskotop
mkdir プロジェクト名
cd プロジェクト名
pip3 install pipenv
pipenv shell
pipenv install django
pipenv install --dev flake8 autopep8 
django-admin startproject config . #ドットはこのフォルダに作ると言う指示 
python manage.py startapp アプリ名​

git追加

new repository プロジェクト名

create repositry

git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/naoki868/kikoo2.git #保存先
git push -u origin main​

django

DIRSにこのプロジェクトのtemlやcssのファイルを入れると指定するその後、プロジェクトファイルにtemplateディレクトリを作成しそのディレクトリにアプリケーションディレクトリを作成、その中にhtmlファイルを作る。

 #config /setting.py
TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [os.path.join(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',
           ],
       },
   },
]​

config のurls.pyの設定

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

urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('user.urls')),
]

userディレクトリにurls.py を作成する。

from django.urls import path
from . import views

app_name = 'user'
urlpatterns = [
   path('home/', views.HomeView.as_view(), name='home')
] #HomeViewはviews .py で作るクラスメイを使う。

views.py の設定

from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.


class HomeView(TemplateView):
   template_name = "user/index.html"

これでHTMLの表示が完了した。途中エラーが出た!

template doesnot exist

これが表示された時はsettings.pyを確認しよう。templateにpath買いた時に保存忘れていて、osをimportできてないことに気づかなかった。。。



この記事が気に入ったらサポートをしてみませんか?