'sex' is an invalid keyword argument for this function

gq2018 发布于 2013/11/16 22:46
阅读 2K+
收藏 0

views.py

# -*- coding:utf-8 -*-
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView, DetailView
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from models import *
from forms import *
import datetime
from django.db.models import Q
from django.contrib import auth
from django.contrib.auth import authenticate
from django.core.urlresolvers import reverse

ITEMS_PER_PAGE = 15



##注册函数
def zbvip_reg(request):
    if request.method == 'POST':
        form = Reg_Form(request.POST, request.FILES)
        if form.is_valid():
                user = User.objects.create_user(
                    username = form.cleaned_data['username'],
                    password = form.cleaned_data['password1'],
                    email = form.cleaned_data['email'],
                    sex = form.cleaned_data['sex'],
                    addr_zz = form.cleaned_data['addr_zz'],
                    #tximg = form.cleaned_data['tximg'],
                    ahjs = form.cleaned_data['ahjs'],
                    
                )
                user.save()
                user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password1'])
                #user = authenticate(username=user.username, password=user.password)
                auth.login(request, user)
                return HttpResponseRedirect('/zbvip/reg/ok/')
                #return render_to_response('registration/register_success.html', {'user': request.user})
        else:
                return HttpResponse(u'注册用户时发生错误,请返回修改后重新注册!')
    else:
        form = Reg_Form()
        variables = RequestContext(request, {'form':form})
        return render_to_response('registration/register.html', variables)

#会员用户注册成功会的函数
def reg_ok(request):
    return render_to_response('registration/register_success.html', {'user':request.user})

models.py:

# -*- coding:utf-8 -*-
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

class ProfileBase(type):
    def __new__(cls, name, bases, arrts):
        module = attrs.pop('__module__')
        parents = [b for b in bases if isinstance(b, ProfileBase)]


        if parents:
            fileds = []
            for obj_name, obj in attrs.items():
                if isinstance(obj, models.Field):fields.append(obj_name)
                User.add_to_class(obj_name, obj)
                UserAdmin.fieldsets = list(UserAdmin.fieldsets)
                UserAdmin.fieldsets.append((name, {'fields':fields}))
            return super(ProfileBase, cls).__new__(cls, name, bases, attrs)

        
class ProfileUser(object):
    __metaclas__ = ProfileBase


class MyProfileUser(ProfileUser):
    SEX_CHOICES = ((u'保密', u'保密'),(u'男', u'男'),(u'女', u'女'))
    sex = models.CharField(verbose_name=u'性别', choices=SEX_CHOICES, max_length=2)
    addr_zz = models.CharField(max_length='100', blank=True)
    ahjs = models.CharField(max_length='300', null=True, blank=True)

 

reg.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>

<body>
{% if user.is_authenticated %}
你已经登陆了。点击这里查看你的注册信息。
{% else %}
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
    <table border="1px" style="font-size:12px" width="900px">
    <tr> 
      <td>用户名:</td>
      <td>{{ form.username }} ×[<font color="red">必填选项 - 字符长度要大于4</font>]    </td>
    </tr>
    <tr> 
      <td>密码:</td>
      <td>{{ form.password1 }}×[<font color="red">必填选项</font>]    </td>
    </tr>
    <tr> 
      <td>重复密码:</td>
      <td>{{ form.password2 }}×[<font color="red">必填选项</font>]
      </td>
    </tr>
    <tr> 
      <td>你的性别:</td>
      <td>{{ form.sex }}    </td>
    </tr>

    <tr> 
      <td>电子邮箱:</td>
      <td>{{ form.email }} *[<font color="red">必填选项</font>]     </td>
    </tr>
    
    <tr> 
      <td>居住地</td>
      <td>{{ form.addr_zz }}-(选填)
      </td>
    </tr>
	   <!-- <tr> 
      <td align="right" width="135px">头像</td>
      <td width="265px">{{ form.tximg }}-(选填)</td>
      <td width="330px"><div id="nick_nameTip" style="width:280px"></div></td>
    </tr>-->
	
	    <tr> 
      <td>情况介绍</td>
      <td>{{ form.ahjs }}-(选填-建议公司或个体商户填写)
        </td>
    </tr>
    
    
  </table>
  
   <div class="form-actions">
   				<input id="submit" type="submit" value="提 交 注 册" class="btn btn-primary"/>
<br />
<span class="style1">提示:上面红色提示的选项必须填写,其它的可以看情况选填 </span></div>
   <p></p></form>
   
   {% endif %}
</body>
</html>

 

 

在提交注册的时候提示:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/zbvip/reg/

Django Version: 1.5.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'vipbbs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "E:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "E:\python\zbvip\vipbbs\views.py" in zbvip_reg
  33.                     ahjs = form.cleaned_data['ahjs'],
File "E:\Python27\lib\site-packages\django\contrib\auth\models.py" in create_user
  183.                           last_login=now, date_joined=now, **extra_fields)
File "E:\Python27\lib\site-packages\django\db\models\base.py" in __init__
  415.                 raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])

Exception Type: TypeError at /zbvip/reg/
Exception Value: 'sex' is an invalid keyword argument for this function

加载中
0
小虫0302
小虫0302
哈哈 ,其实性别用gender,不是SEX
0
CoserSeu
CoserSeu
sex是敏感词被过滤了
0
gq2018
gq2018

是开玩笑的吧?怎么可能。我以前用过sex就可以的

返回顶部
顶部