这是一个非常古老的问题和许多与之相关的类似结果,但我根本找不到解决它的正确方法。
我正在使用 Django 1.5,我有一个相当简单的尝试尝试在 Django 中使用“发布”表单:我在 Django 项目“webnote”中创建了一个“note”应用程序,当 url 为“/note/”时,它将简单地显示表单和简单的欢迎信息 当我单击提交时,我希望它会显示另一个简单的welcome1 信息。
这是相关文件
注意/models.py
from django.db import models
from datetime import datetime, date,time
from django.utils import timezone
class Title(models.Model):
title =models.CharField(max_length=2000)
def __unicode__(self):
return self.title
注意/views.py
from django.template import Context, loader
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from note.models import Title
from django.core.context_processors import csrf
def index(request):
template = loader.get_template('note/index.html')
context = Context({
'Name':"Guys",
})
return HttpResponse(template.render(context))
def insert(request):
#return HttpResponse("Hello World, This is the Index Page")
return render(request, 'note/index.html', {'Name':"NewGuy"})
模板/注释/index.html
<html>
<body>
Hello {{ Name }}, Welcome!
<form action="insert/" method="post">
{% csrf_token %}
<h6>Title</h6>
<input type="text" name="title_input" value="Please Input Title">
<input type="submit" name="SB" value="insert">
</form>
</body>
</html>
A 可能不是那个相关的文件
设置.py
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'webnote',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '3306',
}
}
ALLOWED_HOSTS = []
TIME_ZONE = 'America/New_York'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
SECRET_KEY = 'gpnmov_g$zn8w@_0(s@$wlbgo2+y30*98ab*ne^)@98!zpq_w-'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'webnote.urls'
WSGI_APPLICATION = 'webnote.wsgi.application'
TEMPLATE_DIRS = (
'/www/webnote/note/templates/note'
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'note'
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
我尝试了该论坛中类似(相同)问题中提到的几种方法。喜欢:
1.使用render_to_response
2.显式使用RequestContext替换Context