【问题标题】:unable to get post object models in my template无法在我的模板中获取帖子对象模型
【发布时间】:2013-06-10 18:16:48
【问题描述】:

我有一个包含姓名、年龄、电子邮件字段的学生模型。我为此创建了一个 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,您已成功提交数据”。 Aditya Singh 是学生的名字 我是 django.thanx 的新手,因为您的宝贵回应

【问题讨论】:

  • 你的问题不清楚。无论如何,我认为您提交的表格本身包含学生的姓名作为值。只需使用 Javascript 获取该值并生成所需的警报。
  • HttpResponseRedirect 发出 GET 请愿,您可以将数据发送到您的索引,您可以做的是下面的答案说“制作另一个视图”或制作一个 ajax POST 到另一个视图并显示一个花式框与您相同的页面。

标签: javascript django json response alert


【解决方案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 事件。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2014-12-04
    • 2015-03-24
    相关资源
    最近更新 更多