0

저는 장고를 처음 사용하고 사용자 입력 텍스트를 표시하려고합니다. 여러 가지 시도했지만 지금까지 아무것도 작동하지 않았습니다. 조언/도움 필요!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

양식 작업이 잘못되었습니다. URL 파일을 추가하십시오. –

+0

오, 예. url.py가 추가되었습니다. – sainidad

답변

0

이 URL을 생성합니다 {% url 'result' %}에 양식 동작이 때문에이

<form action="{% url 'result' %}" ....> 
.... 
.... 
</form> 

처럼 될 것입니다 편집, 어디 URL을 하드 코딩하고 너무 잘못 우리 돈이었다 같은 URL에서 두 개의 템플릿없이이 작업을 수행 할 수있는 방법으로 .html을 추가하십시오.)

관련 문제