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

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

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

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1543閲覧

Djangoの同一プロジェクト内複数アプリケーションにおけるNoReverseMatch Errorについて

退会済みユーザー

退会済みユーザー

総合スコア0

Django

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/12/23 13:35

前提・実現したいこと

Djangoで同一プロジェクト内に複数のアプリケーションを配置し、一覧ページ(http://localhost:8000/vulnerability/week/)から編集機能を実装し、編集ページ(http://localhost:8000/vulnerability/week/mod/1/)にリダイレクトさせる際に、NoReverseMatchのエラーが発生しました。

mybookプロジェクト内にcmsアプリケーションとvulnerabilityアプリケーションを作成しております。今回のエラーはvulnerabilityアプリケーションにおいて発生したものになります。cmsアプリケーションにおいても、同様のビジネスロジックで実装しておりますが、このようなエラーは発生しておりません。

私個人では能力不足により、/mybook/vulnerability/views.pyに起因する問題なのか、/mybook/cms/templates/vulnerability/base.htmlに起因する問題なのか、判断が付かない状態です。回答者の方には、問題の切り分けを行っていただき、エラーメッセージを解決し、正常な動作に戻していただきたいです。

その他必要な情報は全て提供致します。分かりにくい文章で申し訳ありませんが、どうぞよろしくお願い致します。

発生している問題・エラーメッセージ

NoReverseMatch at /vulnerability/week/mod/1/ Error during template rendering In template ~/project/mybook/cms/templates/vulnerability/base.html, error at line 7 7 <link rel="stylesheet" href="{% static 'cms/css/bootstrap.min.css' %}">

該当のソースコード

(vulnerability/views.py) from django.shortcuts import render, get_object_or_404, redirect from vulnerability.models import Week from vulnerability.forms import WeekForm def week_list(request): weeks = Week.objects.all().order_by('id') return render(request,'vulnerability/week_list.html', {'weeks': weeks}) def week_edit(request, week_id=None): if week_id: week = get_object_or_404(Week, pk=week_id) else: week = Week() if request.method == 'POST': form = WeekForm(request.POST, instance=week) if form.is_valid(): week = form.save(commit=False) week.save() return redirect('vulnerability:week_list') else: form = WeekForm(instance=week) return render(request, 'vulnerability/week_edit.html', dict(form=form, week_id=week_id)) def week_del(request, week_id): week = get_object_or_404(Week, pk=week_id) week.delete() return redirect('vulnerability:week_list')
(vulnerability/urls.py) from django.urls import path from vulnerability import views app_name = 'vulnerability' urlpatterns = [ path('week/', views.week_list, name='week_list'), path('week/add/', views.week_edit, name='week_add'), path('week/mod/<int:week_id>/', views.week_edit, name='week_mod'), path('week/del/<int:week_id>/', views.week_del, name='week_del'), ]
$ tree . ├── cms │   ├── __init__.py │   ├── __pycache__ │   │   ├── __init__.cpython-36.pyc │   │   ├── admin.cpython-36.pyc │   │   ├── forms.cpython-36.pyc │   │   ├── models.cpython-36.pyc │   │   ├── urls.cpython-36.pyc │   │   └── views.cpython-36.pyc │   ├── admin.py │   ├── apps.py │   ├── forms.py │   ├── migrations │   │   ├── 0001_initial.py │   │   ├── __init__.py │   │   └── __pycache__ │   │   ├── 0001_initial.cpython-36.pyc │   │   └── __init__.cpython-36.pyc │   ├── models.py │   ├── static │   │   ├── cms │   │   │   ├── css │   │   │   │   ├── bootstrap-grid.css │   │   │   │   ├── bootstrap-grid.css.map │   │   │   │   ├── bootstrap-grid.min.css │   │   │   │   ├── bootstrap-grid.min.css.map │   │   │   │   ├── bootstrap-reboot.css │   │   │   │   ├── bootstrap-reboot.css.map │   │   │   │   ├── bootstrap-reboot.min.css │   │   │   │   ├── bootstrap-reboot.min.css.map │   │   │   │   ├── bootstrap.css │   │   │   │   ├── bootstrap.css.map │   │   │   │   ├── bootstrap.min.css │   │   │   │   └── bootstrap.min.css.map │   │   │   └── js │   │   │   ├── bootstrap.bundle.js │   │   │   ├── bootstrap.bundle.js.map │   │   │   ├── bootstrap.bundle.min.js │   │   │   ├── bootstrap.bundle.min.js.map │   │   │   ├── bootstrap.js │   │   │   ├── bootstrap.js.map │   │   │   ├── bootstrap.min.js │   │   │   └── bootstrap.min.js.map │   │   └── vulnerability │   │   ├── css │   │   │   ├── bootstrap-grid.css │   │   │   ├── bootstrap-grid.css.map │   │   │   ├── bootstrap-grid.min.css │   │   │   ├── bootstrap-grid.min.css.map │   │   │   ├── bootstrap-reboot.css │   │   │   ├── bootstrap-reboot.css.map │   │   │   ├── bootstrap-reboot.min.css │   │   │   ├── bootstrap-reboot.min.css.map │   │   │   ├── bootstrap.css │   │   │   ├── bootstrap.css.map │   │   │   ├── bootstrap.min.css │   │   │   └── bootstrap.min.css.map │   │   └── js │   │   ├── bootstrap.bundle.js │   │   ├── bootstrap.bundle.js.map │   │   ├── bootstrap.bundle.min.js │   │   ├── bootstrap.bundle.min.js.map │   │   ├── bootstrap.js │   │   ├── bootstrap.js.map │   │   ├── bootstrap.min.js │   │   └── bootstrap.min.js.map │   ├── templates │   │   ├── cms │   │   │   ├── base.html │   │   │   ├── book_edit.html │   │   │   ├── book_list.html │   │   │   ├── impression_edit.html │   │   │   └── impression_list.html │   │   └── vulnerability │   │   ├── base.html │   │   ├── week_edit.html │   │   └── week_list.html │   ├── tests.py │   ├── urls.py │   └── views.py ├── db.sqlite3 ├── manage.py ├── mybook │   ├── __init__.py │   ├── __pycache__ │   │   ├── __init__.cpython-36.pyc │   │   ├── settings.cpython-36.pyc │   │   ├── urls.cpython-36.pyc │   │   └── wsgi.cpython-36.pyc │   ├── settings.py │   ├── urls.py │   └── wsgi.py └── vulnerability ├── __init__.py ├── __pycache__ │   ├── __init__.cpython-36.pyc │   ├── admin.cpython-36.pyc │   ├── forms.cpython-36.pyc │   ├── models.cpython-36.pyc │   ├── urls.cpython-36.pyc │   └── views.cpython-36.pyc ├── admin.py ├── apps.py ├── forms.py ├── migrations │   ├── 0001_initial.py │   ├── __init__.py │   └── __pycache__ │   ├── 0001_initial.cpython-36.pyc │   └── __init__.cpython-36.pyc ├── models.py ├── tests.py ├── urls.py └── views.py 20 directories, 94 files $
(week_list.html) {% extends "vulnerability/base.html" %} {% block title %}Week Vulnerability List{% endblock title %} {% block content %} <h4 class="mt-4 border-bottom">Week Vulnerability List</h4> <a href="{% url 'vulnerability:week_add' %}" class="btn btn-primary btn-sm my-3">追加</a> <table class="table table-striped table-bordered"> <thead> <tr> <th scope="col">ID</th> <th scope="col">CVE ID</th> <th scope="col">Base Score</th> <th scope="col">Attack Vector</th> <th scope="col">CWE Type</th> <th scope="col">Description</th> <th scope="col">Published Date</th> <th scope="col">Last Modified Date</th> <th scope="col">Vendor Name</th> <th scope="col">Product Name</th> <th scope="col">Affected Version</th> <th scope="col">Action</th> </tr> </thead> <tbody> {% for week in weeks %} <tr> <th scope="row">{{ week.id }}</th> <td>{{ week.cve_id }}</td> <td>{{ week.base_score }}</td> <td>{{ week.attack_vector }}</td> <td>{{ week.cwe_type }}</td> <td>{{ week.description }}</td> <td>{{ week.published_date }}</td> <td>{{ week.last_modified_date}}</td> <td>{{ week.vendor_name }}</td> <td>{{ week.product_name }}</td> <td>{{ week.affected_version }}</td> <td> <a href="{% url 'vulnerability:week_mod' week_id=week.id %}" class="btn btn-outline-primary btn-sm">Edit</a> #ここのリンクをクリックするとリダイレクトエラーが発生します <a href="{% url 'vulnerability:week_del' week_id=week.id %}" class="btn btn-outline-danger btn-sm">Delete</a> </td> </tr> {% endfor %} </tbody> </table> {% endblock content %}

試したこと

7行目がエラーとなっているので、リンクをcmsにしたり、vulnerabilityに変えたりしましたが、エラーの表示内容に変更はありませんでした。

補足情報(FW/ツールのバージョンなど)

Python3.6.0
Django2.1.3

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

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

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

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

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

guest

回答1

0

ベストアンサー

リダイレクト先テンプレートのweek_edit.htmlにおいて、

<form action="{% url 'vulnerability:week_mod' week_id=week_id %}" method="post">

とすべきところを

<form action="{% url 'vulnerability:week_mod' week_id=week.id %}" method="post">

と誤って記載していたのが原因でした。問題の切り分けのために、別プロジェクトを作成し、vulnerabilityアプリケーションを作成致しましたが、同様の事象が発生したため、同一プロジェクト内複数アプリケーションが原因ではないことは明白でした。良い勉強になりました。ありがとうございました。

投稿2018/12/23 16:14

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問