実現したいこと
Djangoでアプリを作成ユーザーのプロフィール編集ページを作成しているのですが、エラーが発生しましたが、どうやらaccounts/profile_edit.htmlが指定できていないらしいのですが、調べても繋げ方が分かりません。
見て貰う事は可能でしょうか
発生している問題・分からないこと
accounts/profile_edit.htmlが存在しないと表示される。
エラーとされている箇所はaccounts/views.pyのreturn render(request, 'accounts/profile_edit.html', {
の箇所でした。
ちなみにaccounts/profile_edit.htmlは
Project |-Project |-accounts |-app |-templates |-allauth |-account |-profile_edit.html
に存在しています。
エラー
Traceback Switch to copy-and-paste view /home/ec2-user/.local/lib/python3.8/site-packages/django/core/handlers/exception.py, line 55, in inner return inner else: @wraps(get_response) def inner(request): try: response = get_response(request) … except Exception as exc: response = response_for_exception(request, exc) return response return inner Local vars Variable Value exc TemplateDoesNotExist('accounts/profile_edit.html') get_response <bound method BaseHandler._get_response of <django.core.handlers.wsgi.WSGIHandler object at 0x7fde9409ad30>> request <WSGIRequest: GET '/accounts/profile_edit/'> /home/ec2-user/.local/lib/python3.8/site-packages/django/core/handlers/base.py, line 197, in _get_response if response is None: wrapped_callback = self.make_view_atomic(callback) # If it is an asynchronous view, run it in a subthread. if iscoroutinefunction(wrapped_callback): wrapped_callback = async_to_sync(wrapped_callback) try: response = wrapped_callback(request, *callback_args, **callback_kwargs) … except Exception as e: response = self.process_exception_by_middleware(e, request) if response is None: raise # Complain if the view returned None (a common error). Local vars Variable Value callback <function View.as_view.<locals>.view at 0x7fde92cc6700> callback_args () callback_kwargs {} request <WSGIRequest: GET '/accounts/profile_edit/'> response None self <django.core.handlers.wsgi.WSGIHandler object at 0x7fde9409ad30> wrapped_callback <function View.as_view.<locals>.view at 0x7fde92cc6700> /home/ec2-user/.local/lib/python3.8/site-packages/django/views/generic/base.py, line 104, in view self = cls(**initkwargs) self.setup(request, *args, **kwargs) if not hasattr(self, "request"): raise AttributeError( "%s instance has no 'request' attribute. Did you override " "setup() and forget to call super()?" % cls.__name__ ) return self.dispatch(request, *args, **kwargs) … view.view_class = cls view.view_initkwargs = initkwargs # __name__ and __qualname__ are intentionally left unchanged as # view_class should be used to robustly determine the name of the view Local vars /home/ec2-user/.local/lib/python3.8/site-packages/django/contrib/auth/mixins.py, line 73, in dispatch class LoginRequiredMixin(AccessMixin): """Verify that the current user is authenticated.""" def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated: return self.handle_no_permission() return super().dispatch(request, *args, **kwargs) … class PermissionRequiredMixin(AccessMixin): """Verify that the current user has all specified permissions.""" permission_required = None Local vars /home/ec2-user/.local/lib/python3.8/site-packages/django/views/generic/base.py, line 143, in dispatch # request method isn't on the approved list. if request.method.lower() in self.http_method_names: handler = getattr( self, request.method.lower(), self.http_method_not_allowed ) else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs) … def http_method_not_allowed(self, request, *args, **kwargs): logger.warning( "Method Not Allowed (%s): %s", request.method, request.path, Local vars /home/ec2-user/environment/agent_test/accounts/views.py, line 30, in get return render(request, 'accounts/profile_edit.html', { … Local vars /home/ec2-user/.local/lib/python3.8/site-packages/django/shortcuts.py, line 24, in render content = loader.render_to_string(template_name, context, request, using=using) … Local vars /home/ec2-user/.local/lib/python3.8/site-packages/django/template/loader.py, line 61, in render_to_string template = get_template(template_name, using=using) … Local vars /home/ec2-user/.local/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template raise TemplateDoesNotExist(template_name, chain=chain) … Local vars Variable Value chain [TemplateDoesNotExist('accounts/profile_edit.html')] engine <django.template.backends.django.DjangoTemplates object at 0x7fde93771310> engines [<django.template.backends.django.DjangoTemplates object at 0x7fde93771310>] template_name 'accounts/profile_edit.html' using None
該当のソースコード
accounts/view.py
1from django.shortcuts import render, redirect 2from django.views.generic import View 3from .models import CustomUser 4from django.contrib.auth.models import AbstractBaseUser 5from django.contrib import messages 6from django.contrib.auth.mixins import LoginRequiredMixin 7from .forms import CustomUpdateForm 8from django.http import HttpResponseRedirect 9from django.views import View 10 11class Show(LoginRequiredMixin, View): 12 template_name = 'accounts/profile.html' 13 def get(self, request, *args, **kwargs): 14 return render(request, 'accounts/profile.html') 15 16 17class Edit(LoginRequiredMixin, View): 18 template_name = 'accounts/profile_edit.html' 19 def get(self, request, *args, **kwargs): 20 user_data = CustomUser.objects.get(id=request.user.id) 21 form = CustomUpdateForm( 22 request.POST or None, 23 initial={ 24 'username': user_data.username, 25 'self_introduction': user_data.self_introduction, 26 'image': user_data.image 27 } 28 ) 29 30 return render(request, 'accounts/profile_edit.html', { 31 'form': form, 32 'user_data': user_data 33 }) 34 35 def post(self, request, *args, **kwargs): 36 form = CustomUpdateForm(request.POST or None) 37 if form.is_valid(): 38 user_data = CustomUser.objects.get(id=request.user.id) 39 user_data.username = form.cleaned_data['username'] 40 user_data.self_introduction = form.cleaned_data['self_introduction'] 41 if request.FILES.get('image'): 42 user_data.image = request.FILES.get('image') 43 user_data.save() 44 return redirect('profile') 45 46 return render(request, 'accounts/profile.html', { 47 'form': form 48 })
accounts/urls.py
1from django.urls import path 2from accounts import views 3app_name = 'accounts' 4urlpatterns = [ 5 path('profile/', views.Show.as_view(), name='profile'), 6 path('profile_edit/', views.Edit.as_view(), name='profile_edit'), 7]
project/urls.py
1from django.contrib import admin 2from django.urls import path,include 3 4urlpatterns = [ 5 path('admin/', admin.site.urls), 6 # 追記 7 path('',include('app.urls')), 8 path('accounts/', include('allauth.urls')), 9 path('accounts/', include('accounts.urls')), 10]
settings.py
1TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'templates'), 5 os.path.join(BASE_DIR, 'templates', 'allauth') 6 ], 7 'APP_DIRS': True, 8 'OPTIONS': { 9 'context_processors': [ 10 'django.template.context_processors.debug', 11 'django.template.context_processors.request', 12 'django.contrib.auth.context_processors.auth', 13 'django.contrib.messages.context_processors.messages', 14 ], 15 }, 16 }, 17]
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
urls.pyにapp_name = 'accounts'を追記したけどダメでした。
またsettings.pyのtemplatesの箇所に'DIRS': [os.path.join(BASE_DIR, 'templates')]を追記して解決した例もあったのですが、既に記載されていました。
補足
python 3
Django 4
回答1件
あなたの回答
tips
プレビュー