django2週間目です。非常に初心な質問となり申し訳ありませんがよろしくおねがいいたします。
#やりたいこと hoge1.htmlからhoge2.htmlにページ遷移する際に変数targetfileの値を引継ぎたいです。
一言でいうと、targetfileの変数をtemplateのHTMLに表示させたいです。
targetfileはother_functionという関数で設定されています。
hoge_functionoがother_functionを呼んでいます。
それでは説明させて頂きます。まずhoge1.htmlからhoge2.htmlへページ遷移は正常に遷移する事は確認しています。
hoge2.htmlにtargetfileという変数を表示させたいです。
#ためしたこと
フォルダ名 hoge_folder
pyファイル名 hoge_pyfile
という中にhoge_functionという関数を作成しました。
def hoge_function(hoge1,targetfile): targetfile = other_function() return targetfile
さらにother.pyというother_function()を呼び出し最終的にother_function()でtargetfileが設定されます。
def other_function(): targetfile = "ターゲットファイルの内容です。この内容をhoge2.htmlに表示させたくて質問しました" return targetfile
hoge_functionはviews.pyから以下で呼ばれています。(▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲の部分です)
また変数を渡す時はhoge2.htmlに移動するタイミングです。(★★★★★★★★★★の部分です)
ここにtargetfileを指定すればいいのかもしれませんが上記のように
hoge_pyfile.py→views→hoge2.html
と遷移させていく変数指定方法がわかりません。
実際のページ遷移先の表示は以下になります。templateここは問題ないかと思います。
hoge2.html
@login_required def hoge2(request): context = { 'fieldname': targetfile, } return render(request, 'app/hoge2.html', context)
問題のviewsです。▲と★中心にどのように設定したらtargetfileという変数がviewに帰ってきてhoge2.htmlへ引き継がれるのでしょうか?
views.py
class CreateHoge(LoginRequiredMixin, generic.CreateView): model = Hoge form_class = HogeCreateForm success_url = reverse_lazy('app:hoge1') def form_valid(self, form): hoge1 = form.save() from .hoge_folder import hoge_pyfile targetfile = hoge_pyfile.hoge_function(hoge1)▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ messages.info(self.request, 'that is just a message') #ここでhoge2.htmlへ行く時に変数targetfileの中身を格納したい。★★★★★★★★★★の部分です return redirect('app:hoge2',targetfile)
ものすごい稚拙な質問となり大変申し訳ありませんがよろしくお願いします。
あなたの回答
tips
プレビュー