/ UserRegis / __init __()的TypeError有一个意外的关键字参数'username'

时间:2015-12-03 11:20:15

标签: django python-2.7 django-models django-1.7

我遇到的问题是,当我使用UserReg.objects.create()时,它会报告上面的内容。而且我真的已经找到了解决了一段时间的问题,但它不起作用。所以我想知道我错在哪里,我该如何解决?

PS:原谅我英语不好,非常感谢。

我很抱歉,这些代码只是所有代码的一部分,我只是张贴了一些。

  

我的整个项目是

  • 项目/
    • APP1 /...
    • APP2 /...
    • other_general(manage.py等) ======我会发布所有文件=======

app1/models.py

from django.db import models
from django.contrib.auth.models import  AbstractUser
from app2.models import SocialMed,Article


class UserReg(AbstractUser):

    nickname =  models.CharField(  max_length = 32 ,null = True, blank = True )
    avatar = models.ImageField( upload_to = '/media/client/avatar/%Y/%m/%d/' , null = True, blank = True)  # need to refer to something
    signature = models.CharField( max_length = 200, null = True , blank = True )
    user_uuid = models.IntegerField( null = True , blank = True,unique = True )
    user_focus = models.ManyToManyField( SocialMed ,null = True , blank = True )
    user_favor_article = models.ManyToManyField( Article, null = True, blank = True )


    def __unicode__(self):
        return self.nickname

    class Meta(AbstractUser.Meta):
         swappable = 'AUTH_USER_MODEL'
    
    def __init__(self,nickname):
          nickname = 'None'
          return nickname
         

app1/views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from client.models import UserReg
from app2.models import Article, SocialMed



def userRegiste( request ):

    if request.method == 'POST':
        user_name = request.POST.get('username')
        pass_word = request.POST.get('password')
        user_mail = request.POST.get('user_mail')
        clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail )
        print  clienter
        return render( request, 'register.html' )
    else:
        return render(request, 'register.html' )

TRACEBACK

Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                    response = middleware_method(request, callback, callback_args, callback_kwargs)
                    if response:
                        break
            if response is None:
                wrapped_callback = self.make_view_atomic(callback)
                try:
                                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
                except Exception as e:
                    # If the view raised an exception, run it through exception
                    # middleware, and if the exception middleware returns a
                    # response, use that. Otherwise, reraise the exception.
                    for middleware_method in self._exception_middleware:
                        response = middleware_method(request, e)
▶ Local vars
E:\django_file\zuiwan\client\views.py in userRegiste
def userRegiste( request ):
    if request.method == 'POST':
        user_name = request.POST.get('username')
        pass_word = request.POST.get('password')
        user_mail = request.POST.get('user_mail')
                    clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail ) ...
        print  clienter
        return render( request, 'register.html' )
    else:
        return render(request, 'register.html' )
▶ Local vars
C:\Python27\lib\site-packages\django\contrib\auth\models.py in create_user
                                             **extra_fields) ...
▶ Local vars
C:\Python27\lib\site-packages\django\contrib\auth\models.py in _create_user
                                      date_joined=now, **extra_fields) ...
▶ Local vars

就是这样,我真的需要你的建议,

2 个答案:

答案 0 :(得分:1)

替换

clienter = UserReg.objects.create_user( user_name, password = pass_word,email = user_mail )

clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail )

答案 1 :(得分:0)

你可以用另一种方式试试。

user = UserReg()
user.username = user_name
user.email = user_mail
user.set_password(pass_word)
user.save()

并尝试在用户模型中添加此内容

    class Meta(AbstractUser.Meta):
         swappable = 'AUTH_USER_MODEL'