2017-09-22 7 views
1

djangoを使用しています。TypeError: 'NoneType'オブジェクトは添え字付きではなく、その後にAttributeError: 'NoneType'オブジェクトには 'split'属性がありません

class Postagem(models.Model): 
id = models.AutoField(primary_key=True, editable=False) 
descricao = models.CharField(max_length=50) 
area = models.ForeignKey('core.Area', null=True) 
user = models.ForeignKey('User') 
categoria = models.CharField(max_length=50, null=True) 
post = models.FileField(upload_to='posts/', null=True) 
thumbnail = models.FileField(upload_to='posts/', null=True) 


def __str__(self): 
    return self.descricao 

次の形式::

class PostForm(forms.ModelForm): 
categoria = forms.ChoiceField(choices=[("Video","Vídeo"),("Audio","Aúdio"),("Imagem","Imagem"),("Musica","Música")], required=True) 
thumbnail = forms.FileField(required=False) 

class Meta: 
    model = Postagem 
    fields = ['descricao', 'area', 'user', 'post'] 

ビュー:

def profileView(request): 
context = getUserContext(request) 

if request.method == 'POST': 
    exception=None 
    userDict = {} 
    userDict["user"] = context["user"].id  
    if "categoria" in request.POST: 
     newPost = request.POST.copy() 
     newPost.update(userDict) 
     form = PostForm(newPost,request.FILES) 
     print("postform POST: ",newPost, " File ",request.FILES) 
     if form.is_valid(): 
      print("valid") 
      try: 
       form.save() 
       print("saved") 
       return HttpResponseRedirect(reverse_lazy('accounts:profile')) 
      except IntegrityError as e: 
       print("Integrity Error") 
       exception=e    
     else: 
      print("PostForm error") 
      print(form.errors) 

    form.non_field_errors=form.errors 
    if exception is not None: 
     form.non_field_errors.update(exception) 
    context['form']=form 

posts = Postagem.objects.get_queryset().order_by('id') 
paginator = Paginator(posts, 12) 
page = request.GET.get('page') 
context["areas"] = Area.objects.all() 
try: 
    posts = paginator.page(page) 
except PageNotAnInteger: 
    # If page is not an integer, deliver first page. 
    posts = paginator.page(1) 
except EmptyPage: 
    # If page is out of range (e.g. 9999), deliver last page of results. 
    posts = paginator.page(paginator.num_pages) 

context["posts"]=posts 

return render(
    request, 
    'accounts/profile.html', 
    context 
) 

そして最後テンプレートで

:の

{% for post in posts %} 
     {% if forloop.counter0|divisibleby:4 or forloop.counter0 == 0 %}  
     <div id="grid-profile" class="row grid"> 
      <div class="col-md-1"></div> 
     {% endif %} 

      {% ifnotequal post.categoria "Imagem"%} 
      <iframe id=post{{forloop.counter}} width="420" height="315" 
       src={{post.post.url}}> 
      </iframe> 
      {% else %} 
      <div class="col-md-2"> 
       <button type="button" id="modal1trigger" data-toggle="modal" data-target="#modal1"><img class="img-responsive" src={{post.post.url}}></img></button> 
      </div> 
      {% endifnotequal %} 

     {% if forloop.counter|divisibleby:4 or forloop.counter == posts|length %} 
      <div class="col-md-1"></div> 
     </div> 
     {% endif %} 
     {% endfor %} 
     <div class="pagination"> 
      <span class="step-links"> 
       {% if posts.has_previous %} 
        <a href="?page={{ posts.previous_page_number }}">previous</a> 
       {% endif %} 

       <span class="current"> 
        Page {{ posts.number }} of {{ posts.paginator.num_pages }}. 
       </span> 

       {% if posts.has_next %} 
        <a href="?page={{ posts.next_page_number }}">next</a> 
       {% endif %} 
      </span> 
     </div> 

は、私はカットし、いくつかの私は、次のモデルを持っていますテンプレート...とにかく、私はできるようになりたいすべての種類の投稿を追加します。歌、ビデオ、画像、文書などが含まれています。投稿が追加されています。しかし、私は繰り返しエラーが発生していると私は理由を知らない。それは非常に危険かもしれませんので、私はそれを調べ始めましたが、受け入れられる答えを見つけるのは幸運でした。 エラーは、曲やビデオを取得しようとしたときにのみ発生します。

[21/Sep/2017 23:32:44] "GET /media/posts/13567830_1641362072853439_1924036772_n.mp4 HTTP/1.1" 200 1171456 
Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 279, in write 
    self._write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 453, in _write 

    result = self.stdout.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 775, in write 
    self._sock.sendall(b) 
ConnectionResetError: [WinError 10054] Foi forçado o cancelamento de uma conexão existente pelo host remoto 
[21/Sep/2017 23:32:44] "GET /media/posts/13567830_1641362072853439_1924036772_n.mp4 HTTP/1.1" 500 59 
---------------------------------------- 
Exception happened during processing of request from ('127.0.0.1', 53113) 
Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 279, in write 
    self._write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 453, in _write 

    result = self.stdout.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 775, in write 
    self._sock.sendall(b) 
ConnectionResetError: [WinError 10054] Foi forçado o cancelamento de uma conexão existente pelo host remoto 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 141, in run 
    self.handle_error() 
    File "C:\Users\eduardo\Envs\ProExC\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_erro 
r 
    super(ServerHandler, self).handle_error() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 368, in handle 
_error 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 274, in write 
    self.send_headers() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 331, in send_h 
eaders 
    if not self.origin_server or self.client_is_modern(): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 344, in client 
_is_modern 
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' 
TypeError: 'NoneType' object is not subscriptable 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 639, in process_re 
quest_thread 
    self.finish_request(request, client_address) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 361, in finish_req 
uest 
    self.RequestHandlerClass(request, client_address, self) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 696, in __init__ 
    self.handle() 
    File "C:\Users\eduardo\Envs\ProExC\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle 
    handler.run(self.server.get_app()) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 144, in run 
    self.close() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\simple_server.py", line 35, in cl 
ose 
    self.status.split(' ',1)[0], self.bytes_sent 
AttributeError: 'NoneType' object has no attribute 'split' 
---------------------------------------- 
[21/Sep/2017 23:32:44] "GET /media/posts/03_-_The_Mute.mp3 HTTP/1.1" 200 1179648 
Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 279, in write 
    self._write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 453, in _write 

    result = self.stdout.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 775, in write 
    self._sock.sendall(b) 
ConnectionResetError: [WinError 10054] Foi forçado o cancelamento de uma conexão existente pelo host remoto 
[21/Sep/2017 23:32:44] "GET /media/posts/03_-_The_Mute.mp3 HTTP/1.1" 500 59 
---------------------------------------- 
Exception happened during processing of request from ('127.0.0.1', 53114) 
Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 138, in run 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 279, in write 
    self._write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 453, in _write 

    result = self.stdout.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 775, in write 
    self._sock.sendall(b) 
ConnectionResetError: [WinError 10054] Foi forçado o cancelamento de uma conexão existente pelo host remoto 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 141, in run 
    self.handle_error() 
    File "C:\Users\eduardo\Envs\ProExC\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_erro 
r 
    super(ServerHandler, self).handle_error() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 368, in handle 
_error 
    self.finish_response() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 180, in finish 
_response 
    self.write(data) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 274, in write 
    self.send_headers() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 331, in send_h 
eaders 
    if not self.origin_server or self.client_is_modern(): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 344, in client 
_is_modern 
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' 
TypeError: 'NoneType' object is not subscriptable 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 639, in process_re 
quest_thread 
    self.finish_request(request, client_address) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 361, in finish_req 
uest 
    self.RequestHandlerClass(request, client_address, self) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\socketserver.py", line 696, in __init__ 
    self.handle() 
    File "C:\Users\eduardo\Envs\ProExC\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle 
    handler.run(self.server.get_app()) 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\handlers.py", line 144, in run 
    self.close() 
    File "c:\users\eduardo\appdata\local\programs\python\python36-32\Lib\wsgiref\simple_server.py", line 35, in cl 
ose 
    self.status.split(' ',1)[0], self.bytes_sent 
AttributeError: 'NoneType' object has no attribute 'split' 
--------------------------------------- 

編集:ゴクゴク

を使用してサーバーを実行ジャンゴ1.11.3、 のWindows 10、オペラとChromeでテスト 、 を使用して

**編集2:**

ちょっと、エラーのみの場合 htmlにiframeタグがある場合は!

答えて

1

それが表示されているPythonのエラー:

ジャンゴチケット:https://code.djangoproject.com/ticket/26995 Pythonのチケット:https://bugs.python.org/issue14574

は私が説明この回答を編集しますことができれば、まだ解決するために管理されていませんでした詳しくはどのように解決するのか。

編集

クロームでエラーのようです。それは私には来なかった。なぜなら私はオペラを使っていたからだが、オペラはクロムも使う。 Internet Explorerでは、エラーは表示されませんでした。

https://code.djangoproject.com/ticket/21227#no1

これは、問題は、このself.environとself.statusは、特に私のプロジェクトからのものではないですバグトラッカー

+0

WSGIrefの問題のように見えましたが、それはまったく当てはまりませんでした。あなたはiframeを使わないと考えましたか? – BenDog

0

最初のエラー self.environ['SERVER_PROTOCOL'].upper()は、self.environ ['SERVER_PROTOCOL']がNoneであり、None.upper()を実行できないため、失敗しています。文字列の.upper()私は意識している)。

2番目のエラー self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' あなたはself.statusので、これは失敗であるNoneです、あなたはNone.split()を行うことができない、再びこれは文字列でなければなりません。

したがって、これらのクラスに必要な値の一部が初期化されていないか、または渡されていないようです。

Django.coreとWSGIrefに問題があるようですので、設定が正しくないか、インストールしたバージョンに問題がありますか?

実行中のDjangoのバージョンは? ./manage.py runserverからサーバーを実行していますか?

は、[更新2017年9月28日]

あり、私が変更した物事のカップルが、だ私はそれを渡す前に、新しい辞書にrequest.POSTをコピーしていないよと開始するには、これは可能性が投稿を処理しようとしたときに値の一部が消えてしまう原因。

コピーしたPOSTに注入するのではなく、ユーザーをkwargとしてフォームに渡しています。

ほとんど私はちょっと読みやすくするためにいくつかのコードを更新しましたが、それがどうなるか見てみましょう。

# forms.py 
class PostForm(forms.ModelForm): 
    categoria = forms.ChoiceField(choices=[("Video","Vídeo"),("Audio","Aúdio"),("Imagem","Imagem"),("Musica","Música")], required=True) 
    thumbnail = forms.FileField(required=False) 

    class Meta: 
     model = Postagem 
     fields = ['descricao', 'area', 'user', 'post'] 

    def __init__(self, *args, **kwargs): # handle user kwarg 
     if 'user' in kwargs.keys(): 
      self.user = kwargs.pop('user') 
     super(PostForm, self).__init__(*args, **kwargs) 

# views.py 
def profileView(request): 
    form = None  
    if request.method == 'POST': 
     exception=None 
     if "categoria" in request.POST: 
      if request.user: 
       form = PostForm(request.POST,request.FILES, user=request.user) # pass user as a kwarg 
       print("postform POST: ",newPost, " File ",request.FILES) 
       if form.is_valid(): 
        print("valid") 
        try: 
         form.save() 
         print("saved") 
         return HttpResponseRedirect(reverse_lazy('accounts:profile')) 
        except IntegrityError as e: 
         print("Integrity Error") 
         exception=e    
       else: 
        print("PostForm error") 
        print(form.errors) 
       #form has been indented as previously it would have been called before being initialised 
       form.non_field_errors=form.errors 
       if exception is not None: 
        form.non_field_errors.update(exception) 
    posts = Postagem.objects.get_queryset().order_by('id') 
    paginator = Paginator(posts, 12) 
    page = None 
    if request.GET: 
     page = request.GET.get('page') 
    areas = Area.objects.all() 
    try: 
     posts = paginator.page(page) 
    except PageNotAnInteger: 
     # If page is not an integer, deliver first page. 
     posts = paginator.page(1) 
    except EmptyPage: 
     # If page is out of range (e.g. 9999), deliver last page of results. 
     posts = paginator.page(paginator.num_pages) 
    #return values  
    return render(
     request, 
     'accounts/profile.html', 
     { 
      'form': form, 
      'areas': areas, 
      'posts': posts, 
     } 
    ) 
+0

です。私はgulpを使用してサーバーを実行しています。私はDjango 1.11.3を実行しています – EduardoMaia

+0

./manage.py runserverを使用してdjangoサーバーを実行すると、この問題が発生しますか? – BenDog

+0

はい... Gulpは基本的にrunserverを実行し、runserverを再度実行するための変更を監視します... – EduardoMaia

関連する問題