Django noob在這裏,不太瞭解這些概念,所以無法從類似StackOverflow的問題中獲得任何幫助。最佳實踐/幫助在表單提交後在Django中重定向
我有一個只有一個下拉的表單,如果按下提交,下拉的值應該被轉發到下一頁。以便內容根據下拉菜單進行自定義。
如何重定向後,後到下一個頁面的參數?是它甚至正確使用 「HttpResponseRedirect(反向」?
views.py
def appStart(request, institution):
#so something with institution
return render(request, 'application/SectionStart.html', {'content':{'if you would like to contact me emial','[email protected]'}})
def ReviewMyView(request):
form_class = ApplicationSelectInstituation
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
for field, value in form.cleaned_data.items():
return HttpResponseRedirect(reverse('appStart', args=(value,)))
#return HttpResponseRedirect(reverse('view_blog', args=(), kwargs={'institution': value}))
#return HttpResponseRedirect(reverse('appStart'), {'institution': value})
#return HttpResponseRedirect(reverse('appStart'), institution=value)
return render(request, 'application/appmyreview.html', {
'form': form_class
})
urls.py
urlpatterns = [
url(r'^xxx/(?P<institution>\d+)$', appStart, name='appStart'),
url(r'^myapp$', ReviewMyView, name='review'),
]
更新
謝謝你的答案,認爲我需要電文讀出的Django的基礎知識,因爲我沒有得到它。
def my_awesome_django_view(request):
form_class = ApplicationSelectInstituation
if request.method == "POST":
form = ApplicationSelectInstituation(data=request.POST)
if form.is_valid():
content_data = form.cleaned_data.get('institution_name').id
#Try 1 - page just refreshes
#redirect("nextapppage/"+str(content_data))
#Try 2 - Reverse for 'next_view' with arguments '()' and keyword arguments '{'institution_id': 3}' not found. 0 pattern(s) tried: []
#return HttpResponseRedirect(reverse('next_view',kwargs={'institution_id': content_data}))
#Try 3 - Reverse for 'next_view' with arguments '(3,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
return HttpResponseRedirect(reverse('next_view', args=[content_data]))
return render(request, 'application/appmyreview.html', {
'form': form_class
})
def the_next_view(request, *args, **kwargs):
print("I got to: the_next_view")
return render(request, 'application/SectionStart.html', {'content':{'if you would like to contact me emial','[email protected]'}})
urls.py
urlpatterns = [
url(r'^myapp$', my_awesome_django_view, name='myapp'),
url(r'^nextapppage/(?P<institution_id>\d+)/$', the_next_view, name='the_next_view'),
'return HttpResponseRedirect(reverse('foo'))'是我怎麼做的。 –