TypeError: 'NoneType' object is not subscriptable followed by AttributeError: 'NoneType' object has no attribute 'split'

≡放荡痞女 提交于 2019-12-04 06:04:37

It appears that its a python error:

Django ticket: https://code.djangoproject.com/ticket/26995 Python ticket: https://bugs.python.org/issue14574

Didn't managed to solve just yet, if I can I will edit this answer explaining in detail how to solve it.

Edit

It seems to be a error on chrome. It didn't come to me, 'cause I was using Opera, but opera uses chromium as well. In internet explorer the error didn't show.

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

This is the bug tracker

the first error self.environ['SERVER_PROTOCOL'].upper() is failing because self.environ['SERVER_PROTOCOL'] is None, and you can't do None.upper() you can only do .upper() on strings (as far as i'm aware).

the second error self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' This is failing because you self.status is None and you can't do None.split(), again this should be a string.

So it looks like some of the required values for these classes aren't being initialised or passed through.

It looks like the issue is with Django.core and WSGIref, so either the configuration isn't correct or perhaps there's an issue with the version you have installed?

Which version of Django are you running? Also are you running the server from ./manage.py runserver ?

[update 2017-09-28]

There's a couple of things I've modified, to start with I'm not copying the request.POST to a new dict, before passing it, this could be the cause of some of the values disappearing when trying to process a post.

I'm also passing user as a kwarg to your form, rather than trying to inject it into the copied POST.

mostly I've just updated some of the code to be a little easier to read, let's see how that goes.

# 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,
        }
    )

Are you using PostgreSQL? It could be caused by a code page error.

C:\>psql -U postgres
psql (10.5)
WARNING: Console code page (850) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=#

Trying setting the code page before you start Django.

cmd.exe /c chcp 1252
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!