I started testing and studying Django recently and I'm building some projects to learn and test it. I tried looking for answers in the documentation but was unable to find any, if you can send me the documentation reference to solve this with your solution/tip, I'd appreciate it.
I'm trying to pass an argument through the URL in Django, the url changes and the int appears on the browser when redirecting (i.e. http://127.0.0.1:8000/edit-contact/1 - 1 being put dinamically from the "redirecting") but, apparently, the view function can't recognize it.
button I'm using to redirect and send the argument
<a href="{% url 'edit-contact' contato.id%}" >
Edit contact
</a>
urls.py file:
urlpatterns = [
path('', views.index, name='index'),
path('busca/', views.search, name='busca'),
path('<int:contato_id>', views.details, name='detalhes'),
path('edit-contact/<int:contato_id>',
views.edit_contact, name='edit-contact'),
]
view function I'm using to capture the request
@login_required(redirect_field_name='login')
def edit_contact(request, contato_id):
if request.method != 'POST':
form = ContatoForm()
return render(request, 'contatos/detalhes.html', {'form': form, 'contato_id': contato_id})
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/edit-contact/1
Django Version: 3.2.9
Python Version: 3.10.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'contatos.apps.ContatosConfig',
'accounts.apps.AccountsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template D:\Gabriel\Codando\studying\django_learning\DjangoProj_agenda\templates\base.html, error at line 0
Reverse for 'edit-contact' with no arguments not found. 1 pattern(s) tried: ['edit\\-contact/(?P<contato_id>[0-9] )$']
1 : {% load static%}
2 :
3 : <!DOCTYPE html>
4 : <html lang="pt">
5 : <head>
6 : {% include 'partials/_head.html' %}
7 :
8 : <title>{% block 'title' %}{% endblock %}</title>
9 : </head>
10 :
Traceback (most recent call last):
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:\gabriel\codando\studying\django_learning\DjangoProj_agenda\contatos\views.py", line 76, in edit_contact
return render(request, 'contatos/detalhes.html', {'form': form, 'contato_id': contato_id})
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 312, in render
return nodelist.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /edit-contact/1
Exception Value: Reverse for 'edit-contact' with no arguments not found. 1 pattern(s) tried: ['edit\\-contact/(?P<contato_id>[0-9] )$']
CodePudding user response:
Does it put a "/" after the 1? On your browser url I.e 127.1.1.8000/edit-contact/1/
CodePudding user response:
send your models.py file it could be a spelling error