get method inside class based view

26 views
Skip to first unread message

Ismail Sarenkapic

unread,
May 7, 2017, 12:21:10 PM5/7/17
to Django users

forms.py
from django import forms


from .validators import validate_url, validate_dot_com

class SubmitUrlForm(forms.Form):
    url = forms.CharField(
            label='', 
            validators=[validate_url],
            widget = forms.TextInput(
                    attrs ={
                        "placeholder": "Long URL",
                        "class": "form-control"
                        }
                )
            )
views.py
class HomeView(View):

    def get(self, request, *args, **kwargs):
        the_form = SubmitUrlForm()
        context = {
            "title": "Kirr.co",
            "form": the_form,
            "bg_image": bg_image
        }
        return render(request, "shortener/home.html", context) # Try Django 1.8 & 1.9 http://joincfe.com/youtube

    def post(self, request, *args, **kwargs):
        form = SubmitUrlForm(request.POST)
        context = {
            "title": "Kirr.co",
            "form": form
        }
        template = "shortener/home.html"
        if form.is_valid():
            new_url = form.cleaned_data.get("url")
            obj, created = KirrURL.objects.get_or_create(url=new_url)
            context = {
                "object": obj,
                "created": created,
            }
            if created:
                template = "shortener/success.html"
            else:
                template = "shortener/already-exists.html"

        return render(request, template ,context)


The question is, what is the purpose of get method here,when we have post method
is it useless in this case?If not what is function of get method?

Daniel Roseman

unread,
May 7, 2017, 1:55:47 PM5/7/17
to Django users


On Sunday, 7 May 2017 17:21:10 UTC+1, Ismail Sarenkapic wrote:
The question is, what is the purpose of get method here,when we have post method
is it useless in this case?If not what is function of get method?


Why would it be useless? The get method is for GET requests, the post method is for POST requests.
-- 
DR. 

Vijay Khemlani

unread,
May 7, 2017, 6:54:35 PM5/7/17
to django...@googlegroups.com
Assuming this view is for a path like

http://example.com/shortener

If you make a GET request to it (as in navigating to it in a browser)
it displays the form calling the "get" method in the view. When you
submit the form it usually does so to the same URL but with the POST
method.

So in this case, GET => Displays form, POST => Handles form submission
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c97e45cc-140e-4493-a697-3c214517bc99%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Ismail Sarenkapic

unread,
May 8, 2017, 2:49:50 AM5/8/17
to Django users
Got it, thank you for very good explanation.
Reply all
Reply to author
Forward
0 new messages