0

This is my models:

from django.db import models

# Create your models here.
class post(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

and this is my views in django

from django.shortcuts import render
from .models import post
from django.http import HttpResponse

# Create your views here.
def post(request, id):
    data = {'post': post.objects.get(id=id)}
    return render(request, 'post/post.html', data)

I try to get post by id but error found:

AttributeError at /post/3/
'function' object has no attribute 'objects'

2 Answers 2

1

Class and Models start with upper case letters. In this case, Django is confusing your model Post with the function post.

An examplo of how do it more clearly:

>>> from blog.models import Blog, Entry
>>> entry = Entry.objects.get(pk=1)
>>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
>>> entry.blog = cheese_blog
>>> entry.save() 
Sign up to request clarification or add additional context in comments.

Comments

0

because your function name is also post so django confused.

so change your view function name post to postView.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.