0
votes

I'm new to django and trying to complete a crud project, however I'm running into problems with updating and deleting. I have tried a numerous ways of troubleshooting, but I'm keep coming up short. I have provided the error message for the update portion of it. So I'm seeking advice.

Error Message: RuntimeError at /update/1

Below is a the code for urls, views, and html.

URL urlpatterns = [

path('admin/', admin.site.urls),
path('add/', views.add, name="add"),
path('', views.show, name="show"),
path('update/<update_id>/', views.update, name="update"),
path('delete/<delete_id>/', views.delete, name="delete"),    

]

Views

def add(request):

form = StudentForm(request.POST or None)
#student = Student.objects.all()
if form.is_valid():
    form.save()
return render(request, 'add.html', {'form': form})

def show(request):

student = Student.objects.all()
return render(request, 'show.html', {'student': student})

def update(request,update_id):

student = Student.objects.get(id=update_id)
form = StudentForm(request.POST, instance=student)
if form.is_valid():
    form.save()
    return HttpResponseRedirect('/')
return render(request, 'update.html', {'student':student})

def delete(request,delete_id):

student = Student.objects.get(id=delete_id)
student.delete()
return HttpResponseRedirect('/')
**Update Html:** {% extends 'base.html' %} {% block content %}
Update {% csrf_token %} ID Name Contact
Update Record Show Details {% endblock content %} It will greatly appreciate for some guidance, maybe I'm not seeing something. Thank you in advance
1
Update wouldn't use form.save(), I think. There's a method to update an instance in the database - Abhinav Mathur

1 Answers

0
votes

Do you have any more information on the error or is "RuntimeError at /update/1" All the output you get?

In any case I will try to help:

  1. For your update view I would do differently: I normally use get_object_or_404 to raise an error in case I cannot find the given model instance. Then I would do a conditional check to see if the request is POST(user has sent update form data to the server) or if it's GET, user has just requested the form prior to update the data.

As far I see you are not taking the GET into account. Also the form should be included in the context. I

This is how my update function would look:

def update_student(request, update_id):
    student = get_object_or_404(student, id=update_id)

    if request.method != 'POST':
        form = StudentForm(instance=student)

    else:
        form = StudentForm(instance=student, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')

    context = {'form': form, 'student': student}
    return render(request, 'update.html', context)
  1. For you delete view the process is similar, I would again user get_object_or_404. I would make sure the request is POST, otherwise no need to delete and would return render(request, template, {}) at the end in order to render the page correctly. I see that you dont pass a template in your function, u need to.

This is how I would do the delete function:

def delete(request, delete_id):
    template = 'delete.html'
    student = get_object_or_404(student, id=delete_id)

    if request.method == 'POST':
        student.delete()
        return HttpResponseRedirect('/')

    return render(request, template, {})

One more thing for a great beginner's introduction to CRUD on function-based views in Django u can check this book: http://bedford-computing.co.uk/learning/wp-content/uploads/2015/10/No.Starch.Python.Oct_.2015.ISBN_.1593276036.pdf

Project Section > Web Applications in there you will find a tutorial app made on Django using function-based views and explained much better than the original django documentation.