2

I'm a new face to Django so please be considerate to if ever my problem is something stupid. So I have been practicing Django, and currently making a CRUD, however I've run into problems with tegards to NoReverseMatch, I went through answers in stackoverflow but still I couldn't find where I went wrong. Can you help me a bit guys? Actually it was working, when I resetted the DB, somehow it had the error, maybe i moved something or whatever unlucky thing i did. I've been at it for 3 hours actually. Just this error. So please help me:

The traceback says that the NoReverseMatch is on the following url:

<a href="{%url 'deleted'%}">Delete</a>

which I properly partnered and connected with the following in my urls.py.

url(r'^delete/(?P<pk>\d+)/', Delete.as_view(), name="deleted"),

Here's the gist of the code:

deleteupdate/urls.py (Delete and Update are from models imported to the file)

urlpatterns = [

    url(r'^', views.list, name='list'),

    url(r'^delete/(?P<pk>\d+)/', Delete.as_view(), name="deleted"),

    url(r'^update/(?P<pk>\d+)/', Update.as_view(), name="updated"),

    url(r'^(?P<student_id>)/', views.detail, name='detail'),

]

main/urls.py

urlpatterns = [
    url(r'^$', include('index.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^list/', include('deleteupdate.urls')),

]

Here's the HTML:

{% if all_students %}

<ul>
    {% for user in all_users %}
    <li><a href="/list/{{ user.id }}"></a>



<button type="button">
<a href="{%url 'deleted'%}">Delete</a>
</button>


<button type="button">
<a href='{% url 'updated' %}'>Update</a>
</button>
{% endfor%}
</ul>

{% else %}
<h3>Users is empty</h3>
{% endif %}

What am I doing wrong? I think I have followed every advice I could find, but yeah it still gives me the error. Any help is appreciated. Thank you very much!

Here's the error btw:

NoReverseMatch at /list/

Reverse for 'deleted' with no arguments not found. 1 pattern(s) tried: ['list/delete/(?P\d+)/']

Ann Yoon
  • 157
  • 1
  • 2
  • 7
  • 1
    Your `deleted` URL requires you to specify a `pk` but you didn't. You need to do `{% url 'deleted' %}`. –  Nov 09 '17 at 01:11
  • @Blurp same idea, i tried his code, it worked for me using the answer i wrote below, but yeah there's a problem with the pk='user.id', the urls won't redirect, it stops at nothing, I'm quite interested why this is happening. Have I passed the pk the wrong way? Please enlighten me. – Rekt Nov 09 '17 at 03:46

3 Answers3

3

You need a <pk> to go with your url so it will match. I edited this part for you:

{% for student in all_students %}
<li><a href="/list/{{ student.id }}">{{ student.first_name }} {{ student.last_name }} ({{ student.course }})</a>

<p><a class="btn btn-lg btn-success" href="{% url "delete" pk=student.id %}"role="button">Delete</a>
            <a class="btn btn-lg btn-success" href="{% url "update" pk=student.id %}" role="button">Update</a>

 </p>

{% endfor%}
Rekt
  • 359
  • 2
  • 18
  • 1
    There shouldn't be quotes around `user.id`. –  Nov 09 '17 at 03:50
  • Thanks, but my links aren't working, any clue why this is happening? – Ann Yoon Nov 09 '17 at 03:52
  • @AnnYoon try it without the quotes around the user.id just as Blurp have suggested – Rekt Nov 09 '17 at 03:54
  • i changed it the links still aren't working. I can access the detailsview but when i click the delete and update button, nothing happens, could this be a problem with the urls? or is it a different matter? – Ann Yoon Nov 09 '17 at 04:14
  • @AnnYoon ahhh that's the issue with your HTML, look at your buttons. – Rekt Nov 09 '17 at 04:15
1

I know I'm too late to write the solution of this error,and I know you have surely resolved your error. But still I'm posting this for those who are stucking into this kind of problem. I solved this problem by just changing the HTML file's deleted button code: Just change your code from:

<button type="button">
<a href="{%url 'deleted'%}">Delete</a>
</button>

to:

<button type="button">
<a href="{%url 'deleted' pk=student.pk %}">Delete</a>
</button>
imjoymhnt
  • 1,011
  • 11
  • 14
0

I had this issue and the problem was that i forgot to put the customer id after the url

<form method="post" action="{% url 'deleteCustomer' **customer.id** %}">
mikail yusuf
  • 197
  • 3
  • 5