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
}
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)