社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

小白问题-views传过去的表单和数据在页面看不到

萝卜斯基 • 10 年前 • 5384 次点击  

大家好,我再views.py里的表单及数据不能在html页面显示,具体代码如下:

def message(request):

    messages=Msg.objects.all()
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
            user = form.cleaned_data['user']
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            m = Msg(user=user,
                        title=title,
                        content=content,
                        )
            m.save()
            return HttpResponseRedirect('registration/index.html')
    else:
        form = MessageForm()
        return render_to_response('registration/index.html',{'form':form, 'messages':messages},context_instance=RequestContext(request))

html如下:

<html>
<head><title> Leave your message here </title></head>
<body>
<h1>留言</h1>
<table>

  {% for message in messages %}
      <br/>
   <tr>
   <strong> Time:</strong>{{ message.datetime|date:'Y-m-d H:i:s'}}
   </tr>
      <br/>
   <tr>
    <strong>From:</strong>{{ message.user }}
   </tr>
      <br/>
  <tr>
   <strong> Title:</strong>{{ message.title }}
  </tr>
      <br/>
  <tr>
   <strong> Content:</strong>{{ message.content }}
  </tr>
      <br/>
  <hr>
  {% endfor %}
</table>
<h2>Leave your message</h2>
<form action="." method="POST">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <p><input type="submit" value="提交"></p>
</form>
</body>
</html>

请各位大神看看问题在哪里,感恩!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/479
 
5384 次点击  
文章 [ 5 ]  |  最新文章 10 年前