0

My webpage looks like the following:

enter image description here

When I press "Submit" button, the page is "redirected" to some different page. If I don't specify get_absolute_url method in my models.py, I will get ImproperlyConfigured error. If I set where to redirect to in my models.py, upon clicking the Submit button, it will be directed to that page. What I don't understand is, how does only the Submit button redirected to whatever the link I set up on my models.py? Why clicking on the links on the sidebar doesn't redirect to it?

Why does get_absolute_url method acts only upon clicking Submit button, but not the others?

practice_add_well.html

<!DOCTYPE html>
{% extends "base.html" %}
  {% block content %}

    <h1>Test Page for BHA</h1>

    <form method="POST">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" class='btn btn-primary' value="Submit">
    </form>

  {% endblock %}

models.py

from django.db import models
from django.urls import reverse

class WellInfo(models.Model):
    name = models.CharField(max_length=100)
    region_location = models.CharField(max_length=100)
    spud_date = models.CharField(max_length=100)
    well_bore = models.CharField(max_length=100)
    rig_name = models.CharField(max_length=100)
    status = models.CharField(max_length=100)

    def get_absolute_url(self):
        return reverse(# some link....)

views.py

from django.shortcuts import render
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView

from . import models

class WellInfoCreateView(CreateView):
    template_name = 'practice_add_well.html'
    context_object_name = 'bha_inputs'
    model = models.WellInfo
    fields = '__all__'
Eric Kim
  • 2,493
  • 6
  • 33
  • 69

2 Answers2

1

the CreateView class inherit success_url property from FormMixin by default it will get url from model

class WellInfoCreateView(CreateView):
    template_name = 'practice_add_well.html'
    context_object_name = 'bha_inputs'
    model = models.WellInfo
    fields = '__all__'
    success_url = 'url'

see class-based-views for more details

Naqib Hakimi
  • 874
  • 4
  • 15
  • ok, I think I was mistaken about Create view's success_url attribute. But still, how does Django know when to direct to success_url? Why does it not direct to sucess_url upon clicking other buttons? – Eric Kim Jul 18 '18 at 22:18
  • it has noting to do with buttons , a form submit button will sent POST request to the Django that's how it's knows what to do you can submit a form with any button but that is another topic – Naqib Hakimi Jul 18 '18 at 22:23
  • so, as long as the type="submit" button is inside
    , it will send POST request?
    – Eric Kim Jul 18 '18 at 22:30
  • 1
    yes, but there is also other ways to submit the form with other button Ex : https://stackoverflow.com/questions/1200266/submit-a-form-using-jquery – Naqib Hakimi Jul 18 '18 at 22:34
0

I am also learning Django currently and found this question as I was asking it myself. I did a bit of further reading and think this is the answer to your question:

Note that within your views, you are inheriting from CreateView. Now, when this view is run, i.e. when you submit your form, there are two possibilities: a successful submission or an error. If the former, then you need to know where to point following the submission. In this case, it is the variable get_absolute_url, which you can set to any url you like. Thus, when you successfully submit the form, you will be taken to this url. Otherwise, if there is an error when you try to submit the form, you will remain on the form page until submitted successfully.

I think this explains what get_absolute_url does and why other links on the page don't also take you to this url.