Django将信息呈现给错误的数据库字段

时间:2017-11-13 04:00:30

标签: python django

所以我在Django中实现了一个数据库,使用给定的Users模型获取用户名和密码。但是,当我点击提交时,密码会被带入电子邮件字段。关于我可能做错什么的任何建议?这是用户注册。谢谢!可能是用户没有密码部分?我该如何添加?

forms.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from django import forms
from .forms import UserRegistrationForm
#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 profiles(request):
    return render(request, 'profiles.html')

def signup(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            userObj = form.cleaned_data
            print(userObj)
            username = userObj['username']
            password =  userObj['password']
            if not (User.objects.filter(username=username).exists()):
                User.objects.create_user(username,password)
                user = authenticate(username = username, password = password)
                login(request)
                return HttpResponseRedirect('/')
            else:
                raise forms.ValidationError('Looks like a username with that email or password already exists')
    else:
        form = UserRegistrationForm()
    return render(request, 'signup_form.html', {'form': form})

def home(request):
    return render(request, 'home.html')

views.py

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Login</title>
  {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'signup.css' %}">
  </head>
<body>
</div>
<div class='login'>
  <div class='login_title'>
    <span>Sign Up</span>
  </div>
  <div class='login_fields'>
    <div class='login_fields__user'>
      <div class='icon'>
        <img src='http://d2nysvt2e2u12u.cloudfront.net/img/NPLwebsite17_header_mobile_accountBtn.png?v=34534534'>
      </div>

      <input placeholder='Username' type='text' name = 'username'>
        <div class='validation'>
          <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/217233/tick.png'>
        </div>
      </input>
    </div>

    <div class='login_fields__password'>
      <div class='icon'>
        <img src='http://upload.wikimedia.org/wikipedia/commons/9/9e/Lock_icon.png'>
      </div>
      <input placeholder='Password' type='password' name = 'password'>
      <div class='validation'>
          </div>

    </div>
    <div class='login_fields__submit'>
      <input type='submit' formmethod="post" value='Submit'>



<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>

    <script  src="{% static 'signup.js' %}"></script>

</body>
</html>

signup.html

function getStatementValue(view, statement){ // vars "@extends('parentview') ", 'extends'
    var parentname = "";

    view.replace(new RegExp(/\B@\w+\('([^(')\W]+)'\)\s/, 'm'), function(occurance){
        parentname = occurance.replace('@' + statement + '(\'', '')
            .replace('\')', "");
        console.log(parentname) // parentview
        console.log(parentname + 'test') // testntview <- unexpected result
    });

    return parentname;
}

1 个答案:

答案 0 :(得分:1)

作为the documentation showscreate_user的签名是:

create_user(username, email=None, password=None, **extra_fields)

因此,您将密码作为电子邮件参数传递。相反,请使用关键字参数:

User.objects.create_user(username, password=password)