-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ログイン・ログアウト機能の実装 #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
# from django.shortcuts import render | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import authenticate, login | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.contrib.auth.models import User | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここで取って来ているUserモデルは、Djangoでデフォルトで提供されている組み込みのモデルです。 |
||
from django.urls import reverse_lazy | ||
from django.views.generic import CreateView | ||
from django.views.generic import CreateView, TemplateView | ||
|
||
from .forms import SignupForm | ||
|
||
|
||
class SignupView(CreateView): | ||
form_class = SignupForm | ||
template_name = "accounts/signup.html" | ||
success_url = reverse_lazy("tweets:home") # リダイレクト先url貼付、success_url使用時遅延評価でurl逆引き | ||
success_url = reverse_lazy( | ||
settings.LOGIN_REDIRECT_URL | ||
) # リダイレクト先url貼付、success_url使用時遅延評価でurl逆引き | ||
|
||
def form_valid(self, form): | ||
response = super().form_valid(form) | ||
|
@@ -21,3 +26,15 @@ def form_valid(self, form): | |
user = authenticate(self.request, username=username, password=password) | ||
login(self.request, user) | ||
return response | ||
|
||
|
||
class UserProfileView(LoginRequiredMixin, TemplateView): | ||
template_name = "accounts/user_profile.html" | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
username = self.kwargs.get("username") | ||
user = User.objects.get(username=username) | ||
context["user"] = user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コンテキストってテンプレート上で表示するために作成しているもので、 |
||
context["username"] = username | ||
return context |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "base.html" %} | ||
{% block title %}login{% endblock %} | ||
{% block content %} | ||
|
||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form.as_p}} | ||
<button type="submit">ログイン</button> | ||
</form> | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}User Profile{% endblock %} | ||
|
||
{% block content %} | ||
<p>{(username)}</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コンテキストを取ってくるには {{ username }} って {{ }} で囲ってあげてください! |
||
|
||
{% endblock %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのコメントアウト外してあげましょう!今のままではプロフィールページ見られないはず