python、djangoで掲示板を作ってみたい①の続きです!!!! さて送信フォームで投稿を送信するとこのようなエラーが出ます。
No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.
それはそうですね。送信した後どのページに飛ぶのか?リダイレクト先を指定していませんから。
リダイレクト先の設定
まずはviews.pyに新しく
from django.urls import reverse_lazy
を追加。reverse_lazyをimportします。
そしてclassを改造します。
class PostandBoard(generic.CreateView): model = Post form_class = PostForm template_name = 'board/board.html' success_url = reverse_lazy('board:index')
success_url = reverse_lazy('app_name:urlsで指定したname')
で リダイレクト先を指定することができます。 今回はpathでいうと
path('', views.IndexView.as_view(), name='index'),
ここを指定します。
post_list.htmlを改造
{% for post in post_list %} <p>{{ post.name }}</p> <p>{{ post.text | linebreaksbr }}</p> <p>{{ post.date }}</p> {% endfor %}
タグで列を分けて、linebreaksbr }}で送信フォームで書き込んだ際の改行を投稿一覧に反映できるようにします。
cssとかはまだ作ってないのですがとりあえずこんな感じで投稿ができるようになりました。
投稿フォームと投稿結果を1つのページで表示する
最初に参考にしたのがこの記事。
mixins modelform 使い方 - Django - ListViewとCreateViewを混在させる - CODE Q&A 問題解決
この通りコードをコピペ。
class FormAndListView(BaseCreateView, BaseListView, TemplateResponseMixin): def get(self, request, args, **kwargs): formView = BaseCreateView.get(self, request, args, kwargs) listView = BaseListView.get(self, request, *args, kwargs) formData = formView.context_data['form'] listData = listView.context_data['object_list'] return render_to_response('textfrompdf/index.html', {'form' : formData, 'all_PDF' : listData}, context_instance=RequestContext(request))
ただこのままだと
class FormAndListView(PostandBoard, IndexView, TemplateResponseMixin): NameError: name 'TemplateResponseMixin' is not defined
TemplateResponseMixinでフォームとリストを同時表示させられると思うんだけど・・・。 たぶんfrom ~~ importするんだろうな。と思っていろいろググってみたのだが見つからず・・・。 とりあえずTemplateResponseMixinをコメントアウトして進めていくことにする。
まずBaseCreateView, BaseListView,。 こいつらを僕の作っているアプリケーションのclass名に差し替える。
class FormAndListView(PostandBoard, IndexView,):#TemplateResponseMixin
formView = PostandBoard.get(self, request, args, **kwargs) listView = IndexView.get(self, request, args, **kwargs)
すると
name 'rendertoresponse' is not defined
name 'RequestContext' is not defined
なエラーが出た。なので随時importしていく。・・・がしかし、
render_to_response() got an unexpected keyword argument 'context_instance'
おかしい・・・。context_instance自体が受け付けていない・・・。 ちょっとぐぐって興味深い記事を発見。
Django 1.10にアップグレードした後、エラーが出ますrender_to_response() got an unexpected keyword argument 'context_instance'。
context_instanceパラメータでは、render_to_responseたジャンゴ1.8で非推奨、とDjango 1.10で除去しました。
解決策はrenderショートカットに切り替えることRequestContextです。ショートカットは自動的にを使用します。
インポートを更新して、次のように表示します。最初の引数としてオブジェクトをrender取りrequestます。
from django.shortcuts import render
def my_view(request): context = {'foo': 'bar'} return render(request, 'my_template.html', context)
googleの和訳機能で日本語訳するとどうやら昔の書き方だったらしくdjangoのバージョンが上がって書き方が変わったらしい・
from django.shortcuts import renderdef my_view(request): context = {'foo': 'bar'} return render(request, 'my_template.html', context)
これいつもの書き方やん!!!
ということで正解はこう!!
class FormAndListView(PostandBoard, IndexView,): def get(self, request, args, **kwargs): formView = PostandBoard.get(self, request, args, kwargs) listView = IndexView.get(self, request, *args, kwargs) formData = formView.context_data['form'] listData = listView.context_data['object_list'] context = {'form' : formData, 'post_list' : listData} return render(request, 'board/test.html', context)
すると・・・。
<span style="font-size: 200%"> あれっ?表示された!!</span>
TemplateResponseMixinいらなかったじゃん。 結構簡単にできるんだな・・・。
というわけで投稿したテキストと投稿フォームが同じページに出現しました。 とりあえずclassをもっとわかりやすくリネームして・・・。 そのあとは画像のアップロード機能とか作れるかな???時間かかりそうだけどやってみたいなあ。
[http://sr2460.hatenablog.com/entry/2019/02/13/113123?_ga=2.148095090.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/13/190340?_ga=2.147647986.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/14/145918?_ga=2.147647986.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/14/213617?_ga=2.147647986.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/15/191257?_ga=2.147647986.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/16/101603?_ga=2.190021998.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/17/094847?_ga=2.190021998.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/17/194641?_ga=2.186306028.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/18/103609?_ga=2.186306028.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/18/194933?_ga=2.186306028.490112225.1550722323-1409412485.1430535027:title]
[http://sr2460.hatenablog.com/entry/2019/02/20/193000?_ga=2.186306028.490112225.1550722323-1409412485.1430535027:title]