1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model 対処法

Posted at

Djangoでsuccess_urlを設定しようとしたのですが、なかなかに錯綜してしまったのでメモ。
やるべきことはかなりシンプルなのに中々答えに辿り着けず時間がかかってしまいました。

エラー内容

DjangoでCreateReviewを使ったデータ登録後に遷移させたいページを設定してる際、やたら以下エラーが出てしまい困りました。

(エラー文)
No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model

結果的にはただ、success_urlの書き方という話になってしまいますが、メモ。

対処法(success_urlの書き方)

####処理成功後に静的なページへ遷移したい場合

from django.urls import reverse_lazy

success_url = reverse_lazy("app名:urls.pyで設定したname")

####処理成功後urlから取得した変数を使用して動的ページへ遷移したい場合

from django.urls import reverse_lazy

class StoreReview(CreateView):
    template_name = "app名/example.html"
    model = Model
    fields = (
        "field",
    )
def get_success_url(self):
    return reverse_lazy(
        "app名:urls.pyで設定したname", kwargs={"pk": self.kwargs["pk"]}
        )

ぐぐってたらmodels.pyをいじるとか結構色々出てきたのですが
これだけ抑えておけばエラーは解消され、無事テーブルにデータ登録後の画面遷移ができました。嬉

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?