2017-06-21 42 views
2

我需要一些帮助。我在使用pylint运行代码时遇到了一些警告,我需要解决这些问题。我在下面解释我的代码。如何使用Django和Python清除一些警告消息

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

在这里,我需要关闭最大警告消息,以便进行评分起来。请做必要的帮助。

回答

6

这是一个细微的修复。您的文档字符串错位。他们应该看起来更像是这样的:

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: 'AuthenticationForm' 的实例没有 'is_valid' 成员(无成员)

至于这一次,pylint的显然错(当然假设你AuthenticationFormforms.Form一个sublcass)

R:28,4:方法可能是一个功能(无自主使用)

如果您对此有疑问,pylint认为getpost不应该是Signup的方法。这是因为您没有使用self参数,该参数是类中任何方法的第一个参数。但是再一次,pylint显然是错误的,因为基于类的视图完全像这样工作。你可能想为django自定义你的pylint安装。或者加入这个

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