Introduction
This is an article where the main focus is about how to solve an error message. The error message appear upon executing a certain URL address. That URL address itself is a part of a Django-based application. The following is the actual display of the execution’s result :
Moreover, the following is part of the error message as it exist in the above image display :
return HttpResponseRedirect(self.get_success_url()) File "C:\programming\python\env\lib\site-packages\django\views\generic\edit.py", line 52, in get_success_url raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.") django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Provide a success_url.
Actually, the base application for the error occur above exist in another article. That is an article with the title of ‘How to Present Error Message in the Form in Django’ where it exist in this link.Basically, there is a Django-based web application which is running and triggering the error message. The following are the description of the application in short :
-
First of all, it has the following line of URL address definition in the ‘urls.py’ file :
path('test/', views.TestFormView.as_view(), name='test'),
-
Moreover, in the ‘views.py’ file, it has a class definition to accommodate the execution of the URL address definition :
class TestFormView(FormView): form_class = TestForm template_name = 'templates/test.html'
-
Furthermore, it has a form class definition to accommodate the above ‘TestFormView’ class definition :
class TestForm(forms.Form): first_name = forms.CharField(widget=forms.TextInput( attrs={'placeholder': 'First Name'}))
- Last but not least, there is a file with the name of ‘test.html’ with the following content :
{% extends 'base.html' %} {% block content %} <form action="{% url 'test' %}" method="POST"> {% csrf_token %} <div class="form-group"> <label for="exampleInputName">Name</label> <input type="text class="form-control" name="first_name" aria-describedby="name" placeholder="Name" /> </div> {% if form.errors %} {% for field in form %} {% for error in field.errors %} <div class="alert alert-danger"> <strong>{{ error|escape }}</strong> </div> {% endfor %} {% endfor %} {% endif %} <button type="submit" class="btn btn-primary">Submit</button> </form> {% endblock %}
Solving Error Message
In summary, according to the above condition, how to solve the error message ?. Basically, there is a form with one field. It is a field for ‘name’ entry. Furthermore, upon submitting the value, there will be an error message appear if the field is empty. It is as in the article with the title of ‘How to Present Error in the Form in Django’ in this link. But if there is a value, the process will then continue to the url alias of ‘test’. That url is actually does nothing to do with the input value. It depends on the logic of the process later on. But that does not enough because of an error message appear. That error message is indicating to provide a success_url. So, the following is the actual solution to do it :
-
Just open the ‘views.py’ file.
-
Provide the success_url. Define it in the TestFormView class definition. The previous content exist as follows :
class TestFormView(FormView): form_class = TestForm template_name = 'templates/test.html'
-
Add the following line of source code :
success_url = '/test/'
-
After editing the TestFormView class definition, the content of the TestFormView class definition will exist as follows :
class TestFormView(FormView): form_class = TestForm template_name = 'templates/test.html' success_url = '/test/'
-
Last but not least, just execute the page once more. The error message will finally disappear.