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

How to clear some warning messages using Django and 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})

这里我需要关闭最大的警告信息,这样评级就会上升。请提供必要的帮助。

这是一个简单的修复。您的文档字符串放错地方了。它们应该看起来更像这样:

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 显然是错误的(当然假设你的 AuthenticationFormforms.Form 的子类)

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

如果你对此感到疑惑,pylint 认为 getpost 不应该是 Signup 的方法。那是因为您没有使用 class 中任何方法的第一个参数所必需的 self 参数。但是再一次 pylint 显然是错误的,因为基于 class 的视图正是这样工作的。你可能想为 django 定制你的 pylint 安装。或添加这个

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