本文介绍了TypeError在/ UserRegis / __init __()得到一个意想不到的关键字参数'username'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我使用UserReg.objects.create()时,它会报告上面的内容。而且我真的在海拔几个小时内查找了解决方案,但是没有办法。所以我想知道我在哪里错了,我该如何解决?



PS:原谅我可怜的英语,非常感谢。



我很抱歉,这些代码只是一部分代码,我只是发布一些。

  • project/
    • app1/...
    • app2/...
    • other_general(manage.py etc)======I will post all files=======

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

that's all , and I realy need your any advice, please

解决方案

Replace

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

with

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

这篇关于TypeError在/ UserRegis / __init __()得到一个意想不到的关键字参数'username'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:03