4
3

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 5 years have passed since last update.

Djangoのページを追加する

Posted at

Djangoでページを作成する手順

ディレクトリ

$ tree
.
project
├── instagram_report_site
│   ├── settings.py
│   ├── test.py
│   ├── urls.py ←編集する
│   └── wsgi.py
├── manage.py
├── static
├── report
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── templates ←追加する
│   │   └── report
│   │       └── post_list.html
│   ├── urls.py ←追加する
│   └── views.py ←編集する
└── uwsgi.ini

#全体のURL

まずはこのファイルを編集

├── instagram_report_site
│   ├── urls.py ←編集する

編集した箇所はdjango.urlsのincludeを呼び出し

urls.py
from django.contrib import admin
from django.urls import path, include # ←includeの記述を追加

urlpatterns = [
    path('report/', include('report.urls')), # ← ここを追加
    path('admin/', admin.site.urls),
]

#個別のURL

個別のテンプレートを呼び出す設定をする

・
├── report
│   ├── urls.py ←追加して編集
・
urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
]
・
├── report
│   ├── views.py ←編集
・
views.py
from django.shortcuts import render

# Create your views here.
def post_list(request):
    return render(request, 'report/post_list.html', {})

reportの配下にtemplatesとreportのディレクトリとpost_list.htmlを作成します

・
├── report
│   ├── templates ←追加する
│   │   └── report
│   │       └── post_list.html
・
post_list.html
<html>
<body>
    <p>Hi there!</p>
    <p>It works!</p>
</body>
</html>

runしているサーバーを再起動
以下のURLにアクセス

ec2 18 191 62 6 us east 2 compute amazonaws com 8000 report .png

参考記事

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?