簡體   English   中英

無法在Django中顯示用戶輸入

[英]Unable to display user input in django

我是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'),
 ]

將您的表單操作編輯為{% url 'result' %}因此會像這樣

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

這將生成URL,您在其中對URL進行了硬編碼,這也是錯誤的,我們也沒有在其中添加.html ,順便說一句,您可以在沒有兩個兩個模板的情況下(在相同的URL中)進行此操作:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM