Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将html输入发送到Django中的views.py_Python_Html_Django - Fatal编程技术网

Python 将html输入发送到Django中的views.py

Python 将html输入发送到Django中的views.py,python,html,django,Python,Html,Django,我正试图将index.html中的输入文本发送到我的视图函数“result”。 当我单击“生成摘要”按钮时,其显示csrf验证失败。csrf令牌丢失。需要紧急帮助 views.py from django.shortcuts import render from django.http import HttpResponse from django.template import loader from .models import Input from django.views.decorat

我正试图将index.html中的输入文本发送到我的视图函数“result”。 当我单击“生成摘要”按钮时,其显示csrf验证失败。csrf令牌丢失。需要紧急帮助

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Input
from django.views.decorators.csrf import csrf_protect

def home(request):
    input=Input.objects.all()
    template=loader.get_template('home/index.html')
    context={
        'input':input,
    }
    return HttpResponse(template.render(context,request))

def result(request,input_text):
    Input.input_text = request.POST('input_text')
    return HttpResponse("<h1> text is"+Input.input_text)
finalproject/url.py(根项目)

models.py

from django.db import models

class Input(models.Model):
    input_text = models.CharField(max_length=250)

    def __str__(self):
        return self.input_text
设置.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See      https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y)m04wnmm%i_#uih%^j5&aqeozlp!gt#px&z!*uf=-%v98x#-i'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition 

INSTALLED_APPS = [
'home',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
 ]

 MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'finalproject.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
 },
]

WSGI_APPLICATION = 'finalproject.wsgi.application'



DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}



AUTH_PASSWORD_VALIDATORS = [
{
    'NAME':     'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
    'NAME':  'django.contrib.auth.password_validation.CommonPasswordValidator',
 },
{
    'NAME':   'django.contrib.auth.password_validation.NumericPasswordValidator',
 },
]



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

出于某种原因,您正在以艰难的方式呈现模板,这绕过了Django为您所做的所有自动操作:最重要的是,运行包含CSRF令牌的上下文处理器

你的观点应该是:

def home(request):
    input=Input.objects.all()
    context={
        'input':input,
    }
    return render(request, 'home/index.html', context)

还要注意,在结果视图中设置
Input.Input_text
毫无意义;您需要创建输入实例,设置输入文本,然后保存。

显示settings.py文件。您使用的是哪个版本的django?用于呈现模板的许多代码看起来都很旧。。。没有任何东西在调用你的结果视图。我想结果是没有被调用。请建议一个remedy。我认为它需要从models.py输入文本。还有一件事我认为结果视图没有被调用…home函数再次被调用…你能提些建议吗?我不知道你的第一个评论是什么意思。第二,您似乎没有为结果视图定义URL。URL(r“^result/”,views.result,name='result')我将其添加到URL.py中,并将表单操作更改为“/result/”
from django.db import models

class Input(models.Model):
    input_text = models.CharField(max_length=250)

    def __str__(self):
        return self.input_text
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See      https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y)m04wnmm%i_#uih%^j5&aqeozlp!gt#px&z!*uf=-%v98x#-i'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition 

INSTALLED_APPS = [
'home',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
 ]

 MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'finalproject.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
 },
]

WSGI_APPLICATION = 'finalproject.wsgi.application'



DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}



AUTH_PASSWORD_VALIDATORS = [
{
    'NAME':     'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
    'NAME':  'django.contrib.auth.password_validation.CommonPasswordValidator',
 },
{
    'NAME':   'django.contrib.auth.password_validation.NumericPasswordValidator',
 },
]



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'
def home(request):
    input=Input.objects.all()
    context={
        'input':input,
    }
    return render(request, 'home/index.html', context)