I feel kinda bad using my first post as a cry for help but hey, im certainly not the first lulz, anyway, im teaching myself python/django and im really stuck atm and ive been slogging through problems myself lately and wasting a lot of time doing it and this one has me stuck.
Im getting the error: NoReverseMatch at /messages/newreply/1/ Reverse for 'newreply' with arguments '('',)' not found. 1 pattern(s) tried: ['messages/newreply/(?P<post_id>[0-9] )/\Z']
This is my url file;
app_name = 'board'
urlpatterns = [
path('', views.index, name='index'),
path('<int:post_id>/', views.postdetail, name='detail'),
path('newmsg/', views.newmsg, name='newmsg'),
path('newreply/<int:post_id>/', views.newreply, name='newreply')
view
def newreply(request, post_id):
post = Post.objects.get(id=post_id)
if request.method != "POST":
form = ReplyForm()
else:
form = ReplyForm(data=request.POST)
if form.is_valid():
newreply= form.save(commit=False)
newreply.post = post
newreply.save()
return redirect('board:index')
context = {'form':form}
return render(request, 'board/newreply.html', context)
template;
{% extends 'pages/base.html' %}
{% block content %}
<p>Add your reply here</p>
<form action="{% url 'board:newreply' post.id %}"method ='post'>
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">Add post</button>
</form>
{% endblock content %}
Ive tried so many things now im not even sure where i began anymore so id really appreciate any help, especially knowing why its actually happening as ive read through a few posts with the same type of error but trying the solutions posted has produced other errors for me.
Thank you!
CodePudding user response:
It is indeed a useless and annoying error. Usually, it means you haven't passed something in the context of the request. In this case, the post
object.
context = {'form':form, 'post': post}
return render(request, 'board/newreply.html', context)