0
votes

Error : AttributeError: 'BookForm' object has no attribute 'get'. im unable to debug the problem.please be helping out.thanks in advance

forms.py

from django import forms
from .models import Book

class BookForm(forms.Form):
class Meta:
    model = Book
    fields = ['title', 'sub_title', 'author', 'image', 'price', 'description', 'ISBN', 'number_of_pages','dimensions', 'interior_pages','binding','availability','genre']

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('',views.BookForm, name = 'bookform' ),
]

views.py

from django.shortcuts import render,redirect from .forms import BookForm

def bookview(request):
    if request.method == 'POST':
        form = BookForm(request.post)
        if form.is_valid():
            model_instance = form.save()
            model_instance.save()
            return redirect('/')
    else:
        form = BookForm()
        return render(request,'book.html',{'myform':form})

template

<html>
<head>
<title>edit</title>
</head>
<body>

<form method="post" >
{% csrf_token %}
{{form.as_p}}

<input type="submit" value="ok">
</form>
</body>
</html>
1

1 Answers

3
votes

In url pattern you need to link views with url path. But you are using form class here, change urls.py to this:

urlpatterns = [
    path('', views.bookview, name='bookform'),
]

Also to use auto generated fields you need to use ModelForm as form's base class:

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ['title', 'sub_title', 'author', 'image', 'price', 'description', 'ISBN', 'number_of_pages','dimensions', 'interior_pages','binding','availability','genre']

UPD

You have myform variable in view's context, so you need to use it in template instead of form:

{{myform.as_p}}