我正在研究一个非常基本的Django应用程序,希望能够自学这个框架,并且我遇到了一个小问题。该应用程序旨在获取用户输入,将其存储在mysqlite数据库中,并在接收请求时显示数据库的内容。当应用程序在本地运行时(manage.py runserver),我可以正常工作,但是一旦部署它就会行为不端。出于某种原因,存储逻辑在实时站点上出现故障(Windows Server 2012通过MS Azure)。
以下是相关代码:
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import render_to_response
from helloworld.models import Registry
def home(request):
return render_to_response("home.html")
def register(request):
return render_to_response("register.html")
def view(request):
entries = Registry.objects.all()
args = {}
args["entries"] = entries
return render_to_response("registry.html", args)
def submit(request):
nameIn = request.GET.get('name')
ageIn = request.GET.get('age')
monthIn = request.GET.get('month')
try:
Registry(name=nameIn, age=ageIn, month=monthIn).save()
except:
pass
return view(request)
我做了大量的调试,并尝试了几种存储数据的方法。我检查过该方法是否正在接收有效值。我不能,为了我的生活,弄清楚为什么调用save()会引发错误。希望对你们这些Django-pros来说很明显!提前谢谢,任何帮助将不胜感激!附:我附加了我的设置文件,因为我怀疑可怜的配置是我的麻烦的来源。
"""
Django settings for cny_solar project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's7xv#m=5h=mjrdg_7-$=7kr8kx4np9$@6qzlg+m4xt&6-@-zi2'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS=['127.0.0.1','localhost']
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'helloworld',
)
MIDDLEWARE_CLASSES = (
'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 = 'helloworld.urls'
WSGI_APPLICATION = 'helloworld.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_ROOT = BASE_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', os.path.join(BASE_DIR, 'static')),
)