Enoch
Enoch

Reputation: 41

Pylint errors in Django, Pylint(E0611:no-name-in-module) and Pylint(E1101:no-member)

It seems like my models.py, forms.py, urls.py and views.py are not recognizing the elements in each other and I don't understand what could be going wrong. I'm just starting the project (CS50W Project4-Network) and I noticed the problem when I tried to render the model form and the textarea field of the form linked to the model wouldn't show on the browser, it only shows when I click submit, as a missing field to be filled, I'm not sure if its because of the same pylint errors I'm getting or something else.

Here is my models:

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    pass

class Post(models.Model):
    body = models.TextField()
    date = models.DateTimeField(auto_now_add = True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="author", default=None)

    def __str__(self):
        return f"{self.body} by {self.author} at {self.date}"

The forms.py:

from django import forms
from django.forms import ModelForm

from .models import Post

class PostForm(ModelForm):

    class Meta:
        model = Post
        fields = ["body"]

        widgets = {
            "body": forms.Textarea(attrs={'class': 'form-control col-md-5 col-lg-6'}),
        }

The views

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .forms import PostForm

from .models import User, Post

**I left out logging in routes**

@login_required(login_url='login')
def create_post_page(request):
    return render(request, "network/create_post.html")

def create_post(request):
    posted = False
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.author = request.user
            instance.save()
            return HttpResponseRedirect("/create_post?posted=True")
    else:
        form = PostForm()
        if "posted" in request.GET:
            posted = True
    return render(request, "network/create_post.html", {
        "posted": posted,
        "form": form
    })

The urls:

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("create_post", views.create_post, name="create_post"),
    path("create_post_page", views.create_post_page, name="create_post_page")
    ]

The template code where the form is not showing, only showing when I click submit as a missing field to be field

{% extends "network/layout.html" %}

{% block body %}

{% if posted %}
 
{% else %}
<h2>Create a new post</h2>
<form action="{% url 'create_post' %}" method = "POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-secondary" type="submit" value="Post">
</form>
{% endif%}
{% endblock %}

The problems vscode keep raising:

No name 'Post' in module 'network.models'Pylint(E0611:no-name-in-module)

Module 'network.views' has no 'create_post' memberPylint(E1101:no-member)

Module 'network.views' has no 'create_post_page' memberPylint(E1101:no-member)

Somebody help me please, I feel like its a small problem but its driving me nuts because I can't figure out why.

Upvotes: 0

Views: 608

Answers (1)

David S.
David S.

Reputation: 465

Make sure you load the plugin "pylint_django" when pylint is called, and that you pass it the correct value for django-settings-module. To do this, create a file pylintrc in your project folder and give it the following contents:

[MAIN]
load-plugins = pylint_django,
django-settings-module = mysite.settings

Upvotes: 1

Related Questions