Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ConnectionError(<;urllib3.connection.HTTPConnection对象位于0x7f70f408b3c8>;):无法建立新连接_Python_Django - Fatal编程技术网

Python ConnectionError(<;urllib3.connection.HTTPConnection对象位于0x7f70f408b3c8>;):无法建立新连接

Python ConnectionError(<;urllib3.connection.HTTPConnection对象位于0x7f70f408b3c8>;):无法建立新连接,python,django,Python,Django,我正试图用django建立一个社交网络。一切都很好。然后我在两个视图中更改了一些代码。一个是类似于不类似于的帖子视图和个人资料详细视图。我添加了SingleObjectMixin和formviwe,以便人们在访问其他个人资料时可以喜欢和评论帖子。之后,我注意到这个错误每次我尝试登录或更新某些内容时都会出现ccurs。我不知道是什么导致了此错误。有人能帮我解决此问题吗。这是我的个人资料详细信息和更新视图 from django.http import HttpResponseRedirect

我正试图用django建立一个社交网络。一切都很好。然后我在两个视图中更改了一些代码。一个是类似于不类似于的帖子视图和个人资料详细视图。我添加了SingleObjectMixin和formviwe,以便人们在访问其他个人资料时可以喜欢和评论帖子。之后,我注意到这个错误每次我尝试登录或更新某些内容时都会出现ccurs。我不知道是什么导致了此错误。有人能帮我解决此问题吗。这是我的个人资料详细信息和更新视图

from django.http import HttpResponseRedirect
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic import DetailView, RedirectView, UpdateView, FormView
from django.views.generic.detail import SingleObjectMixin
from .models import Profile
from posts.forms import PostModelForm, CommentModelForm
from posts.models import Post
from relationships.models import Relationship


User = get_user_model()

class ProfileDetailView(LoginRequiredMixin, DetailView):

    model = Profile
    slug_field = "slug"
    slug_url_kwarg = "slug"
    form_class = CommentModelForm
    queryset = Profile.objects.prefetch_related('posts__likes', 'friends', 'posts__comment_posted', 'posts__comment_posted__user')

    def get_context_data(self, **kwargs): 
        context = super().get_context_data(**kwargs)
        
        
        sender_profile = get_object_or_404(Profile, id=self.request.user.id)
        receiver_profile = get_object_or_404(Profile, id=self.object.id)
        frnd_btn_state = Relationship.objects.relation_status(
            sender=sender_profile,
            receiver=receiver_profile,
        )
        context['frnd_btn_state'] = frnd_btn_state

        context['frnd_req_rcvd'] = Relationship.objects.invitation_recieved(
            receiver = sender_profile
        )
        context['form'] = CommentModelForm()
        return context
    

class ProfilePostComment(SingleObjectMixin, FormView):
    template_name = 'profiles/profile_detail.html'
    form_class = CommentModelForm
    model = Profile

    def post(self, request, *args, **kwargs):
        comment_form = CommentModelForm()
        profile=Profile.objects.get(user=request.user)
        if 'submit_c_form' in request.POST:
            comment_form = CommentModelForm(request.POST or None)
            if comment_form.is_valid():
                instance = comment_form.save(commit=False)
                instance.user = profile
                post_id = request.POST.get('post_id')
                instance.post = Post.objects.get(id=post_id)
                instance.save()
                comment_form = CommentModelForm()
                return HttpResponseRedirect(self.request.path_info)


    def get_success_url(self):
        return reverse("profiles:detail", kwargs={"slug": self.request.user.username})         

class ProfileDetailComment(View):

    def get(self, request, *args, **kwargs):
        view = ProfileDetailView.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = ProfilePostComment.as_view()
        return view(request, *args, **kwargs)


profile_detail_view = ProfileDetailComment.as_view()

class ProfileUpdateView(LoginRequiredMixin, UpdateView):

    model = Profile
    fields = ["name", 'bio', 'gender', 'country', 
        'favourite',
    ]

    def get_success_url(self):
        return reverse("profiles:detail", kwargs={"slug": self.request.user.username})

    def get_object(self):
        return Profile.objects.get(id=self.request.user.id)

    def form_valid(self, form):
        messages.add_message(
            self.request, messages.INFO, _("Infos successfully updated")
        )
        return super().form_valid(form)


profile_update_view = ProfileUpdateView.as_view()


class ProfileRedirectView(LoginRequiredMixin, RedirectView):

    permanent = False

    def get_redirect_url(self):
        return reverse("profiles:detail", kwargs={"slug": self.request.user.username})


profile_redirect_view = ProfileRedirectView.as_view()
这是我的post视图,包含post和commenting视图以及类似的视图

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db.models import Prefetch
from django.views.generic import UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Post, Like, Comment
from profiles.models import Profile
from .forms import PostModelForm, CommentModelForm



@login_required
def post_comment_create_and_list_view(request):
    profile=Profile.objects.get(user=request.user)
    profile_list = Profile.get_all_friends_list(self=request.user)
    #profile contains list of the friends i have
    queryset = Post.objects.all_posts(user_id=request.user.id, profile_list=profile_list)
    

    #post form
    post_form = PostModelForm()
    if 'submit_p_form' in request.POST:
        post_form = PostModelForm(request.POST or None, request.FILES or None)
        if post_form.is_valid():
            instance = post_form.save(commit=False)
            instance.author = profile
            instance.save()
            post_form = PostModelForm()
            messages.success(request, 'Post published')
            return redirect('posts:main_post_list')
    
    #comment form
    comment_form = CommentModelForm()
    if 'submit_c_form' in request.POST:
        comment_form = CommentModelForm(request.POST or None)
        if comment_form.is_valid():
            instance = comment_form.save(commit=False)
            instance.user = profile
            post_id = request.POST.get('post_id')
            instance.post = Post.objects.get(id=post_id)
            instance.save()
            comment_form = CommentModelForm()
            return redirect('posts:main_post_list')

    context = {
        'queryset': queryset,
        'profile': profile,
        'post_form': post_form,
        'comment_form': comment_form,
    }
    return render(request, 'posts/main.html', context)

        

def like_unlike_post(request):
    user = request.user
    print(user)
    if request.method == 'POST':
        post_id = request.POST.get('post_id')
        post_obj = Post.objects.get(id=post_id)
        print(post_obj)
        profile = Profile.objects.get(user=user)
        print(profile)

        if profile in post_obj.likes.all():
            print(1)
            post_obj.likes.remove(profile)
        else:
            print(2)
            post_obj.likes.add(profile)

        like, created = Like.objects.get_or_create(user=profile, post_id=post_id)

        if not created:
            print(3)
            if like.value=='LIKE':
                print(4)
                like.value='UNLIKE'
            else:
                print(5)
                like.value='LIKE'
        else:
            print(6)
            like.value='LIKE'

            post_obj.save()
            like.save()

    


class PostDeleteView(DeleteView):
    model = Post
    template_name = 'posts/confirm_del.html'
    success_url = reverse_lazy('posts:main_post_list')

    def get_object(self, *args, **kwargs):
        id = self.kwargs.get('id')
        obj = Post.objects.get(id=id)

        if not obj.author.user == self.request.user:
            messages.warning(self.request, 'You can\'t delete others post!')
            return redirect('posts:main_post_list')
        return obj

class PostUpdateView(UpdateView):
    form_class = PostModelForm
    model = Post
    template_name = 'posts/update.html'
    success_url = reverse_lazy('posts:main_post_list')

    def form_valid(self, form):
        profile = Profile.objects.get(user = self.request.user)
        if form.instance.author == profile:
            return super().form_valid(form)
        else:
            form.add_error(None, 'Hey!! You are not the owner of this post!')
            return super().form_invalid(form)


class CommentDeleteView(DeleteView):
    model = Comment
    template_name = 'posts/confirm_del.html'
    success_url = reverse_lazy('posts:main_post_list')

    def get_object(self, *args, **kwargs):
        id = self.kwargs.get('id')
        obj = Comment.objects.get(id=id)

        if not obj.user.user == self.request.user:
            messages.warning(self.request, 'Hey!! You are not the owner of this post!')
            return redirect('posts:main_post_list')
        return obj

class CommentUpdateView(UpdateView):
    form_class = CommentModelForm
    model = Comment
    template_name = 'posts/update.html'
    success_url = reverse_lazy('posts:main_post_list')

    def form_valid(self, form):
        profile = Profile.objects.get(user = self.request.user)
        if form.instance.user.id == profile.id:
            return super().form_valid(form)
        else:
            form.add_error(None, 'Hey!! You are not the owner of this post!')
            return super().form_invalid(form)
我得到的错误

ConnectionError at /accounts/login/
ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8>: Failed to establish a new connection: [Errno 111] Connection refused)
Request Method: POST
Request URL:    http://localhost:8000/accounts/login/
Django Version: 3.0.10
Exception Type: ConnectionError
Exception Value:    
ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8>: Failed to establish a new connection: [Errno 111] Connection refused)
Exception Location: /home/sddhruvo/Desktop/django/stalker/env/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py in perform_request, line 124
Python Executable:  /home/sddhruvo/Desktop/django/stalker/env/bin/python
Python Version: 3.7.3
Python Path:    
['/home/sddhruvo/Desktop/django/stalker/stalker_project',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/home/sddhruvo/Desktop/django/stalker/env/lib/python3.7/site-packages',
 '/home/sddhruvo/Desktop/django/stalker/stalker_project/stalker_project',
 '/home/sddhruvo/Desktop/django/stalker/stalker_project/stalker_project']
Server time:    Sat, 26 Sep 2020 12:26:55 +0000
connectionErrorat/accounts/login/
ConnectionError(:未能建立新连接:[Errno 111]连接被拒绝)原因:NewConnectionError(:未能建立新连接:[Errno 111]连接被拒绝)
申请方式:邮寄
请求URL:http://localhost:8000/accounts/login/
Django版本:3.0.10
异常类型:ConnectionError
异常值:
ConnectionError(:未能建立新连接:[Errno 111]连接被拒绝)原因:NewConnectionError(:未能建立新连接:[Errno 111]连接被拒绝)
异常位置:/home/sddhruvo/Desktop/django/stapper/env/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py,在perform_请求中,第124行
Python可执行文件:/home/sddhruvo/Desktop/django/stapper/env/bin/Python
Python版本:3.7.3
Python路径:
['/home/sddhruvo/Desktop/django/stapper/stapper_project',
“/usr/lib/python37.zip”,
“/usr/lib/python3.7”,
“/usr/lib/python3.7/lib dynload”,
“/home/sddhruvo/Desktop/django/stapper/env/lib/python3.7/site packages”,
“/home/sddhruvo/Desktop/django/stapper/stapper_project/stapper_project”,
“/home/sddhruvo/Desktop/django/stapper/stapper_project/stapper_project”]
服务器时间:2020年9月26日星期六12:26:55+0000

因此,我对我的项目使用了elastic search,这是导致此问题的原因。我在尝试创建另一个超级用户表单命令行时发现了这一点。为了解决此问题,我必须停止elastic search并再次运行它