简体   繁体   中英

How to send large amount of text between two views using Django

I need a recommendation to pass large amount of text between two views with Django. The code below is what I thought, but first is not working and second I don't know if its better using cookies.

<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>

then:

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})

and in the urls.py

url(r'^new_view/', new_view, {'post':'baam'}, name='new_view'),

This is raising this exception:

Exception Type: NoReverseMatch
Exception Value: Reverse for 'new_view' with arguments '()' and keyword arguments '{'post': u''}' not found. 1 pattern(s) tried: ['new_view/'] 

As per your url definition, the new_view url does not take any parameter hence the error that you are getting.

But passing data in url, specifically large data as you mentioned, is not good. Cookies or django sessions are much better option.

To use django session update your view as

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM