I am learning Django and have one question.
I have done a feedback form and I need to redirect the user to the same page after the feedback form confirmation. Below is the code:
models.py
class Feedback(models.Model):
title = models.CharField(max_length=255)
text = models.TextField(max_length=5000)
user_name = models.CharField(max_length=255)
user_lastname = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
send_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def get_absolute_url(self):
return
urls.py
url(r'^feedback$',views.FeedbackSendForm.as_view(), name='feedback'),
views.py
class FeedbackSendForm(CreateView):
model = Feedback
fields = [
'title',
'text',
'user_name',
'user_lastname',
'email',
]
template_name = 'feedback.html'
feedback.html
<form method="post">
{% csrf_token %}
{% for field in form %}
<span class="text-danger">{{ field.errors }}</span>
<div>
<label class="control-label">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
<button type="submit">Submit</button>
</form>
How can I fix this?
If you want to override the
get_absolute_url
for your model, the following code can help you:The absolute URL for any
Feedback
object will be the viewFeedbackSendForm
. That is specified by passing the view namefeedback
toreverse
.