javascript - 无法在我的模板中获取发布对象模型

标签 javascript django json response alert

我有一个带有姓名、年龄、电子邮件字段的学生模型。我为此创建了一个 StudentForm 表单,并创建了一个这样的 View

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>

现在我想在我的学生 View 中创建一个 json 响应,其中包含学生对象的所有值,并在发布时将其呈现到我的 index.html 中,这样我就可以生成一个警报,例如 ---> “Aditya SIngh,您已成功提交数据”。 Aditya SIngh 是学生的名字 我是 django.thanx 的新手,为了你的宝贵回应而提前

最佳答案

因此,您希望在成功保存后查看已保存的学生数据......为此您不需要 javascript/json。

在您的代码中,保存信息后,您将用户重定向到“索引” View 。相反,您可能希望重定向到“成功!”显示信息的页面:

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

所以现有 View 可能看起来像:

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

然后您将为成功页面添加一个 View (也是相应的模板和 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 事件。

如果你想要警告对话框索引页,那就有问题了。 View 只能返回一个东西,而你已经在返回索引页了。您将不得不以某种方式告诉索引页面要获取有关哪个学生的信息,但索引页面并不是真正为此设计的(假设您使用的是 Django 教程中的“索引”页面,即没有形式的模型列表) .

很多网站所做的是,如果他们成功创建了一个帐户,就会将新创建的用户放在他们的个人资料页面上。通过这种方式,他们可以确认已成功登录,并准备好做一些有用的事情,而不是查看“成功”页面。

或者,他们将此人放在站点的主页上,但此人的登录名却在导航栏中。这假设他们已经登录并注册。

关于javascript - 无法在我的模板中获取发布对象模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17102706/

相关文章:

Javascript 两次更改类

python - 使用 PyCharm 在 Docker 中使用 Django

java - 强制 jackson 在没有 JsonIgnore 的情况下忽略 isEmpty

arrays - 如何使用路径中文件中的数组序列化 JSON 数组?

javascript - .find ('.foo' ).text() 作为数组

javascript - Angular ngrx 属性 'payload' 在类型 'Action' 上不存在

javascript - 在javascript中输出所有表单变量(类似于php的print_r())

python - 在 Django 中处理动态生成表单的更好方法

python - 如何在 Django View 中访问 ModelForm 的字段

arrays - 在Go中,如何解码包含结构数组的字符串