django在提交时不会添加用户

时间:2017-11-12 15:23:15

标签: 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 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(表单)

{% extends 'signup.html' %}

{% block body %}

    <form method = "post" action = ".">
        {% 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.
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user')
    photo = FileField(verbose_name=_("Profile Picture"),
                      upload_to=upload_to("main.UserProfile.photo", "profiles"),
                      format="Image", max_length=255, null=True, blank=True)
    website = models.URLField(default='', blank=True)
    bio = models.TextField(default='', blank=True)
    phone = models.CharField(max_length=20, blank=True, default='')
    city = models.CharField(max_length=100, default='', blank=True)
    country = models.CharField(max_length=100, default='', blank=True)
    organization = models.CharField(max_length=100, default='', blank=True)

@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()

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>

3 个答案:

答案 0 :(得分:1)

在您定义了models.py类的UserProfile文件中,添加两个信号接收器以创建&amp;保存个人资料;

@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) 

答案 1 :(得分:1)

如果您的模型未显示在管理员中,请确保已在admin.py文件中注册

overflow: scroll

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

答案 2 :(得分:0)

错误实际上是在我从signup.html扩展的signup_form中.html提交按钮无效。所以我不得不手动将它添加到我的signup_form。