2017-09-12 9 views
1

これは、私のdjango app.iのviews.pyのスニペットで、modelformを使用してフォームを作成しています。 models.pyには、attibuteユーザ名を持つクラスユーザがいます。私は登録ページをビュー 'リダイレクト'にリダイレクトしたいと思いますが、フォームの送信ボタンを押した後に取得したユーザーの名前を渡して、データが 'new_form'インスタンスに格納されるようにします。djangoでテンプレートのモデル属性にアクセスする

def registration(request): 
    if request.method=='POST': 
     form=userForm(request.POST) 
     if form.is_valid(): 
      name=form.cleaned_data['username'] 
      new_form=form.save() 

      return HttpResponseRedirect(reverse('redirect')) 
    else: 
     form=userForm() 
    return render(request,'student/registration.html',{'form':form}) 

my redirect.htmlは次のようになります。

<h1>You have signed up.</h1> 
<p>hello {{field.name}}</p> 

が、問題は名前は​​私が逆の(「リダイレクト」)を使って何を合格していないので、明白であるハロー後に表示されていないことです。だから私はそれをどうやってやるの? redirect.htmlにも問題はありますか?

答えて

1

コンテキスト辞書を作成して送信します。

def registration(request): 
    context ={} 
    if request.method=='POST': 
     form=userForm(request.POST) 
     if form.is_valid(): 
      name=form.cleaned_data['username'] 
      context['name']=name 
      new_form=form.save() 

      return render(request,'student/registration.html',context) 
    else: 
     form=userForm() 
     context['form']=form 
    return render(request,'student/registration.html',context) 

HTML

<h1>You have signed up.</h1> 
<p>hello {{name}}</p> 
+0

thanks ... this too too! – Shefali

+0

@Shefaliこのようにして、 'name'、' form'、 'location'など必要なフィールドをいくつでも送ることができます。** context ** dictionaryに追加して送るだけです。 –

+0

大丈夫...ありがとう! – Shefali

1

return HttpResponseRedirect(reverse('redirect'))の代わりに。

return render(request, 'redirect.html', {'name': name})を使用してください。

そして、このことができますあなたのredirect.html

<h1>You have signed up.</h1> 
<p>hello {{name}}</p> 

に希望を編集します。

+0

それが助けええ...それを行うには、他の方法がありthanks..is? – Shefali

+0

あなたは何を求めているのか説明していただけますか? – kartikmaji

+0

テンプレート内の{{field.name}}を使用する並べ替えのようなものがありますか?どこかで誤読されている可能性がありますか? – Shefali

関連する問題