83👍
Firstly, your URL definition does not accept any parameters at all. If you want parameters to be passed from the URL into the view, you need to define them in the urlconf.
Secondly, it’s not at all clear what you are expecting to happen to the cleaned_data dictionary. Don’t forget you can’t redirect to a POST – this is a limitation of HTTP, not Django – so your cleaned_data either needs to be a URL parameter (horrible) or, slightly better, a series of GET parameters – so the URL would be in the form:
/link/mybackend/?field1=value1&field2=value2&field3=value3
and so on. In this case, field1, field2 and field3 are not included in the URLconf definition – they are available in the view via request.GET
.
So your urlconf would be:
url(r'^link/(?P<backend>\w+?)/$', my_function)
and the view would look like:
def my_function(request, backend):
data = request.GET
and the reverse would be (after importing urllib
):
return "%s?%s" % (redirect('my_function', args=(backend,)),
urllib.urlencode(form.cleaned_data))
Edited after comment
The whole point of using redirect and reverse, as you have been doing, is that you go to the URL – it returns an Http code that causes the browser to redirect to the new URL, and call that.
If you simply want to call the view from within your code, just do it directly – no need to use reverse at all.
That said, if all you want to do is store the data, then just put it in the session:
request.session['temp_data'] = form.cleaned_data
98👍
urls.py:
#...
url(r'element/update/(?P<pk>\d+)/$', 'element.views.element_update', name='element_update'),
views.py:
from django.shortcuts import redirect
from .models import Element
def element_info(request):
# ...
element = Element.object.get(pk=1)
return redirect('element_update', pk=element.id)
def element_update(request, pk)
# ...
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-Django – how to visualize signals and save overrides?
- [Django]-How to add url parameters to Django template url tag?
13👍
I do like this in django3
redirect_url = reverse('my_function', args=(backend,))
parameters = urlencode(form.cleaned_data)
return redirect(f'{redirect_url}?{parameters}')
- [Django]-Access web server on VirtualBox/Vagrant machine from host browser?
- [Django]-How to make two django projects share the same database
- [Django]-How to use permission_required decorators on django class-based views
4👍
I am new to Django. One of my project, I used render instead of redirect to send data. That worked good. My code was like this —>
for key, value in request.POST.lists():
print(key, value)
if key.split('-')[-1] != 'csrfmiddlewaretoken':
qu_id = key.split('-')[-1]
get_answer = Answer.objects.filter(question_id=qu_id,
correct_answer__option__contains=value[0])
total_correct_answer = get_answer.count()
context = {'score': total_correct_answer}
return render(request, 'result.html', context)
context = {'questions': questions, 'total_correct_answer': total_correct_answer}
return render(request, 'test.html', context)
- [Django]-Timestamp fields in django
- [Django]-Django, Models & Forms: replace "This field is required" message
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?