This seems like I must be missing something really simple. I'm trying to use the url command for a link. I keep getting a NoReverseMatch error. The code in the HTML looks like this:
a href="MyApp/Batch_Up_Load">Batch Upload</a
a href="{% url 'MyApp/Batch_Up_Load' %}">test</a
I'm just learning this so I duplicated the link, one in a button and one as an url. If I take out the second line the HTML loads find and the button takes me to the correct page. When I add the second line I get the NoReverseMatch error. I have it added to the installed apps in the settings page but I would think if I had done this wrong the first button wouldn't work at all.
If instead I link it to a name in the base module like this: test
it works fine. Is there some place special I need to list the MyApp application in order for python to find it when using url?
CodePudding user response:
Url
tag works with name
in path
.
For example:
urls.py
path('MyApp/Batch_Up_Load/',views.batch_upload,name='batch_upload')
In your template file
<a href="{% url 'batch_upload' %}"> batches </a>
So, it will render the desired page, url
tag is used with name
of path().
In your case:
<a href="MyApp/Batch_Up_Load/">Batch Upload</a>
It will render.
a href="{% url 'MyApp/Batch_Up_Load' %}">test</a
It will not render as url
takes name
.
Note:
: Its recommended, if you give/
at the end of your every route or path.
CodePudding user response:
Here is the traceback:
Template error:
In template C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\Resource2\Resource2\templates\index.html, error at line 13
Reverse for 'batch_upload' not found. 'batch_upload' is not a valid view function or pattern name.
3 : {% block content %}
4 :
5 : Up Load New File
6 :
7 : View All Files
8 :
9 : Batch Download
10 :
11 : Batch Upload
12 :
13 : batches
14 :
15 : Search
16 :
17 : {% endblock %}
Traceback (most recent call last): File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\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\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\Resource2\Resource2\views.py", line 6, in home return render(request, 'index.html', {}) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\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\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\loader.py", line 62, in render_to_string return template.render(context, request) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\backends\django.py", line 62, in render return self.template.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 175, in render return self._render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 958, in render_annotated return self.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\loader_tags.py", line 157, in render return compiled_parent._render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 958, in render_annotated return self.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\loader_tags.py", line 63, in render result = block.nodelist.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 1000, in return SafeString("".join([node.render_annotated(context) for node in self])) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\template\base.py", line 958, in render_annotated return self.render(context) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\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\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\urls\base.py", line 88, in reverse return resolver._reverse_with_prefix(view, prefix, *args, **kwargs) File "C:\Users\Stacey WayneM\Documents\NurseStaceyCode\Resource_Two\env\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at / Exception Value: Reverse for 'batch_upload' not found. 'batch_upload' is not a valid view function or pattern name.