質問編集履歴
2
内容の変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
django
|
1
|
+
djangoの自作したカスタムユーザーモデルにユーザー情報が保存されない
|
test
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
-
djangoで
|
1
|
+
djangoでUserというカスタムユーザーモデルを作り、djangoの管理画面にもそのカスタムユーザーモデルが適用されているのですが、views.pyのregister関数が呼び出すフォームで送信してもデータベースに保存されません。
|
2
|
-
|
2
|
+
|
3
|
-
よ
|
3
|
+
どうすれば保存できるようになるか、また、UserCreationFormを継承するとパスワード入力欄が3つ出てきてしまうのですが、2つか1つにする方法も教えていただけると嬉しいです。
|
4
4
|
|
5
5
|
```python
|
6
6
|
|
7
|
+
|
8
|
+
|
9
|
+
###views.py
|
10
|
+
|
11
|
+
|
12
|
+
|
7
13
|
from django.shortcuts import render, redirect, get_object_or_404
|
8
14
|
|
9
15
|
from django.views.generic import CreateView, DeleteView
|
@@ -12,15 +18,19 @@
|
|
12
18
|
|
13
19
|
from django.http import HttpResponse
|
14
20
|
|
15
|
-
from django.contrib.auth import authenticate, login
|
21
|
+
from django.contrib.auth import authenticate, login, logout, get_user_model
|
16
22
|
|
17
23
|
from django.contrib.auth.decorators import login_required
|
18
24
|
|
19
25
|
|
20
26
|
|
27
|
+
User = get_user_model()
|
28
|
+
|
29
|
+
|
30
|
+
|
21
|
-
from .forms import
|
31
|
+
from .forms import UserCreateForm
|
22
|
-
|
32
|
+
|
23
|
-
from .models import
|
33
|
+
from .models import User
|
24
34
|
|
25
35
|
|
26
36
|
|
@@ -32,168 +42,538 @@
|
|
32
42
|
|
33
43
|
def register(request):
|
34
44
|
|
35
|
-
form = AddressModelCreateForm(request.POST or None)
|
36
|
-
|
37
45
|
|
38
46
|
|
39
47
|
if request.method == 'POST':
|
40
48
|
|
49
|
+
form = UserCreateForm(request.POST)
|
50
|
+
|
41
|
-
|
51
|
+
if form.is_valid():
|
52
|
+
|
42
|
-
|
53
|
+
email = form.cleaned_data.get('email')
|
54
|
+
|
55
|
+
password = form.cleaned_data.get('password')
|
56
|
+
|
57
|
+
location = form.cleaned_data.get('location')
|
58
|
+
|
59
|
+
user = User.objects.create_user(email, password, location)
|
60
|
+
|
43
|
-
|
61
|
+
user.save()
|
44
62
|
|
45
63
|
return redirect('register_comp')
|
46
64
|
|
47
|
-
|
65
|
+
|
48
|
-
|
49
|
-
return render(request, 'register.html', {
|
50
|
-
|
51
|
-
'error': 'このメールアドレスは登録されています',
|
52
|
-
|
53
|
-
'form': AddressModelCreateForm()
|
54
|
-
|
55
|
-
})
|
56
66
|
|
57
67
|
|
58
68
|
|
59
|
-
return render(request, 'register.html', {'form':
|
69
|
+
return render(request, 'register.html', {'form': UserCreateForm()})
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def registercomp(request):
|
68
|
-
|
69
|
-
return render(request, 'register_comp.html', {'message': '登録が完了しました'})
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
@login_required
|
78
|
-
|
79
|
-
def update(request, pk):
|
80
|
-
|
81
|
-
addressModel = get_object_or_404(AddressModel, pk=pk)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
form = AddressModelCreateForm(request.POST or None, instance=addressModel)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
if request.method == 'POST' and form.is_valid():
|
90
|
-
|
91
|
-
form.save()
|
92
|
-
|
93
|
-
return redirect('register_comp')
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
return render(request, 'update.html', {'form': form})
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
def loginfunc(request):
|
104
|
-
|
105
|
-
if request.method == 'POST':
|
106
|
-
|
107
|
-
username = request.POST['username']
|
108
|
-
|
109
|
-
password = request.POST['password']
|
110
|
-
|
111
|
-
user = authenticate(request, username=username, password=password)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
if user is not None:
|
116
|
-
|
117
|
-
login(request, user)
|
118
|
-
|
119
|
-
return redirect('login')
|
120
|
-
|
121
|
-
else:
|
122
|
-
|
123
|
-
return render(request, 'login.html', {
|
124
|
-
|
125
|
-
'error': '未登録ユーザーです',
|
126
|
-
|
127
|
-
'form': AddressModelCreateForm()
|
128
|
-
|
129
|
-
})
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
return render(request, 'login.html', {'form': AddressModelCreateForm()})
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
70
|
|
139
71
|
|
140
72
|
|
141
73
|
```
|
142
74
|
|
143
|
-
```h
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
75
|
+
```python
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
###models.py
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
from django.db import models
|
84
|
+
|
85
|
+
from django.core.mail import send_mail
|
86
|
+
|
87
|
+
from django.contrib.auth.models import PermissionsMixin
|
88
|
+
|
89
|
+
from django.contrib.auth.base_user import AbstractBaseUser
|
90
|
+
|
91
|
+
from django.utils.translation import ugettext_lazy as _
|
92
|
+
|
93
|
+
from django.utils import timezone
|
94
|
+
|
95
|
+
from django.contrib.auth.base_user import BaseUserManager
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
class UserManager(BaseUserManager):
|
102
|
+
|
103
|
+
"""ユーザーマネージャー."""
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
use_in_migrations = True
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def _create_user(self, email, password, **extra_fields):
|
112
|
+
|
113
|
+
"""Create and save a user with the given username, email, and
|
114
|
+
|
115
|
+
password."""
|
116
|
+
|
117
|
+
if not email:
|
118
|
+
|
119
|
+
raise ValueError('The given email must be set')
|
120
|
+
|
121
|
+
email = self.normalize_email(email)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
user = self.model(email=email, **extra_fields)
|
126
|
+
|
127
|
+
user.set_password(password)
|
128
|
+
|
129
|
+
user.save(using=self._db)
|
130
|
+
|
131
|
+
return user
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def create_user(self, email, password=None, **extra_fields):
|
136
|
+
|
137
|
+
extra_fields.setdefault('is_staff', False)
|
138
|
+
|
139
|
+
extra_fields.setdefault('is_superuser', False)
|
140
|
+
|
141
|
+
return self._create_user(email, password, **extra_fields)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def create_superuser(self, email, password, **extra_fields):
|
146
|
+
|
147
|
+
extra_fields.setdefault('is_staff', True)
|
148
|
+
|
149
|
+
extra_fields.setdefault('is_superuser', True)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if extra_fields.get('is_staff') is not True:
|
154
|
+
|
155
|
+
raise ValueError('Superuser must have is_staff=True.')
|
156
|
+
|
157
|
+
if extra_fields.get('is_superuser') is not True:
|
158
|
+
|
159
|
+
raise ValueError('Superuser must have is_superuser=True.')
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
return self._create_user(email, password, **extra_fields)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
LOCATION = (('130010', '東京'), ('230010', '名古屋'), ('270000', '大阪'), ('400010', '福岡'))
|
170
|
+
|
171
|
+
class User(AbstractBaseUser, PermissionsMixin):
|
172
|
+
|
173
|
+
"""カスタムユーザーモデル."""
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
email = models.EmailField(_('email address'), unique=True)
|
178
|
+
|
179
|
+
first_name = models.CharField(_('first name'), max_length=30, blank=True)
|
180
|
+
|
181
|
+
last_name = models.CharField(_('last name'), max_length=150, blank=True)
|
182
|
+
|
183
|
+
location = models.CharField(
|
184
|
+
|
185
|
+
'住所',
|
186
|
+
|
187
|
+
max_length = 20,
|
188
|
+
|
189
|
+
choices = LOCATION
|
190
|
+
|
191
|
+
)
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
is_staff = models.BooleanField(
|
196
|
+
|
197
|
+
_('staff status'),
|
198
|
+
|
199
|
+
default=False,
|
200
|
+
|
201
|
+
help_text=_(
|
202
|
+
|
203
|
+
'Designates whether the user can log into this admin site.'),
|
204
|
+
|
205
|
+
)
|
206
|
+
|
207
|
+
is_active = models.BooleanField(
|
208
|
+
|
209
|
+
_('active'),
|
210
|
+
|
211
|
+
default=True,
|
212
|
+
|
213
|
+
help_text=_(
|
214
|
+
|
215
|
+
'Designates whether this user should be treated as active. '
|
216
|
+
|
217
|
+
'Unselect this instead of deleting accounts.'
|
218
|
+
|
219
|
+
),
|
220
|
+
|
221
|
+
)
|
222
|
+
|
223
|
+
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
objects = UserManager()
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
EMAIL_FIELD = 'email'
|
232
|
+
|
233
|
+
USERNAME_FIELD = 'email'
|
234
|
+
|
235
|
+
REQUIRED_FIELDS = []
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
class Meta:
|
240
|
+
|
241
|
+
verbose_name = _('user')
|
242
|
+
|
243
|
+
verbose_name_plural = _('users')
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def get_full_name(self):
|
248
|
+
|
249
|
+
"""Return the first_name plus the last_name, with a space in
|
250
|
+
|
251
|
+
between."""
|
252
|
+
|
253
|
+
full_name = '%s %s' % (self.first_name, self.last_name)
|
254
|
+
|
255
|
+
return full_name.strip()
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
def get_short_name(self):
|
260
|
+
|
261
|
+
"""Return the short name for the user."""
|
262
|
+
|
263
|
+
return self.first_name
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
def email_user(self, subject, message, from_email=None, **kwargs):
|
268
|
+
|
269
|
+
"""Send an email to this user."""
|
270
|
+
|
271
|
+
send_mail(subject, message, from_email, [self.email], **kwargs)
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
198
278
|
|
199
279
|
```
|
280
|
+
|
281
|
+
```python
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
###forms.py
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
from django import forms
|
290
|
+
|
291
|
+
from django.contrib.auth.forms import UserCreationForm
|
292
|
+
|
293
|
+
from django.contrib.auth import get_user_model
|
294
|
+
|
295
|
+
User = get_user_model()
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
class UserCreateForm(UserCreationForm):
|
304
|
+
|
305
|
+
password = forms.CharField(widget=forms.PasswordInput)
|
306
|
+
|
307
|
+
password1 = forms.CharField(required=False)
|
308
|
+
|
309
|
+
password2 = password1
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
class Meta:
|
314
|
+
|
315
|
+
model = User
|
316
|
+
|
317
|
+
fields = ('email', 'password', 'location')
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
```
|
324
|
+
|
325
|
+
```python
|
326
|
+
|
327
|
+
"""
|
328
|
+
|
329
|
+
Django settings for weathermailproject project.
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
Generated by 'django-admin startproject' using Django 2.2.5.
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
For more information on this file, see
|
338
|
+
|
339
|
+
https://docs.djangoproject.com/en/2.2/topics/settings/
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
For the full list of settings and their values, see
|
344
|
+
|
345
|
+
https://docs.djangoproject.com/en/2.2/ref/settings/
|
346
|
+
|
347
|
+
"""
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
import os
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
356
|
+
|
357
|
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
# Quick-start development settings - unsuitable for production
|
364
|
+
|
365
|
+
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
# SECURITY WARNING: keep the secret key used in production secret!
|
370
|
+
|
371
|
+
SECRET_KEY = '_3&*rp@a@yxu1_8%w%rw7qh7&^@57#15s6(mbep2(1&!nl8=+h'
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
# SECURITY WARNING: don't run with debug turned on in production!
|
376
|
+
|
377
|
+
DEBUG = True
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
ALLOWED_HOSTS = []
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
# Application definition
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
INSTALLED_APPS = [
|
392
|
+
|
393
|
+
'django.contrib.admin',
|
394
|
+
|
395
|
+
'django.contrib.auth',
|
396
|
+
|
397
|
+
'django.contrib.contenttypes',
|
398
|
+
|
399
|
+
'django.contrib.sessions',
|
400
|
+
|
401
|
+
'django.contrib.messages',
|
402
|
+
|
403
|
+
'django.contrib.staticfiles',
|
404
|
+
|
405
|
+
'weathermail',
|
406
|
+
|
407
|
+
]
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
MIDDLEWARE = [
|
412
|
+
|
413
|
+
'django.middleware.security.SecurityMiddleware',
|
414
|
+
|
415
|
+
'django.contrib.sessions.middleware.SessionMiddleware',
|
416
|
+
|
417
|
+
'django.middleware.common.CommonMiddleware',
|
418
|
+
|
419
|
+
'django.middleware.csrf.CsrfViewMiddleware',
|
420
|
+
|
421
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
422
|
+
|
423
|
+
'django.contrib.messages.middleware.MessageMiddleware',
|
424
|
+
|
425
|
+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
426
|
+
|
427
|
+
]
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
ROOT_URLCONF = 'weathermailproject.urls'
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
TEMPLATES = [
|
436
|
+
|
437
|
+
{
|
438
|
+
|
439
|
+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
440
|
+
|
441
|
+
'DIRS': [BASE_DIR, 'templates'],
|
442
|
+
|
443
|
+
'APP_DIRS': True,
|
444
|
+
|
445
|
+
'OPTIONS': {
|
446
|
+
|
447
|
+
'context_processors': [
|
448
|
+
|
449
|
+
'django.template.context_processors.debug',
|
450
|
+
|
451
|
+
'django.template.context_processors.request',
|
452
|
+
|
453
|
+
'django.contrib.auth.context_processors.auth',
|
454
|
+
|
455
|
+
'django.contrib.messages.context_processors.messages',
|
456
|
+
|
457
|
+
],
|
458
|
+
|
459
|
+
},
|
460
|
+
|
461
|
+
},
|
462
|
+
|
463
|
+
]
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
WSGI_APPLICATION = 'weathermailproject.wsgi.application'
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
# Database
|
474
|
+
|
475
|
+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
DATABASES = {
|
480
|
+
|
481
|
+
'default': {
|
482
|
+
|
483
|
+
'ENGINE': 'django.db.backends.sqlite3',
|
484
|
+
|
485
|
+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
# Password validation
|
496
|
+
|
497
|
+
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
AUTH_PASSWORD_VALIDATORS = [
|
502
|
+
|
503
|
+
{
|
504
|
+
|
505
|
+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
506
|
+
|
507
|
+
},
|
508
|
+
|
509
|
+
{
|
510
|
+
|
511
|
+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
512
|
+
|
513
|
+
},
|
514
|
+
|
515
|
+
{
|
516
|
+
|
517
|
+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
518
|
+
|
519
|
+
},
|
520
|
+
|
521
|
+
{
|
522
|
+
|
523
|
+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
524
|
+
|
525
|
+
},
|
526
|
+
|
527
|
+
]
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
# Internationalization
|
534
|
+
|
535
|
+
# https://docs.djangoproject.com/en/2.2/topics/i18n/
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
LANGUAGE_CODE = 'ja'
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
TIME_ZONE = 'Asia/Tokyo'
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
USE_I18N = True
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
USE_L10N = True
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
USE_TZ = True
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
# Static files (CSS, JavaScript, Images)
|
562
|
+
|
563
|
+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
STATIC_URL = '/static/'
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
LOGIN_URL = 'login'
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
AUTH_USER_MODEL = 'weathermail.User'
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
```
|
1
内容の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
djangoでログイン機能を作
|
1
|
+
djangoでログイン機能を作りたいのですが、loginfunc関数のauthenticateでの照合が、自分で作ったAddressModelではなく、djangoに元から備わっているUserオブジェクトにされている気がするのですが、もしそうなのであればどうすればAddressModelのオブジェクトと照合できるのか教えていただきたいです。
|
2
|
-
|
3
|
-
原因がわかる方がいらっしゃいましたら教えていただきたいです。
|
4
2
|
|
5
3
|
よろしくお願いします。
|
6
4
|
|