当前位置: 首页 > 面试题库 >

Django-[Errno 111]拒绝连接

华旭
2023-03-14
问题内容

当我发表评论时,不要保存,崩溃(错误:[Errno 111]连接被拒绝),为什么?

views.py

import time
from calendar import month_name

from django.http import HttpResponseRedirect, HttpResponse  
from django.shortcuts import get_object_or_404, render_to_response  
from django.contrib.auth.decorators import login_required  
from django.core.context_processors import csrf  
from django.core.paginator import Paginator, InvalidPage, EmptyPage  
from django.core.urlresolvers import reverse  

from dbe.blog.models import *  
from django.forms import ModelForm  


class CommentForm(ModelForm):  
    class Meta:  
        model = Comment  
        exclude = ["post"]  


def post(request, pk):  
    post = Post.objects.get(pk=pk)  
    comments = Comment.objects.filter(post=post)  
    d = dict(post=post, comments=comments, form=CommentForm(), user=request.user)  
    d.update(csrf(request))  
    return render_to_response("post.html", d)  

def delete_comment(request, post_pk, pk=None):  
    if request.user.is_staff:  
        if not pk: pklst = request.POST.getlist("delete")  
        else: pklst = [pk]  

        for pk in pklst:  
            Comment.objects.get(pk=pk).delete()  
        return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[post_pk]))  

def add_comment(request, pk):  
    p = request.POST  

    if p.has_key("body") and p["body"]:  
        author = "Anonymous"  
        if p["author"]: author = p["author"]  
        comment = Comment(post=Post.objects.get(pk=pk))  

        cf = CommentForm(p, instance=comment)  
        cf.fields["author"].required = False  
        comment = cf.save(commit=False)  

        comment.author = author  
        notify = True  
        if request.user.username == "ak": notify = False  
        comment.save(notify=notify)  
    return HttpResponseRedirect(reverse("dbe.blog.views.post", args=[pk]))  

def mkmonth_lst():  
    if not Post.objects.count(): return []  

    # set up vars  
    year, month = time.localtime()[:2]  
    first = Post.objects.order_by("created")[0]  
    fyear = first.created.year  
    fmonth = first.created.month  
    months = []  

    for y in range(year, fyear-1, -1):  
        start, end = 12, 0  
        if y == year: start = month  
        if y == fyear: end = fmonth-1  

        for m in range(start, end, -1):  
            months.append((y, m, month_name[m]))  
    return months  

def month(request, year, month):  
    posts = Post.objects.filter(created__year=year, created__month=month)  
    return render_to_response("list.html", dict(post_list=posts, user=request.user,  
                                                months=mkmonth_lst(), archive=True))  

def main(request):  
    posts = Post.objects.all().order_by("-created")  
    paginator = Paginator(posts, 10)  
    try: page = int(request.GET.get("page", '1'))  
    except ValueError: page = 1  

    try:  
        posts = paginator.page(page)  
    except (InvalidPage, EmptyPage):  
        posts = paginator.page(paginator.num_pages)  

    return render_to_response("list.html", dict(posts=posts, user=request.user,  
                                                post_list=posts.object_list,   months=mkmonth_lst()))

models.py

from django.db import models  
from django.contrib.auth.models import User  
from django.contrib import admin  
from django.core.mail import send_mail  


class Post(models.Model):  
    title = models.CharField(max_length=60)  
    body = models.TextField()  
    created = models.DateTimeField(auto_now_add=True)  

    def __unicode__(self):  
        return self.title  


class Comment(models.Model):  
    created = models.DateTimeField(auto_now_add=True)  
    author = models.CharField(max_length=60)  
    body = models.TextField()  
    post = models.ForeignKey(Post)  

    def __unicode__(self):  
        return unicode("%s: %s" % (self.post, self.body[:60]))  

    def save(self, *args, **kwargs):  
       if "notify" in kwargs and kwargs["notify"] == True:  
       message = "Comment was was added to '%s' by '%s': \n\n%s" % (self.post,   self.author,  
                                                                         self.body)  
            from_addr = "no-reply@mydomain.com"  
            recipient_list = ["myemail@mydomain.com"]  
            send_mail("New comment added", message, from_addr, recipient_list)  

        if "notify" in kwargs: del kwargs["notify"]  
        super(Comment, self).save(*args, **kwargs)

管理员

class PostAdmin(admin.ModelAdmin):  
    search_fields = ["title"]  
    display_fields = ["title", "created"]  

class CommentAdmin(admin.ModelAdmin):  
    display_fields = ["post", "author", "created"] 

问题答案:

为了进行调试,你可以使用以下命令设置本地smtpserver:

python -m smtpd -n -c DebuggingServer localhost:1025

并相应地调整你的邮件设置:

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025


 类似资料:
  • 问题内容: 我正在尝试将Redis用作我的使用Docker Compose的Django项目的Celery的代理。我无法弄清楚我到底做错了什么,但是尽管控制台日志消息告诉我Redis正在运行并接受连接(实际上,当我这样做时,我可以看到容器正在运行),但我仍然会收到有关拒绝连接的错误消息。我什至做到了 看到回应是。 这是my中的Celery设置: 这是my中的Redis容器设置: 我还记得将容器与我

  • 我使用的是Postgres 9.1.5。JDBC在第一个实例中运行良好,但当我再次尝试时,它开始拒绝连接。例如,我使用JDBC登录到我的web应用程序,效果很好。但是,我尝试使用连接获取其他数据,它引发了一个异常:连接被拒绝。我确信我的代码没有问题,因为上次运行良好的登录JDBC也引发了一个异常:连接被拒绝。 重新启动postgres windows服务后,它再次工作。然后,登录JDBC,获取一些

  • 我试图实现一个TCP连接,在服务器端一切正常,但当我运行客户端程序(从客户端计算机)时,我得到以下错误: 服务器代码: 客户端代码:

  • 问题内容: 我是 HBase 和 Hadoop的 新手。我已经完全设置了HBase并完美启动。现在,当我尝试使用Java客户端从 p1 连接到HBase(HBase安装在 p2上 )时,它抛出了一个奇怪的异常。 问题答案: 我找到了解决方案。 通过仅从我的 主机中 删除 localhost 条目。现在我的本地主机条目就像 192.169.19.50 [这是我的hbase IP] =本地主机,而不是

  • 问题内容: 我正在尝试实现TCP连接,从服务器端一切正常,但是当我运行客户端程序(从客户端计算机)时,出现以下错误: 我尝试更改套接字号以防万一,但无济于事,有谁知道导致此错误的原因和解决方法。 服务器代码: 客户代码: 问题答案: 此异常意味着你尝试连接的IP /端口上没有侦听服务: 你试图连接到错误的IP /主机或端口。 你尚未启动服务器。 你的服务器没有监听连接。 在Windows服务器上,