基本上我是django世界的新手,我想在这里做点什么 我做了views.py
def user(request):
original = request.POST.get('originaltext', None)
return render (request, 'posts/home.html', {'original' : original or ''})
和我的urls.py
from django.conf.urls import url
from django.contrib import admin
from posts import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', views.user, name="home")
和帖子/ home.html:
<!DOCTYPE html>
<html>
<head>
<title>Django Project</title>
</head>
<body>
<h1>User Input</h1>
<form action="{% url 'home' %}" method="POST">
<input type="text" name="originaltext" />
{% csrf_token %}
<br />
<input type="submit" value="Try" />
<br />
<br />
<br />
<h3>Original</h3>
{{ original }}
我想要的是当用户在输入上写东西时,信息保留在同一个html页面上,当他输入其他东西时,它会附加到列表中 有什么建议吗?
答案 0 :(得分:0)
尝试添加:
if original.is_valid():
cd = original.cleaned_data
return render (request, 'posts/home.html', {'original' : cd or ''})