1

私はセットアップを試みると助けになります。client次のエラーが表示されます:AttributeError: 'NoneType' object has no attribute 'first_name'。ここでdjango - AttributeError: 'NoneType'オブジェクトに 'first_name'属性がありません。

はログです:

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' 

それを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}) 
+0

クライアントセット –

答えて

2

あなたはアイテムがない場合if client is not NoneNoneを返すことがfirstとしてデータベースからの読み込み直後に来るべきでチェックした行:

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 

それとも、少なくとも1つのクライアントが存在するかどうかをテストするために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 

existsアプローチはより効率的である。

Returns True if the QuerySet contains any results, and False if not. 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.

+0

助けてくれてありがとう!今度は 'IntegrityError:列のnull値user_idがnot-null constraint'に違反するという別のエラーが発生しました。エラーログと私の 'views.py 'の新しいバージョンで私の質問を更新しました。 –

+0

心配しないで、解決しました。ありがとう! –

1

ドキュメントfirst()によると一致するものは何もなかった場合Noneを返します:

Returns the first object matched by the queryset, or None if there is no matching object.

I Nすなわち、clientこの行を実行した後None値を有する:

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

現在のユーザのためのクライアントが存在しなかったことを意味します。

+0

ご協力ありがとう、あなたの質問と前のいずれかが優れています!しかし今、私はそれが 'IntegrityError:列のヌル値" user_id "not-null constraint'に違反しているという別のエラーを出しています。エラーログと私の 'views.py 'の新しいバージョンで私の質問を更新しました。 –

+0

解決済みです。ありがとう! –

関連する問題