質問編集履歴

2

2019/12/05 23:38

投稿

shmpmrkw
shmpmrkw

スコア4

test CHANGED
@@ -1 +1 @@
1
- Djnago 新規ユーザー登録後ログインがなされない
1
+ Djnago authenticate()でNoneが常され
test CHANGED
File without changes

1

model.py × form.py ○

2019/12/05 23:38

投稿

shmpmrkw
shmpmrkw

スコア4

test CHANGED
File without changes
test CHANGED
@@ -62,77 +62,229 @@
62
62
 
63
63
  ```
64
64
 
65
+ form.py
66
+
67
+ ```python3
68
+
69
+
70
+
71
+ class UserCreationForm(forms.ModelForm):
72
+
73
+ password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
74
+
75
+ password2 = forms.CharField(
76
+
77
+ label='Password confirmation', widget=forms.PasswordInput)
78
+
79
+
80
+
81
+ class Meta:
82
+
83
+ model = User
84
+
85
+ fields = ('email', 'username')
86
+
87
+
88
+
89
+ def clean_password2(self):
90
+
91
+ password1 = self.cleaned_data.get("password1")
92
+
93
+ password2 = self.cleaned_data.get("password2")
94
+
95
+ if password1 and password2 and password1 != password2:
96
+
97
+ raise forms.ValidationError("Passwords don't match")
98
+
99
+ return password2
100
+
101
+
102
+
103
+ def save(self, commit=True):
104
+
105
+ user = super().save(commit=False)
106
+
107
+ user.set_password(self.cleaned_data["password1"])
108
+
109
+ if commit:
110
+
111
+ user.save()
112
+
113
+ return user
114
+
115
+
116
+
117
+
118
+
119
+ class UserChangeForm(forms.ModelForm):
120
+
121
+ password = ReadOnlyPasswordHashField()
122
+
123
+
124
+
125
+ class Meta:
126
+
127
+ model = User
128
+
129
+ fields = ('email', 'password', 'username','profile','prof_img','cover_img')
130
+
131
+
132
+
133
+ def clean_password(self):
134
+
135
+ return self.initial["password"]
136
+
137
+ ```
138
+
139
+
140
+
141
+
142
+
65
143
  model.py
66
144
 
67
145
  ```python3
68
146
 
69
-
70
-
71
- class UserCreationForm(forms.ModelForm):
147
+ class UserManager(BaseUserManager):
72
-
73
- password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
148
+
74
-
75
- password2 = forms.CharField(
76
-
77
- label='Password confirmation', widget=forms.PasswordInput)
149
+ def create_user(self, username, email, password=None):
78
-
79
-
80
-
150
+
81
- class Meta:
151
+ if not username:
82
-
83
- model = User
152
+
84
-
85
- fields = ('email', 'username')
153
+ raise ValueError('Users must have an username')
154
+
155
+
156
+
86
-
157
+ user = self.model(
158
+
87
-
159
+ username=username,
160
+
88
-
161
+ email=self.normalize_email(email),
162
+
163
+ )
164
+
165
+
166
+
89
- def clean_password2(self):
167
+ user.set_password(password)
90
-
91
- password1 = self.cleaned_data.get("password1")
168
+
92
-
93
- password2 = self.cleaned_data.get("password2")
94
-
95
- if password1 and password2 and password1 != password2:
96
-
97
- raise forms.ValidationError("Passwords don't match")
98
-
99
- return password2
100
-
101
-
102
-
103
- def save(self, commit=True):
104
-
105
- user = super().save(commit=False)
106
-
107
- user.set_password(self.cleaned_data["password1"])
108
-
109
- if commit:
110
-
111
- user.save()
169
+ user.save(using=self._db)
112
170
 
113
171
  return user
114
172
 
115
173
 
116
174
 
117
-
175
+ def create_superuser(self, username, email, password):
176
+
118
-
177
+ user = self.create_user(
178
+
179
+ username=username,
180
+
181
+ email=self.normalize_email(email),
182
+
183
+ password=password,
184
+
185
+ )
186
+
187
+ user.is_admin = True
188
+
189
+ user.is_staff = True
190
+
191
+ user.is_superuser = True
192
+
193
+ user.save(using=self._db)
194
+
195
+ return user
196
+
197
+
198
+
199
+
200
+
119
- class UserChangeForm(forms.ModelForm):
201
+ class User(AbstractBaseUser):
202
+
120
-
203
+ username_validator = UnicodeUsernameValidator()
204
+
121
- password = ReadOnlyPasswordHashField()
205
+ username = models.CharField(
206
+
122
-
207
+ _('username'),
123
-
124
-
208
+
125
- class Meta:
209
+ max_length=150,
126
-
210
+
127
- model = User
211
+ unique=True,
128
-
212
+
129
- fields = ('email', 'password', 'username','profile','prof_img','cover_img')
213
+ help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
214
+
130
-
215
+ validators=[username_validator],
216
+
131
-
217
+ error_messages={
218
+
132
-
219
+ 'unique': _("A user with that username already exists."),
220
+
221
+ },
222
+
223
+ )
224
+
225
+ email = models.EmailField(
226
+
227
+ verbose_name='email',
228
+
229
+ max_length=255,
230
+
231
+ unique=True,
232
+
233
+ )
234
+
235
+ profile = models.CharField(max_length=200, blank=True)
236
+
237
+ prof_img = models.ImageField(upload_to='prof_img',verbose_name='プロフィール画像',blank=True)
238
+
239
+ cover_img = models.ImageField(upload_to='cover_img',verbose_name='背景画像',blank=True)
240
+
241
+
242
+
243
+ is_active = models.BooleanField(default=True)
244
+
245
+ is_admin = models.BooleanField(default=False)
246
+
247
+ date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
248
+
249
+
250
+
251
+ objects = UserManager()
252
+
253
+
254
+
255
+ EMAIL_FIELD = 'email'
256
+
257
+ USERNAME_FIELD = 'username'
258
+
259
+ REQUIRED_FIELDS = []
260
+
261
+
262
+
263
+ def __str__(self):
264
+
265
+ return self.username
266
+
267
+
268
+
269
+ def has_perm(self, perm, obj=None):
270
+
271
+ return True
272
+
273
+
274
+
275
+ def has_module_perms(self, app_label):
276
+
277
+ return True
278
+
279
+
280
+
281
+ @property
282
+
133
- def clean_password(self):
283
+ def is_staff(self):
134
-
284
+
135
- return self.initial["password"]
285
+ return self.is_admin
286
+
287
+
136
288
 
137
289
  ```
138
290
 
@@ -140,158 +292,6 @@
140
292
 
141
293
 
142
294
 
143
- model.py
144
-
145
- ```python3
146
-
147
- class UserManager(BaseUserManager):
148
-
149
- def create_user(self, username, email, password=None):
150
-
151
- if not username:
152
-
153
- raise ValueError('Users must have an username')
154
-
155
-
156
-
157
- user = self.model(
158
-
159
- username=username,
160
-
161
- email=self.normalize_email(email),
162
-
163
- )
164
-
165
-
166
-
167
- user.set_password(password)
168
-
169
- user.save(using=self._db)
170
-
171
- return user
172
-
173
-
174
-
175
- def create_superuser(self, username, email, password):
176
-
177
- user = self.create_user(
178
-
179
- username=username,
180
-
181
- email=self.normalize_email(email),
182
-
183
- password=password,
184
-
185
- )
186
-
187
- user.is_admin = True
188
-
189
- user.is_staff = True
190
-
191
- user.is_superuser = True
192
-
193
- user.save(using=self._db)
194
-
195
- return user
196
-
197
-
198
-
199
-
200
-
201
- class User(AbstractBaseUser):
202
-
203
- username_validator = UnicodeUsernameValidator()
204
-
205
- username = models.CharField(
206
-
207
- _('username'),
208
-
209
- max_length=150,
210
-
211
- unique=True,
212
-
213
- help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
214
-
215
- validators=[username_validator],
216
-
217
- error_messages={
218
-
219
- 'unique': _("A user with that username already exists."),
220
-
221
- },
222
-
223
- )
224
-
225
- email = models.EmailField(
226
-
227
- verbose_name='email',
228
-
229
- max_length=255,
230
-
231
- unique=True,
232
-
233
- )
234
-
235
- profile = models.CharField(max_length=200, blank=True)
236
-
237
- prof_img = models.ImageField(upload_to='prof_img',verbose_name='プロフィール画像',blank=True)
238
-
239
- cover_img = models.ImageField(upload_to='cover_img',verbose_name='背景画像',blank=True)
240
-
241
-
242
-
243
- is_active = models.BooleanField(default=True)
244
-
245
- is_admin = models.BooleanField(default=False)
246
-
247
- date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
248
-
249
-
250
-
251
- objects = UserManager()
252
-
253
-
254
-
255
- EMAIL_FIELD = 'email'
256
-
257
- USERNAME_FIELD = 'username'
258
-
259
- REQUIRED_FIELDS = []
260
-
261
-
262
-
263
- def __str__(self):
264
-
265
- return self.username
266
-
267
-
268
-
269
- def has_perm(self, perm, obj=None):
270
-
271
- return True
272
-
273
-
274
-
275
- def has_module_perms(self, app_label):
276
-
277
- return True
278
-
279
-
280
-
281
- @property
282
-
283
- def is_staff(self):
284
-
285
- return self.is_admin
286
-
287
-
288
-
289
- ```
290
-
291
-
292
-
293
-
294
-
295
295
  ### 試したこと
296
296
 
297
297