我需要建议使用Django在两个视图之间传递大量文本。 下面的代码是我的想法,但首先是不工作,其次我不知道它是否更好地使用cookie。
<form method="POST"> {% csrf_token %}
{{ form.post }} <!-- This is a textarea, the user can write as much as he wants -->
<input type="submit" value="Prepare to send">
</form>
然后:
def sender(request):
if form.is_valid():
cd = form.cleaned_data
letter = cd['post']
next = reverse('new_view', kwargs={'post':post})
return HttpResponseRedirect(next)
def new_view(request, post=''):
return render(request, 'new_view.html', {'post': post})
和urls.py
url(r'^new_view/', new_view, {'post':'baam'}, name='new_view'),
这引发了这个例外:
Exception Type: NoReverseMatch
Exception Value: Reverse for 'new_view' with arguments '()' and keyword arguments '{'post': u''}' not found. 1 pattern(s) tried: ['new_view/']
答案 0 :(得分:1)
根据您的网址定义,new_view
网址不会带任何参数,因此会出现错误。
但是在url中传递数据,特别是你提到的大数据,并不好。 Cookies或Django会话是更好的选择。
要使用django会话,请将视图更新为
def sender(request):
if form.is_valid():
cd = form.cleaned_data
letter = cd['post']
request.session['post_data'] = letter
next = reverse('new_view',)
return HttpResponseRedirect(next)
def new_view(request,):
post_data = request.session.get('post_data')
return render(request, 'new_view.html', {'post': post})