2

私は名前、年齢、電子メールのフィールドを持つ学生モデルを持っています.私はこのためにStudentFormというフォームを作成し、このようなビューを作成しました

def student(request):
form=Studentform(request.POST)
if request.method=='POST':

    if form.is_valid():
        stu=Student()
        stu.name=form.cleaned_data['name']
        stu.email=form.cleaned_data['email']
        stu.age=form.cleaned_data['age']
        stu.save()

        return HttpResponseRedirect('/index/')

else:
    form=Studentform()
return render_to_response('index.html',{'form':form},context_instance=RequestContext(request) )

そしてここに私のindex.html

<html>
   <head>
   <title></title>
    <script type="text/javascript">
    var student={{ stu_var }}
    alert("erer")
     </script>
  </head>
  <body>

   <form action="/index/" method="post">{% csrf_token %}
       {{form.as_p}}
        <input type="submit" id="submit" name="submit" onclick="alert(student)">
      </form>
     </body>
</html>

ここで、学生ビューで json 応答を作成して、学生オブジェクトのすべての値を保持し、投稿時にそれを index.html にレンダリングして、次のようなアラートを生成できるようにします--->「Aditya SIngh、Youデータの送信に成功しました。」ここで、Aditya Singh は django.thanx の新しい生徒の名前になります。

4

1 に答える 1

1

それで、正常に保存された後に保存された学生データを表示したい...そのためにjavascript/jsonは必要ありません。

コードでは、情報を保存した後、ユーザーを「インデックス」ビューにリダイレクトします。代わりに、「成功!」にリダイレクトすることをお勧めします。情報を表示するページ:

HttpResponseRedirect('/success/%d/' % stu.id)

したがって、既存のビューは次のようになります。

def student(request):

    form=Studentform(request.POST)

    if request.method=='POST':

        if form.is_valid():

            stu=Student()
            stu.name=form.cleaned_data['name']
            stu.email=form.cleaned_data['email']
            stu.age=form.cleaned_data['age']
            stu.save()

            return HttpResponseRedirect('/success/%d/' % stu.id)
        else:
            pass
            # return the half-completed form with the old data so the
            # user can make corrections
            # this "else" is not required, I just put it in here
            # to have a place to put this comment
            # and to show that this is another path that might be taken
            # through the code.

    else:
        # empty form for the user to fill out
        form=Studentform()

    return render_to_response('index.html',
        {'form':form},
        context_instance = RequestContext(request) )

そして、成功ページのビュー (対応するテンプレートと URL エントリも) を追加します。

def success (request, id=None):

    stu = Student.objects.get (id = id)

    return render_to_response ('success.html',
        {'stu', stu},
        context_instance = RequestContext(request) )

本当に「アラート」ダイアログ ボックスが必要な場合は、そのための onLoad イベントを作成できます。

アラート ダイアログ ボックスインデックス ページが必要な場合は、問題があります。ビューは 1 つしか返すことができず、既にインデックス ページを返しています。どういうわけかインデックス ページにどの学生について情報を取得するかを伝える必要がありますが、インデックス ページは実際にはそのようには設計されていません (Django チュートリアルのようにフォームのないモデルのリストである「インデックス」ページを使用していると仮定します)。 .

多くの Web サイトでは、アカウントの作成に成功すると、新しく作成されたユーザーをプロファイル ページに表示します。そうすれば、ログインに成功したという確認が得られ、「成功」ページを見る代わりに何か役に立つことをする準備が整います。

または、その人物をサイトのメイン ページに配置しますが、その人物のログイン名はナビゲーション バーに表示されます。これは、ユーザーがログインして登録していることを前提としています。

于 2013-06-19T00:54:56.823 に答える