2016-11-03 41 views
1

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'), 
+0

'return HttpResponseRedirect(reverse('foo'))'是我怎麼做的。 –

回答

0

你或許應該在演唱會使用HttpResponseRedirect具有反向。

您可以提供參數以與args參數相反。所以,在你看來:

def my_awesome_django_view(request): 
    # some get method stuff 
    if request.method == "POST": 
     form = MyForm(data=request.POST) 
     if form.is_valid(): 
      content_data = form.cleaned_data.get('my_field') 
      return HttpResponseRedirect(reverse('next_view', args=[content_data])) 

在接下來的觀點:

def the_next_view(request, *args, **kwargs): 
    content = content_getter(*args) 
    return HttpResponse(content) 

你只是不會使用命名參數的正則表達式,但我會用一個命名參數爲清楚和顯這樣的URL配置2個月內,當你試圖弄清楚爲什麼你寫了這個,你就可以給自己一些麪包屑(瞭解到硬盤的方式太多次)。

隨着kwargs你只是通過它在像這樣:

reverse('my_view_name', kwargs={'my_kwarg': value}) 

只是爲了完整性:

urlpatterns = [ 
    url(r'^xxx/(?P<institution>[\d]+)$', the_next_view, name='appStart'), 
    url(r'^myapp$', my_awesome_django_view, name='review'), 
] 

另外請注意,我已經改變了URLconf中的正則表達式。

0

您是否考慮過使用class-based views?它們使用起來非常簡單,大部分基本功能都已經爲您準備好,並且包含直觀的名稱和出色的文檔。

from django.views.generic.edit import FormView 

class MyFormView(FormView): 
    template_name = 'my_template.html' 
    form_class = MyForm 

    def form_valid(self, form): 
     # on successful form submit 
     self.next_url = reverse('appStart', args=[form.some_value]) 
     return super().form_valid(form) 

    def get_success_url(self): 
     return self.next_url 

urls.py

url(r'^myapp$', MyFormView.as_view(), name='review'), 
0

如果您需要,以導致另一種觀點則認爲將提供給瀏覽器的響應,爲什麼不使用快捷redirect

from django.shortcuts import redirect 

def ReviewMyView(requst): 
    if request.method == 'POST': 
     # do stuff... 
     redrect(appStart, form.cleaned_data[?]) 

您可以傳遞下一個視圖所需的任何參數。