Abu RayhaN
Abu RayhaN

Reputation: 459

userpost() got an unexpected keyword argument 'instance'

I am practicing a little CRUD project in django.

here is the views.py of crudproject

from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect, render

from .models import userpost
from .forms import customerform
#creating postdate
def create(request):
    form= customerform()
    if request.method=='POST':
        Form=customerform(request.POST)
        if Form.is_valid():
            Form.save()
    Form={}
    context ={'form':form}
    return render(request,'create.html',context)
#reading the post
def read(request):
    user_data=userpost.objects.all()
    context ={ 'user_data':user_data}
    return render(request,'read.html',context)
#Updating the post
def update(request,pk):
    get_user_data=get_object_or_404(userpost,pk=pk)
    form= userpost(instance=get_user_data)
    if request.method=='POST':
        form=userpost(request.POST,isinstance=get_user_data)
        if form.is_valid():
            form.save()
            messages.success(request,'User data has been Updated')
            return redirect('read')
    context={'form':form}
    return render(request,'update.html',context)

#deleting the post
def delete(request,pk):
    get_user=get_object_or_404(userpost,pk=pk)
    get_user.delete()
    messages.error(request,'User deleted')
    return redirect('/')

urls.py of crud project

from django.urls import path
from .import views
urlpatterns = [
    path('new/',views.create,name='create'),
    path('update<int:pk>/',views.update,name='update'),
    path('delete/<int:pk>/',views.delete,name='delete'),
    path('',views.read,name='read')

]

but the server says

TypeError at /update8/
userpost() got an unexpected keyword argument 'instance'
Request Method: GET
Request URL:    http://localhost:8000/update8/
Django Version: 3.2.8
Exception Type: TypeError
Exception Value:    
userpost() got an unexpected keyword argument 'instance'
Exception Location: C:\Users\ITS\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py, line 503, in __init__

please help me to figure this out.it would be so much helpful if you give me a little explaination. Thanks in advance

Upvotes: 2

Views: 1121

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

In your update view, you use userpost as a model, not a form. You thus should (and perhaps first define) a form that works on a userpost, so:

def update(request,pk):
    get_user_data = get_object_or_404(userpost,pk=pk)
    if request.method=='POST':
        #         ↓ work with a form
        form = UserPostForm(request.POST, request.FILES, instance=get_user_data)
        if form.is_valid():
            form.save()
            messages.success(request,'User data has been Updated')
            return redirect('read')
    else:
        #         ↓ work with a form
        form= UserPostForm(instance=get_user_data)
    context={'form':form}
    return render(request,'update.html',context)

In the urls.py, you also forgot a slash (/) between update and the primary key:

urlpatterns = [
    # …
    #     slash ↓
    path('update/<int:pk>/',views.update,name='update'),
    # …
]

Upvotes: 1

Related Questions