/ ask /模块'django.db.models'的AttributeError没有属性'Student'

时间:2019-05-16 01:16:01

标签: django python-3.x django-models

我在Django中收到以下错误: 谁能帮我 Pic from Visual Studio 模块'django.db.models'没有'学生'成员pylint(无成员) 我在Django中收到以下错误: 谁能帮我 Pic from Visual Studio 模块'django.db.models'没有'学生'成员pylint(无成员)

Request Method: POST
Request URL:    http://127.0.0.1:8000/ask/
Django Version: 2.2.1
Exception Type: AttributeError
Exception Value:    
module 'django.db.models' has no attribute 'Student'
Python Version: 3.7.3

这是生成错误的地方: views.py


from django.shortcuts import render, get_object_or_404
from .models import post
from .models import Lost
from .models import Student
from social import forms
from django.db import models



def home (request) :
    context = {
        'titel': 'homepage',
        'posts': post.objects.all()
    }
    return render (request, 'site.html', context) 


def post_detail(request, post_id):
    post= get_object_or_404(Lost, pk=post_id)
    context = {
        'title': post,
        'post': post,
    }

    return render(request, 'details.html', context)



def Register(request):

  form_data=forms.UserRegistrar(request.POST or None)
  msg=''
  if form_data.is_valid():
       student=models.Student()
       student.first_name=form_data.cleaned_data['first_name']
       student.last_name=form_data.cleaned_data['last_name']
       student.save()
       msg='data is saved'

  context={
        'formregister':form_data,
      'msg':msg
    }
  return render(request,'ask.html',context)

forms.py

from django import forms
from django.db import models

class UserRegistrar(forms.Form):
    first_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'form-control'} ))
    last_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'form-control'}  ))

models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone


class post(models.Model):
    title = models.CharField(max_length=10)
    content = models.TextField(max_length=30)
    post_date = models.DateTimeField(default=timezone.now)
    post_update = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-post_date', )


class Lost(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=300)
    post_date = models.DateTimeField(default=timezone.now)
    post_update = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-post_date', )    


class Student(models.Model):
  first_name=models.CharField(max_length=15)
  last_name=models.CharField(max_length=15)
  age=models.IntegerField(default=15)
  date_birth=models.DateTimeField()
  def __str__(self):
    return self.first_name

1 个答案:

答案 0 :(得分:0)

解决方案: 将student=models.Student()更改为student=Student()