Html “Django”;如果request.method==';邮政';:&引用;失败
嗨,我的新Django项目有一个奇怪的问题 我基本上是在测试来自的NameForm示例。这似乎很简单,但不知何故,当我试图提交一些名字时,什么都没有发生,甚至连重新加载页面都没有 form.pyHtml “Django”;如果request.method==';邮政';:&引用;失败,html,django,post,Html,Django,Post,嗨,我的新Django项目有一个奇怪的问题 我基本上是在测试来自的NameForm示例。这似乎很简单,但不知何故,当我试图提交一些名字时,什么都没有发生,甚至连重新加载页面都没有 form.py from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) from django.shortcuts import
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
from django.shortcuts import render
from django.http import HttpResponseRedirect
from groupbuilder import forms
def GroupBuilder(request):
if request.method == 'POST':
form = forms.NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = forms.NameForm()
return render(request, 'groupbuilder.html', {'form': form})
"""
Django settings for crossLFG project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.utils.translation import ugettext_lazy as _
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
#"frontpage.context_processors.contactform",
)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_PATH = '/home/KBrothers/crossLFG/'
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/'
DATABASE_PATH = os.path.join(PROJECT_PATH,'users.db')
#LOCALE_PATHS = os.path.join(PROJECT_PATH, 'locale',)
LOCALE_PATHS = ('/home/KBrothers/crossLFG/locale',)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ****
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
CRISPY_TEMPLATE_PACK = 'bootstrap3'
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'main_app',
'groupbuilder',
'crispy_forms',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
os.path.join(PROJECT_PATH, 'main_app/templates'),
os.path.join(PROJECT_PATH, 'accounts/templates'),
os.path.join(PROJECT_PATH, 'groupbuilder/templates'),
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'crossLFG.urls'
WSGI_APPLICATION = 'crossLFG.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
MAIL_USE_TLS = True
EMAIL_HOST = '****'
EMAIL_PORT = ****
EMAIL_HOST_USER = '********'
EMAIL_HOST_PASSWORD = '********'
#CRISPY_TEMPLATE_PACK = 'bootstrap3'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': DATABASE_PATH, # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en'
#LANGUAGE_CODE = 'de'
#LANGUAGE_CODE = 'fr'
LANGUAGE_COOKIE_NAME = 'wm_lang'
TIME_ZONE = 'UTC'
LANGUAGES = (
('en', _('English')),
#('de', _('German')),
#('fr', _('French')),
)
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
视图.py
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
from django.shortcuts import render
from django.http import HttpResponseRedirect
from groupbuilder import forms
def GroupBuilder(request):
if request.method == 'POST':
form = forms.NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = forms.NameForm()
return render(request, 'groupbuilder.html', {'form': form})
"""
Django settings for crossLFG project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.utils.translation import ugettext_lazy as _
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
#"frontpage.context_processors.contactform",
)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_PATH = '/home/KBrothers/crossLFG/'
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/'
DATABASE_PATH = os.path.join(PROJECT_PATH,'users.db')
#LOCALE_PATHS = os.path.join(PROJECT_PATH, 'locale',)
LOCALE_PATHS = ('/home/KBrothers/crossLFG/locale',)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ****
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
CRISPY_TEMPLATE_PACK = 'bootstrap3'
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'main_app',
'groupbuilder',
'crispy_forms',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
os.path.join(PROJECT_PATH, 'main_app/templates'),
os.path.join(PROJECT_PATH, 'accounts/templates'),
os.path.join(PROJECT_PATH, 'groupbuilder/templates'),
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'crossLFG.urls'
WSGI_APPLICATION = 'crossLFG.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
MAIL_USE_TLS = True
EMAIL_HOST = '****'
EMAIL_PORT = ****
EMAIL_HOST_USER = '********'
EMAIL_HOST_PASSWORD = '********'
#CRISPY_TEMPLATE_PACK = 'bootstrap3'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': DATABASE_PATH, # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en'
#LANGUAGE_CODE = 'de'
#LANGUAGE_CODE = 'fr'
LANGUAGE_COOKIE_NAME = 'wm_lang'
TIME_ZONE = 'UTC'
LANGUAGES = (
('en', _('English')),
#('de', _('German')),
#('fr', _('French')),
)
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
模板:
<form action="/your-name/" method="post">
{% csrf_token %}
{ form.as_p }}
<input type="submit" value="Submit" />
</form>
奇怪的是如果我把
return HttpResponseRedirect('/thanks/')
就在“else”之后
声明,我甚至无法在尝试访问表单URL时填写表单。它直接带我去/谢谢/。因此,request.method检查总是失败
我错过了什么???您的模板代码中有一个错误:
{form.as\u p}
添加“{”我尝试了以下操作:
如果表单字段为
your_name = forms.CharField(label='Your name', max_length=5, required = True)
我只是点击了提交按钮,没有填写姓名,什么也没有发生。没有重新加载页面,没有错误消息(因为字段设置为“必需”)
如果我将字段定义为
your_name = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=("our name"))
我点击了Submit而没有名字我确实得到了一个Django表单字段错误问题解决了:
<script src="{{STATIC_URL}}js/plugins/jqBootstrapValidation.js"></script>
我不太清楚为什么,但一定是因为一些表单功能受到了干扰
无论如何,感谢您的帮助控制台上是否有任何错误?如果您的请求是
POST
请求,但表单无效,会发生什么情况?确实要将有效数据传递给表单吗?尝试调试到代码中:粘贴import pdb;pdb.set_trace()
在if
语句之前。当解释器“点击”这一行时,控制台中会出现一个“愚蠢”的Python shell。在这种情况下,request.method会说什么?如果将重定向放在else之后,您描述的行为正是您应该期望的:当您最初加载页面时,方法是get.thepdb.set\u tra行政长官()
失败,未提供任何异常消息请使用问题的编辑链接添加其他信息。“发布答案”按钮仅用于完整回答问题。这不提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论-你可以随时对自己的帖子发表评论,一旦你有足够的评论,你就可以发表评论了。