質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.39%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

255閲覧

【Python】TemplateDoesNotExist at /accounts/profile_edit/

Java_student

総合スコア79

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2024/08/06 07:01

編集2024/08/06 08:54

実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

quickquip

2024/08/06 08:00

エラーはTracebackも含めて載せてください。「accounts/profile_edit.htmlが存在しないと表示される。」は"エラーを読んであなたが読み取ったこと"でしかありません。回答側にはそのような解釈は基本的に不要です(こう思っているのですが違うのですか? という質問ならあってもよいですが)
Java_student

2024/08/06 10:36

Tracebackも含めて載せさせて頂きました。 拙くてすみませんでした。
guest

回答1

0

ベストアンサー

ファイル配置が

Project |-templates |-allauth |-account |-profile_edit.html

で、Project/templatesProject/templates/allauthをsettings.pyのTEMPLATESに入れているならテンプレートの名前は

account/profile_edit.html もしくは allauth/account/profile_edit.html では?
(コードには account s /profile_edit.html と書いてあります)
(もしくはディレクトリ名を accounts にするか)


(追記)
accounts がアプリケーションとして追加されているようなので

Project |-accounts |-templates |-accounts |-profile_edit.html

の位置に置くのが自然なのではないでしょうか?
(コードは今のrender(request, 'accounts/profile_edit.html', 略)のまま)

投稿2024/08/06 07:50

編集2024/08/07 03:05
quickquip

総合スコア11187

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Java_student

2024/08/07 07:48

ありがとうございます。 表示されました!
quickquip

2024/08/07 08:40

何をしたのです? という疑問が(常に)あります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.39%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問