“经理”对象没有“有效”属性

时间:2016-07-04 16:30:29

标签: python html django sqlite

我想为我的表单创建一个搜索字段... 请帮忙

models.py

from __future__ import unicode_literals

from django.db import models
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone

from django.utils.text import slugify

class PostManager(models.Manager):
    def active(self, *args, **kwargs):
        # Post.objects.all() = super(PostManager, self).all()
        return super(PostManager, self).filter(draft=False).filter(timestamp__lte=timezone.now())

class Post(models.Model):

    from1 = models.CharField(max_length=20)
    type_of_truck = models.CharField(max_length=20)
    date = models.CharField(max_length=20)
    weight = models.DecimalField( max_digits=5, decimal_places=2)
    Material_Name = models.CharField(max_length=20)
    To = models.CharField(max_length=20)
    Number_Of_Truck = models.CharField(max_length=20)
    Time = models.CharField(max_length=20)
    Volume = models.CharField(max_length=20)
    Material_Type = models.CharField(max_length=20)
    #updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    slug = models.SlugField(unique=True)


    def __unicode__(self):
        return self.from1

    def __str__(self):
        return self.from1

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})



def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = Post.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)



pre_save.connect(pre_save_post_receiver, sender=Post)


    # def __unicode__(self):
        # return self.user.username

而我的views.py

from django.shortcuts import render, render_to_response, redirect, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import auth
from django.contrib.auth import authenticate, get_user_model, login, logout
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
# from personal.forms import MyRegistrationForm
from personal.forms import *
from django.template import RequestContext
from django.views.decorators.csrf import csrf_protect


from .forms import PostForm
from .models import Post





def index(request):
    return render(request, 'before_login/index.html')

def deal(request):
    queryset_list = Post.objects.active() #.order_by("-timestamp")
    if request.user.is_staff or request.user.is_superuser:
        queryset_list = Post.objects.all()

    query = request.GET.get("q")
    if query:
        queryset_list = queryset_list.filter(
                Q(from1__icontains=query)|
                Q(To__icontains=query)|
                Q(Material_Type__icontains=query) |
                Q(Volume__icontains=query)
                ).distinct()
    context = {
        "object_list": queryset, 
        "title": "List",
        "page_request_var": page_request_var,
        "today": today,
    }
    return render(request, 'before_login/deal.html', context)

def post_detail(request, id=None): #retrieve
    #instance = Post.objects.get(id=1)
    instance = get_object_or_404(Post, id=id)
    context = {
        "title": instance.from1,
        "instance": instance,
    }
    return render(request, "loggedin_load/post_detail.html", context)

def registration(request):
    return render(request, 'before_login/registration.html')

def truckprovider(request):
    return render(request, 'before_login/truckprovider.html')

def loadprovider(request):
    return render(request, 'before_login/loadprovider.html')

@csrf_protect
def register_view(request):
    title = "Register"
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['password1'],
            email=form.cleaned_data['email']
            )
            return HttpResponseRedirect('/register/success/')
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {
    'form': form,
    'title': title
    })

    return render_to_response(
    'registration/form.html',
    variables,
    )

def register_success(request):
    return render_to_response(
    'registration/success.html',
    )

def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')

@login_required
def home(request):
    return render_to_response(
    'home.html',
    { 'user': request.user }
    )
def post_load(request):
    return render(request, 'loggedin_load/post_load.html')


def profile_view(request):
    return render(request, 'loggedin_load/profile.html')

def profile_settings(request):
    return render(request, 'loggedin_load/profile_settings.html')

def live_bids(request):
    return render(request, 'loggedin_load/live_bids.html')

def active_deals(request):
    return render(request, 'loggedin_load/active_deals.html')

def post_create(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        print(form.cleaned_data.get("from1"))
        instance.save()
    context = {
        "form": form,
    }
    return render(request, "loggedin_load/post_load.html", context)

    return render(request, 'loggedin_load/live_bids.html')`

我能够通过创建表单在我的数据库中输入值,但我无法为其创建搜索字段,这可以显示搜索结果... 善意的帮助将受到高度赞赏...谢谢

1 个答案:

答案 0 :(得分:2)

您的Post.objects.active()在默认模型管理器中不存在,这就是您收到该错误消息的原因。您已创建自定义模型管理器,但您没有使用它。您需要将其设置为Post模型:

class Post(models.Model):

    from1 = models.CharField(max_length=20)
    type_of_truck = models.CharField(max_length=20)
    date = models.CharField(max_length=20)
    weight = models.DecimalField( max_digits=5, decimal_places=2)
    Material_Name = models.CharField(max_length=20)
    To = models.CharField(max_length=20)
    Number_Of_Truck = models.CharField(max_length=20)
    Time = models.CharField(max_length=20)
    Volume = models.CharField(max_length=20)
    Material_Type = models.CharField(max_length=20)
    #updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    slug = models.SlugField(unique=True)
    # Manager
    objects = PostManager()


    def __unicode__(self):
        return self.from1

    def __str__(self):
        return self.from1

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

Here你会找到Django的自定义管理员文档。