简体   繁体   English

django-AttributeError:“ NoneType”对象没有属性“ first_name”

[英]django - AttributeError: 'NoneType' object has no attribute 'first_name'

Would appreciate some help understanding whenever I try to setup my client I am getting the following error: AttributeError: 'NoneType' object has no attribute 'first_name' . 每当我尝试安装client时,都会得到一些帮助,以理解以下错误: AttributeError: 'NoneType' object has no attribute 'first_name'

Here is the log: 这是日志:

Environment:


Request Method: GET
Request URL: http://192.168.33.10:8000/podfunnel/clientsetup/

Django Version: 1.9
Python Version: 2.7.6
Installed Applications:
('producer',
 'django.contrib.admin',
 'django.contrib.sites',
 'registration',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'storages',
 'django_extensions',
 'randomslugfield',
 'adminsortable2',
 'crispy_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/vagrant/fullcast_project/producer/views/pod_funnel.py" in get
  110.         initial_values['first_name'] = client.first_name

Exception Type: AttributeError at /podfunnel/clientsetup/
Exception Value: 'NoneType' object has no attribute 'first_name'

It has to do in my ClientSetupView under y views.py : 它必须在y views.py下的ClientSetupView执行:

class ClientSetupView(View):
form_class = ClientSetupForm
template_name = 'pod_funnel/forms.html'

# In the get we manage the request to get the form and prefill it if necessary
def get(self, request, *args, **kwargs):
    # We need to check first if the current user has a client and podcast setup,
    # if so, prepopulate. Otherwise get empty form.
    initial_values = {}
    user = request.user

    if Client.objects.filter(user=user).exists():

        client = CLient.objects.filter(user=user).first()

        initial_values['first_name'] = client.first_name
        initial_values['last_name'] = client.last_name

        podcast = Podcast.objects.filter(client=client).first()
        request.session['client_id'] = client.id

        if podcast:
            initial_values['podcast_name'] = podcast.name
            # lets store podcast_id in session so we can retrieve it directly in post
            request.session['podcast_id'] = podcast.id

    form = self.form_class(initial=initial_values)
    return render(request, self.template_name, {'form': form})

# In the the post we manage updates to the data
def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST)

    if form.is_valid():
        # lets get the data
        first_name = form.cleaned_data.get('first_name')
        last_name = form.cleaned_data.get('last_name')
        podcast_name = form.cleaned_data.get('podcast_name')

        # First we see if we have a client_id in session, if so we retrieve and update
        # otherwise we create a new one.
        client_id = request.session.get('client_id', None)
        if client_id is not None:
            request.session.delete('client_id')
            client = Client.objects.get(id=client_id)
        else:
            client = Client()
        client.first_name = first_name
        client.last_name = last_name
        client.company_name = podcast_name
        client.save()

        # Now we update or create the associated podcast.
        podcast_id = request.session.get('podcast_id', None)
        if podcast_id is not None:
            request.session.delete('podcast_id')
            podcast = Podcast.objects.get(id=podcast_id)
        else:
            podcast = Podcast()
        podcast.client = client
        podcast.name = podcast_name
        podcast.save()

        success, message_list = update_or_create_preset_for_podcast(podcast)

        # in any case we can continue with additional podcast setup, but we need
        # to attempt to create the preset one more time at some point before the
        # user can create the first productions.
        return HttpResponseRedirect(reverse('podfunnel:podcastsetup'))

    return render(request, self.template_name, {'form': form})

The line were you check if client is not None should come immediately after reading from the database as first may return None if there are no items: 您检查if client is not None应从数据库读取后立即出现,因为如果没有项目,则first可能返回None

client = Client.objects.filter(user=user).first()
if client is not None:
    initial_values['first_name'] = client.first_name
    initial_values['last_name'] = client.last_name

Or you can use exists to test if there is at least one client for that user before fetching first : 或者,您可以使用exists来测试在first获取该用户之前是否至少有一个该用户的客户端:

if Client.objects.filter(user=user).exists():
    client = Client.objects.filter(user=user).first()
    initial_values['first_name'] = client.first_name
    initial_values['last_name'] = client.last_name

The exists approach is more efficient: exists方法更有效:

Returns True if the QuerySet contains any results, and False if not. 如果QuerySet包含任何结果,则返回True,否则返回False。 This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query. 这试图以最简单,最快的方式执行查询,但是它执行的查询与普通QuerySet查询几乎相同。

According to the documentation first() would return None if there was nothing matched: 根据文档,如果没有匹配项, first()将返回None

Returns the first object matched by the queryset, or None if there is no matching object. 返回与查询集匹配的第一个对象,如果没有匹配的对象,则返回None。

In other words, client has a None value after executing this line: 换句话说,执行以下行后, client具有None值:

client = Client.objects.filter(user=user).first()

which means that there was no clients for the current user. 这意味着当前用户没有客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“ CustomUser”对象没有属性“ first_name” - AttributeError: 'CustomUser' object has no attribute 'first_name' /admin 'AnonymousUser' 对象的 AttributeError 没有属性 'first_name' - AttributeError at /admin 'AnonymousUser' object has no attribute 'first_name' Django Admin-'UserProfile'对象没有属性'first_name' - Django Admin - 'UserProfile' object has no attribute 'first_name' Django:AttributeError:'NoneType'对象没有属性'split' - Django: AttributeError: 'NoneType' object has no attribute 'split' Django - AttributeError: 'NoneType' 对象没有属性 'pk' - Django - AttributeError: 'NoneType' object has no attribute 'pk' Django - AttributeError:“NoneType”对象没有属性“方法” - Django - AttributeError: 'NoneType' object has no attribute 'method' AttributeError: 'NoneType' 对象没有属性 'name'? - AttributeError: 'NoneType' object has no attribute 'name'? AttributeError:'NoneType'对象没有属性'name - AttributeError: 'NoneType' object has no attribute 'name AttributeError: 'NoneType' object 没有带有 streamlit 的属性 'name' - AttributeError: 'NoneType' object has no attribute 'name' with streamlit AttributeError:'NoneType'对象没有属性'name' - AttributeError: 'NoneType' object has no attribute 'name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM