带参数的 Django 返回 redirect()

在 view 函数中,我想调用另一个视图并将数据传递给它:

return redirect('some-view-name', backend, form.cleaned_data)

其中,后端是 registration.backend 对象,form.clean _ data 是表单数据的结果(但是两者都必须作为 * args 或 * * kwargs 发送,以避免产生 Don't mix *args and **kwargs in call to reverse()!错误)。根据我在文件中的发现:

def my_view(request):
...
return redirect('some-view-name', foo='bar')

看起来我需要提供“ some-view-name”参数,但它仅仅是 view 函数的名称,还是 url 的名称?因此,我想让它类似于在 django 注册中的方式,其中:

to, args, kwargs = backend.post_registration_redirect(request, new_user)
return redirect(to, *args, **kwargs)


def post_registration_redirect(self, request, user):
return ('registration_complete', (), {})

现在,我可以直接调用 view 函数,还是需要为它提供一个 URL?更重要的是,我的函数调用(如果需要,还有一个 url)应该是什么样子?后端和 clean _ data 都只是通过这个视图传递,以供以后使用。我试过了,但不合适:

url(r'^link/$', some-view-name)
def some-view-name(request, *args):

还有这个:

return redirect('some_url', backend=backend, dataform.cleaned_data)
url(r'^link/$', some-view-name)
def some-view-name(request, backend, data):

但是在姜戈注册中,我见过这样的情况:

url(r'^register/$',register,{'backend': 'registration.backends.default.DefaultBackend'}, name='registration_register'),


def register(request, backend, success_url=None, form_class=None,
disallowed_url='registration_disallowed',
template_name='user/login_logout_register/registration_form.html',
extra_context=None):
226081 次浏览

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

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)
# ...

I do like this in django3

redirect_url = reverse('my_function', args=(backend,))
parameters = urlencode(form.cleaned_data)
return redirect(f'{redirect_url}?{parameters}')

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)