Home > database >  Reverse for 'update-project' with arguments '('',)' not found. 1 patte
Reverse for 'update-project' with arguments '('',)' not found. 1 patte

Time:01-28

Trying to create an update function for a Project Model in Django but i've run into a problem. Here's what i have so far

update view function

@login_required
def updateProject(request, pk):
    project = Project.objects.get(id=pk)
    form = ProjectForm(instance=project)
    
    if request.method == 'POST':
        project.name = request.POST.get('name')
        project.description = request.POST.get('description')
        project.save()
        
        return redirect('project', pk=project.id)
    
    context = {'form': form, 'project': project}
    return render(request, 'projects/project_form.html', context)

This is how I'm calling it in the template

<li><a href="{% url 'update-project' project.id %}">Edit</a></li>

and this is what the urlpattern is

path('update-project/<int:pk>/', views.updateProject, name='update-project'),

What am I missing?

CodePudding user response:

To redirect to another URL I usually use:

return HttpResponseRedirect(reverse('update-project', args=(project.id,)))

instead of your

return redirect('project', pk=project.id)

which probably causes your error after POST

  • Related