Reverse for 'update-project' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-project/(?P<pk>[0-9]+)/\\Z']

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?

Is there any other view or template (or line in this template) attempting to reverse the update-project url?

Note, it’s generally helpful to post the complete traceback to help ensure we’re looking at the right code.


Traceback (most recent call last):
  File "C:\Users\mikha\issue_env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\mikha\mable\projects\views.py", line 31, in project
    return render(request, 'projects/project.html', context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\backends\django.py", line 62, in render
    return self.template.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\template\defaulttags.py", line 472, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\urls\resolvers.py", line 828, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /projects/1/
Exception Value: Reverse for 'update-project' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-project/(?P<pk>[0-9]+)/\\Z']

Here’s the traceback

What view is being executed for the url /projects/1? That’s the view causing this error.

1 Like

Hello Sir i have the same issue can you help me please
I’ve been staring at the error for 24hrs

My Views.py:
def update_project(request, project_id):
project = Project.objects.get(pk=project_id)
context = {‘project’:project}
return render(request, ‘wiztask/update_project.html’,context)

Urls.py:
path(‘update_project/str:project_id/’, views.update_project, name=‘update-project’),

template:
href=“{% url ‘update-project’ project.id %}”

models.py:
class Project(models.Model):
STATUS = (
(‘Pending’,‘Pending’),
(‘On Going’,‘On Going’),
(‘Done’,‘Done’),
)
project_id = models.AutoField(primary_key=True, default=None)
leader = models.ForeignKey(User, default=None,on_delete=models.CASCADE)
title = models.CharField(max_length=50)
version = models.FloatField()
about = models.TextField()
deadline = models.DateField(null=True, blank=True)
status = models.CharField(max_length=50, choices=STATUS, blank=True)

Welcome @jzfanai !

If you’re looking for assistance with a specific issue, please open a new topic for it. Post this information in your new post.

Also note that I have deleted your other post on this topic. Do not post the same issue multiple times. Create one topic with the necessary information. (See the “Keep it Tidy” section of the forum FAQ.)

Side note: When posting code, templates, or other preformatted text here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.