如何使用Django和Python清除一些警告消息

我需要一些帮助.我在使用pylint运行代码时收到一些警告,我需要解决这些问题.我在下面解释我的代码.

W: 11, 0: String statement has no effect (pointless-string-statement)
C: 15, 4: Missing method docstring (missing-docstring)
R: 15, 4: Method could be a function (no-self-use)
W: 18, 4: String statement has no effect (pointless-string-statement)
C: 19, 4: Missing method docstring (missing-docstring)
E: 21,11: Instance of 'UserCreationForm' has no 'is_valid' member (no-member)
R: 19, 4: Method could be a function (no-self-use)
C: 28, 4: Missing method docstring (missing-docstring)
R: 28, 4: Method could be a function (no-self-use)
W: 31, 4: String statement has no effect (pointless-string-statement)
C: 32, 4: Missing method docstring (missing-docstring)
E: 34,11: Instance of 'AuthenticationForm' has no 'is_valid' member (no-member)
R: 32, 4: Method could be a function (no-self-use)
C: 38, 0: Missing function docstring (missing-docstring)
W: 41, 0: String statement has no effect (pointless-string-statement)
C: 43, 0: Missing function docstring (missing-docstring)
C: 72, 0: Missing function docstring (missing-docstring)
W: 75, 0: String statement has no effect (pointless-string-statement)
C: 77, 0: Missing function docstring (missing-docstring)
W: 98, 0: String statement has no effect (pointless-string-statement)
C:100, 0: Missing function docstring (missing-docstring)

我的python文件如下.

views.py:

""" coding: utf-8 """
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.views.generic import View
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm)
from lxml import etree
import random
import xml.dom.minidom as m

""" this class is used for user signup """

class Signup(View):
    """ this function used to get the sign up form """
    def get(self, request):
        form = UserCreationForm()
        return render(request, 'booking/signup.html', {'form': form})
    """ this function used for post the sign up data """
    def post(self, request):
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')


class AuthLogin(View):
    """ this function used to get the login form """
    def get(self, request):
        form = AuthenticationForm()
        return render(request, 'booking/login.html', {'form': form})
    """ this function used for post the login data """
    def post(self, request):
        form = AuthenticationForm(None, request.POST or None)
        if form.is_valid():
            login(request, form.get_user())
        return redirect('/')

def bmr(request):
    return render(request, 'booking/bmr.html', {})

""" this function is used for save all the data """

def insert(request):
    if request.method == 'POST':
        location_name = request.POST.get('lname')
        rname = request.POST.get('rname')
        seat = request.POST.get('seat')
        projector = request.POST.get('projector')
        video = request.POST.get('video')
        num = str(random.randint(100000000000, 999999999999))
        location_name = location_name[0:255]
        rname = rname[0:255]
        seat = seat[0:10]
        doc = m.parse("roomlist.xml")
        valeurs = doc.getElementsByTagName("roomlist")[0]
        element = doc.createElement("location")
        element.setAttribute("name", location_name)
        el1 = element.appendChild(doc.createElement("room"))
        el1.setAttribute("id", num)
        el2 = el1.appendChild(doc.createElement("roomname"))
        el2.appendChild(doc.createTextNode(rname))
        el3 = el1.appendChild(doc.createElement("noseats"))
        el3.appendChild(doc.createTextNode(seat))
        el4 = el1.appendChild(doc.createElement("projectorscreen"))
        el4.appendChild(doc.createTextNode(projector))
        el5 = el1.appendChild(doc.createElement("videoconf"))
        el5.appendChild(doc.createTextNode(video))
        valeurs.appendChild(element)
        doc.writexml(open("roomlist.xml", "w"))
    return render(request, 'booking/bmr.html', {})

def home(request):
    return render(request, 'booking/home.html', {})

""" This function is used for disply all the data """

def view_book(request):
    doc = m.parse("roomlist.xml")
    staffs = doc.getElementsByTagName("location")
    root = []
    for staff in staffs:
        lname = staff.getAttribute("name")
        roomname = staff.getElementsByTagName(
            "roomname")[0].firstChild.nodeValue
        seat = staff.getElementsByTagName("noseats")[0].firstChild.nodeValue
        project = staff.getElementsByTagName(
            "projectorscreen")[0].firstChild.nodeValue
        video = staff.getElementsByTagName("videoconf")[0].firstChild.nodeValue
        root.append(
            {'lname': lname,
             'roomname': roomname,
             'seat': seat,
             'project': project,
             'video': video})
    return render(request, 'booking/view_book.html', {'people': root})


""" This function is used to sear the data as per room name """

def search(request):
    per = []
    if request.method == 'POST':
        rname = request.POST.get('rname')
        tree = etree.parse('roomlist.xml')
        result = tree.xpath(".//roomname[text()=$rmname]/./..", rmname=rname)
        for user in result:
            per.append(
                {'roomname': user.find('roomname').text,
                 'seat': user.find('noseats').text,
                 'project': user.find('projectorscreen').text,
                 'video': user.find('videoconf').text})
    return render(request, 'booking/home.html', {'people': per})

在这里,我需要关闭最大警告消息,以便评级上升.请做必要的帮助.

最佳答案 这是一个简单的修复.你的doc字符串放错了地方.他们看起来应该更像这样:

class Signup(View):
    """ this class is used for user signup """

    def get(self, request):
        """ this class is used for user signup """
        form = UserCreationForm()
        return render(request, 'booking/signup.html', {'form': form})

    def post(self, request):
        """ this function used for post the sign up data """
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')

E: 34,11: Instance of ‘AuthenticationForm’ has no ‘is_valid’ member (no-member)

至于这个,pylint显然是错误的(当然假设你的AuthenticationForm是form.Form的一个sublcass)

R: 28, 4: Method could be a function (no-self-use)

如果您对此感到疑惑,那么pylint认为get和post不应该是Signup的方法.那是因为你没有使用自身参数,它必须是类中任何方法的第一个参数.但是pylint再一次显然是错误的,因为基于类的视图就像这样工作.您可能想要为django自定义pylint安装.或者添加这个

def get(self, request):  #pylint disable=no-self-use
点赞