我是Django的新手并尝试显示用户输入文本。我尝试了很多东西但到目前为止还没有任何工作。需要建议/帮助! 这是我的文件:
models.py
from django.db import models
class Post(models.Model):
message = models.TextField(max_length=4000)
def __unicode__(self):
return self.title
views.py
from .models import Post
from django.core.exceptions import *
def index(request):
return render('index.html')
def result(request):
p = request.POST['message']
return render_to_response('result.html', {'message': p}, context_instance=RequestContext(request))
的index.html
<!DOCTYPE html>
<head>
<title>Index page</title>
</head>
<body>
<div id="header">Welcome to index page</div>
<div id="content">
<p>Enter your name</p>
<form action="/polls/results.html/" method="post" accept-charset="utf-8">{% csrf_token %}
<input type="text" name="message">
<input type="submit" value="Send!">
</form>
</div>
</body>
results.html
<!DOCTYPE html>
<head>
<title>Result page</title>
</head>
<body>
<div id="header">Here is the result</div>
<div id="content">
<p>Your name is: {{ message }}</p>
</div>
</body>
url.py
from django.conf.urls import include, url
from django.conf.urls.static import static
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.result, name='results'),
]
答案 0 :(得分:0)
将您的表单操作修改为{% url 'result' %}
,以便它就像这样
<form action="{% url 'result' %}" ....>
....
....
</form>
这会生成网址,你在那里硬编码网址也是错误的我们也不会添加.html
,顺便说一句,你可以在没有两个模板的情况下做到这一点(在同一个网址中) ):)