teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

書きミスを直しました。

2021/11/05 15:55

投稿

rike
rike

スコア0

title CHANGED
File without changes
body CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
  ### 該当のソースコード
17
-
17
+ post_list.html
18
18
  ```post_list.html
19
19
  <div>
20
20
  <h1><a href="/">Django Girls Blog</a></h1>
@@ -28,6 +28,7 @@
28
28
  </div>
29
29
  {% endfor %}
30
30
  ```
31
+ views.py
31
32
  ```views.py
32
33
  from django.shortcuts import render, get_object_or_404
33
34
  from django.shortcuts import render
@@ -50,6 +51,7 @@
50
51
  return render(request, 'blog/post_detail.html', {'post': post})
51
52
 
52
53
  ```
54
+ setting.py
53
55
  ```setting.py
54
56
  """
55
57
  Django settings for mysite project.

1

ソースコードを追加しました。

2021/11/05 15:55

投稿

rike
rike

スコア0

title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
 
16
16
  ### 該当のソースコード
17
17
 
18
- ```ここに言語名を入力
18
+ ```post_list.html
19
19
  <div>
20
20
  <h1><a href="/">Django Girls Blog</a></h1>
21
21
  </div>
@@ -28,7 +28,153 @@
28
28
  </div>
29
29
  {% endfor %}
30
30
  ```
31
+ ```views.py
32
+ from django.shortcuts import render, get_object_or_404
33
+ from django.shortcuts import render
34
+ from django.utils import timezone
35
+ from .models import Post
36
+ from django.views.generic import ListView
37
+
38
+ class PostListView(ListView):
39
+ model = Post
40
+
41
+ def get_queryset(self):
42
+ return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
31
43
 
44
+ def post_list(request):
45
+ posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
46
+ return render(request, 'blog/post_list.html', {'posts': posts})
47
+
48
+ def post_detail(request, pk):
49
+ post = get_object_or_404(Post, pk=pk)
50
+ return render(request, 'blog/post_detail.html', {'post': post})
51
+
52
+ ```
53
+ ```setting.py
54
+ """
55
+ Django settings for mysite project.
56
+
57
+ Generated by 'django-admin startproject' using Django 2.2.24.
58
+
59
+ For more information on this file, see
60
+ https://docs.djangoproject.com/en/2.2/topics/settings/
61
+
62
+ For the full list of settings and their values, see
63
+ https://docs.djangoproject.com/en/2.2/ref/settings/
64
+ """
65
+
66
+ import os
67
+
68
+ # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
69
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
70
+
71
+
72
+ # Quick-start development settings - unsuitable for production
73
+ # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
74
+
75
+ # SECURITY WARNING: keep the secret key used in production secret!
76
+ SECRET_KEY = '8jkypoy2d4*t!d3c_xg4(&vh!@s-)qjn4)rgj7sc5*940bdqt2'
77
+
78
+ # SECURITY WARNING: don't run with debug turned on in production!
79
+ DEBUG = True
80
+
81
+ ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
82
+
83
+
84
+ # Application definition
85
+
86
+ INSTALLED_APPS = [
87
+ 'django.contrib.admin',
88
+ 'django.contrib.auth',
89
+ 'django.contrib.contenttypes',
90
+ 'django.contrib.sessions',
91
+ 'django.contrib.messages',
92
+ 'django.contrib.staticfiles',
93
+ 'blog.apps.BlogConfig',
94
+ ]
95
+
96
+ MIDDLEWARE = [
97
+ 'django.middleware.security.SecurityMiddleware',
98
+ 'django.contrib.sessions.middleware.SessionMiddleware',
99
+ 'django.middleware.common.CommonMiddleware',
100
+ 'django.middleware.csrf.CsrfViewMiddleware',
101
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
102
+ 'django.contrib.messages.middleware.MessageMiddleware',
103
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
104
+ ]
105
+
106
+ ROOT_URLCONF = 'mysite.urls'
107
+
108
+ TEMPLATES = [
109
+ {
110
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
111
+ 'DIRS': [],
112
+ 'APP_DIRS': True,
113
+ 'OPTIONS': {
114
+ 'context_processors': [
115
+ 'django.template.context_processors.debug',
116
+ 'django.template.context_processors.request',
117
+ 'django.contrib.auth.context_processors.auth',
118
+ 'django.contrib.messages.context_processors.messages',
119
+ ],
120
+ },
121
+ },
122
+ ]
123
+
124
+ WSGI_APPLICATION = 'mysite.wsgi.application'
125
+
126
+
127
+ # Database
128
+ # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
129
+
130
+ DATABASES = {
131
+ 'default': {
132
+ 'ENGINE': 'django.db.backends.sqlite3',
133
+ 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
134
+ }
135
+ }
136
+
137
+
138
+ # Password validation
139
+ # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
140
+
141
+ AUTH_PASSWORD_VALIDATORS = [
142
+ {
143
+ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
144
+ },
145
+ {
146
+ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
147
+ },
148
+ {
149
+ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
150
+ },
151
+ {
152
+ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
153
+ },
154
+ ]
155
+
156
+
157
+ # Internationalization
158
+ # https://docs.djangoproject.com/en/2.2/topics/i18n/
159
+
160
+ LANGUAGE_CODE = 'ja'
161
+
162
+ TIME_ZONE = 'Asia/Tokyo'
163
+
164
+ USE_I18N = True
165
+
166
+ USE_L10N = True
167
+
168
+ USE_TZ = True
169
+
170
+
171
+ # Static files (CSS, JavaScript, Images)
172
+ # https://docs.djangoproject.com/en/2.2/howto/static-files/
173
+
174
+ STATIC_URL = '/static/'
175
+ STATIC_ROOT = os.path.join(BASE_DIR, 'static')
176
+
177
+ ```
32
178
  ### 試したこと
33
179
 
34
180
  静的ファイルがうまく作動していない?と思ったので