学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Django3.2 | クラウドソーシングアプリの構築 | 04 | startapp

03 | Django管理ダッシュボード】 << 【ホーム】 >> 【05 | Bootstrap

アプリを作成します。
今回はアプリ名を「core」としました。
コマンド
python manage.py startapp core

アプリ作成
アプリ作成



「settings.py」ファイルにアプリを登録します。


記述編集 【Desktop/crowdsource/crowdsource/settings.py】40行目

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core', #追加
]



ページを作成します。
「core/views.py」ファイルを以下のように編集します。


記述編集 【Desktop/crowdsource/core/views.py】

from django.shortcuts import render

# Create your views here.
def home(request):
    return render(request, 'home.html')



「crowdsource/urls.py」ファイルにパスの記述を追加します。


記述追加 【Desktop/crowdsource/crowdsource/urls.py】18, 22行目

"""crowdsource URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from core import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
]



「Desktop/crowdsource/core」フォルダに「templates」フォルダを新規作成します。
作成した「templates」フォルダに「home.html」ファイルを新規作成します。



新規作成 【Desktop/crowdsource/core/templates/home.html】

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1" />
        <title>クラウドソーシングアプリ</title>

    </head>
    <body>
        <h1>ホームページ</h1>
    </body>
</html>



Webサーバを起動し、ページを確認します。
コマンド
python manage.py runserver


ブラウザ確認
http://127.0.0.1:8000/

ホームページ
ホームページ


↓↓クリックして頂けると励みになります。

03 | Django管理ダッシュボード】 << 【ホーム】 >> 【05 | Bootstrap