Reputation: 25
I'm trying to pass data from a successful form submission to a thank you page that could then display this data. I am trying to do a reverse HttpResponseRedirect, but I keep getting this error:
NoReverseMatch at /contactform/process Reverse for 'thankyou' with arguments '(24,)' not found. 1 pattern(s) tried: [u'contactform/thankyou/$']
Here is the code I have so far:
views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .models import Contact_Object
def index(request):
template_name = 'contactform/index.html'
return render(request, 'contactform/index.html')
def thankyou(request, entered_id):
contact_info = get_object_or_404(Contact_Object, pk=contact_object_id)
template_name = 'contactform/thankyou.html'
return render(request, 'contactform/thankyou.html', {name: contact_info.name})
def process(request):
entered_name = request.POST['fullname']
entered_email = request.POST['email']
entered_message = request.POST['message']
entered = Contact_Object(name=entered_name, email=entered_email, message=entered_message)
entered.save()
entered_id = entered.id
return HttpResponseRedirect(reverse('contactform:thankyou', args=(entered.id,)))
urls.py
from django.conf.urls import url
from . import views
app_name = 'contactform'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^thankyou/$', views.thankyou, name='thankyou'),
url(r'^process$', views.process, name='process'),
]
Template for Form
<H1>Contact Form</H1>
<FORM action="{% url 'contactform:process' %}" method="post">
{% csrf_token %}
Full Name:<br>
<input type="text" name="fullname"><br>
Email:<br>
<input type="text" name="email" ><br>
Message:<br>
<textarea name="message" cols="40" rows="5"></textarea>
<input type="submit" value="Enter">
</FORM>
Template for Thank you page
<h1>Thank You</h1>
<p>Thank you, {% contact_info.name %}</p>
I'm new to working with Python/Django so I feel it's probably an obvious beginners mistake, I just can't seem to spot it.
Thanks in advance.
Upvotes: 0
Views: 74
Reputation: 5203
Your problem is right here:
return HttpResponseRedirect(reverse('contactform:thankyou', args=(entered.id,)))
the thankyou
url doesn't take any arguments.
url(r'^thankyou/$', views.thankyou, name='thankyou'),
To fix it you should change it to this:
url(r'^thankyou/([0-9]*)$', views.thankyou, name='thankyou'),
Using the reverse
function and the args
kwarg, it translates it into a usable URL like: /thankyou/24
plugging in the args provided in the args
iterable. You can also use kwargs
to pass in keywords to your url if you're using keyword groupings in your url as described below.
See here: https://docs.djangoproject.com/en/1.11/topics/http/urls/#example
Each of the regex groupings is an argument that gets passed to the view.
You can also have keyword arguments passed if you do the url like this:
url(r'^thankyou/(?P<pk>[0-9]*)$', views.thankyou, name='thankyou'),
and in a function based view you would access it by defining it in the view function signature like this:
def thankyou(request, pk=None):
Upvotes: 1