Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django不会在提交时添加用户_Python_Django - Fatal编程技术网

Python django不会在提交时添加用户

Python django不会在提交时添加用户,python,django,Python,Django,我真的很困惑为什么我的models.py在登录时不会向用户发送电子邮件并传递到数据库。它不会显示在Django管理页面中。任何帮助都将不胜感激。非常感谢。我粘贴了模型和view.py 这就是我到目前为止所做的: views.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render from django.contrib.auth impo

我真的很困惑为什么我的models.py在登录时不会向用户发送电子邮件并传递到数据库。它不会显示在Django管理页面中。任何帮助都将不胜感激。非常感谢。我粘贴了模型和view.py

这就是我到目前为止所做的:

views.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():
            print("TESTINGGINGIGNGING")
            form.save()
           password=raw_password)
            #login(request, user)
            #return HttpResponse("success")
            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:
        form = UserCreationForm()
    return render(request, 'signup_form.html', {'form': form})
signupform.html(表单)

signup.html

<!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>

登录
{%load staticfiles%}
}
注册

在定义了
用户配置文件
类的
models.py
文件中,添加两个信号接收器以创建和保存配置文件

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
这应该在创建新用户时创建概要文件对象,然后在每次保存用户时更新概要文件

对不起,我误解了这个问题

如果您想查看用户管理员上的配置文件字段,则需要为
user
创建自己的管理员类

您需要为您的配置文件添加内联线

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'Profile'
    fk_name = 'user'

class CustomUserAdmin(UserAdmin):
    inlines = (UserProfileInline, )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomUserAdmin, self).get_inline_instances(request, obj) 

在定义了
UserProfile
类的
models.py
文件中,添加两个信号接收器以创建和保存配置文件

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
这应该在创建新用户时创建概要文件对象,然后在每次保存用户时更新概要文件

对不起,我误解了这个问题

如果您想查看用户管理员上的配置文件字段,则需要为
user
创建自己的管理员类

您需要为您的配置文件添加内联线

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'Profile'
    fk_name = 'user'

class CustomUserAdmin(UserAdmin):
    inlines = (UserProfileInline, )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomUserAdmin, self).get_inline_instances(request, obj) 

如果您的模型未显示在Admin中,请确保已在Admin.py文件中注册该模型

from django.contrib import admin
from myproject.myapp.models import UserProfiles

admin.site.register(UserProfiles)

要使此代码段正常工作,您需要将myproject和myapp替换为您自己的名称。

如果您的模型没有显示在Admin中,请确保您已在Admin.py文件中注册它

from django.contrib import admin
from myproject.myapp.models import UserProfiles

admin.site.register(UserProfiles)

要使此代码段正常工作,您需要将myproject和myapp替换为您自己的姓名。

错误实际上出现在我从signup.html扩展的注册表单中。html提交按钮不起作用。因此,我不得不手动将其添加到我的注册表单中。

错误实际上出现在我从signup.html扩展的注册表单中。html提交按钮不起作用。因此,我不得不手动将其添加到我的注册表单中。

包括该模板提交到的视图。包括该模板提交到的视图。我添加了所有这些,它不起作用。是什么阻止了它被读取?非常感谢你!如果您正在保存
用户
模型,没有任何东西可以阻止它们,因为每次保存
用户
时都会执行它们。检查你的终端。如果您没有看到创建的配置文件,则是其他原因造成的。@coderManzala666抱歉,误解了您的问题。你需要使用内联管理类来获得在django admin中向用户显示的配置文件。我在中添加了所有这些,但它不起作用。是什么阻止了它被读取?非常感谢你!如果您正在保存
用户
模型,没有任何东西可以阻止它们,因为每次保存
用户
时都会执行它们。检查你的终端。如果您没有看到创建的配置文件,则是其他原因造成的。@coderManzala666抱歉,误解了您的问题。您需要使用内联管理类来获取要在django admin中的用户上显示的配置文件。它仍然不会添加到数据库中。我更新了我的代码。它仍然没有添加到数据库中。我更新了我的代码。