所以我一直试图将信息从Django的前端保存到后端。但是,当我在Django上检查我的数据库时,它不存在。你能否检查一下我做错了什么,因为我只想要数据库中的电子邮件和密码。我是Django的新手。谢谢!
view.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
#from django.contrib.auth import views as auth_views
#from . import views
# Create your views here.
def login(request):
return render(request, 'login.html')
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
#username = form.cleaned_data.get('username')
#raw_password = form.cleaned_data.get('password1')
#user = authenticate(username=username, password=raw_password)
#login(request, user)
return redirect('login.html')
else:
form = UserCreationForm()
return render(request, 'templates/signup_form.html', {'form': form})
return render(request, 'signup_form.html')
signup_form.html
{% extends 'signup.html' %}
{% block body %}
<form method = "post">
{% csrf_token %}
{{ form.as_p}}
</form>
{% endblock %}
models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.11.7.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
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.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*o7vg-hqbcx9bqh6fcg^daw21(#2bb8ik14-^02e!nus*y##&c'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{ #['./website/static'],
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['./website/templates'],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "website", "static"),
)
答案 0 :(得分:2)
将内部else
的{{1}}子句移至外部if form.is_valid()
并删除最后的if request.method == 'POST'
语句。
您的注册视图应如下所示。
return
参考: https://docs.djangoproject.com/en/1.11/topics/forms/#the-view
答案 1 :(得分:2)
您希望将表单提交到同一网址,但是通过帖子请求,您需要提供.
作为action
变量的值,格式为
在您的注册表单中添加action
变量,如下所示
<form method = "post" action=".">
{% csrf_token %}
{{ form.as_p}}
</form>
您的视图无法处理get
网址的signup
请求,这是由于额外的缩进造成的。
按如下所示更改注册视图
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
#username = form.cleaned_data.get('username')
#raw_password = form.cleaned_data.get('password1')
#user = authenticate(username=username, password=raw_password)
#login(request, user)
return redirect('login.html')
else:
#this means the form is not valid
#so you need to provide the varibles as they were submitted, so the user can change it to proper values
form = UserCreationForm(request.POST)
else:
#this is when the user sends get request
form = UserCreationForm()
return render(request, 'templates/signup_form.html', {'form': form})