girlshostel=GirlsHostel.objects.create(owner, hostel, address,phone,teliphone,
facilities,images)
girlshostel.save()
error:
'function' object has no attribute 'objects'
Talk Programming , Career, Mental Health, Talk Personal Finance ❤️ Post a query and receive responses ✅
Post a query and receive responses. Ask anything, Ask Mitra ❤️
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
the
GirlsHostel
is what?you imported from model.py?
model class ho bhani
.objects
chai hunai parcha.. so confirm once..you could have directly done this
girlshostel = models.GirlsHostel.objects.......
include screenshots of errors and that portion of code in reply.. because i have not enough info to assist you
what a code highlighter code katai cha ani highlighting katai cha.. already edited more than 3 times fixing that
you dont have owner field in your GirlsHostel Model
models.py
class GirlsHostel(models.Model):
owner = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
hostel=models.CharField(max_length=70)
address=models.CharField(max_length=70)
phone=models.IntegerField()
teliphone=models.IntegerField()
facilities=models.TextField(max_length=2000)
images=models.ImageField(upload_to=”images”,default=”xd”)
views.py
def GirlsHostel(request):
if request.user.is_authenticated:
if request.method==”POST”:
owner=request.POST[‘Owner’]
hostel=request.POST[‘Hostel’]
address=request.POST[‘Address’]
phone=request.POST[‘Phone’]
teliphone=request.POST[‘Teliphone’]
facilities=request.POST[‘Facilities’]
images = request.FILES[‘Images’]
girlshostel=GirlsHostel(owner=owner, hostel=hostel, address=address, phone=phone,
teliphone=teliphone,
faciliti=facilities, images=images)
girlshostel.save()
return redirect(‘/home’)
else:
return render(request,’post.html’)
else:
return redirect(‘/login’)
Always write python class name in PascalCase, For example, “GirlsHostel” and function name in lower case with separated by underscore like this “def girls_hostel(request):”.
And remember that naming convention is also important in every language which makes your code easily readable and understandable by the machine itself. You wrote the same name for the model and function which is causing a problem because the Model name and view name shouldn’t be the same in Django.
Thank you so much Sir ! Bug is fix now .