def homepage(request): """ This view is responsible for displaying the landing page with the shortening form. This page has three flows: first -- post method for accepting the url to be shortened second -- if not post show form third -- error in submitted form Keyword arguments: """ if request.method == 'POST': form = ShortenForm(request.POST) if form.is_valid(): #print form.cleaned_data url = form.cleaned_data['url'] url_shortened = form.cleaned_data['url'] site = strip_to_domain(url) result = make_url_model(url, site) request.session['url_short'] = result.url_shortened return HttpResponseRedirect('/thanks/') else: form = ShortenForm() return render (request, 'index.html', {'form':form})
def index(request): current_site = Site.objects.get_current() data = {} if request.method == 'POST': form = ShortenForm(request.POST) if form.is_valid(): try: link = Link.objects.filter(url=form.cleaned_data['url'], is_autoslug=True)[0] except IndexError: link = form.save() link.users.add(request.user) link.save() # success data data.update({ 'success': True, 'long_url': form.cleaned_data['url'], 'short_url': 'http://%s/%s' % (current_site.domain, link.slug) }) else: # allow pre-populating url with GET (from bookmarklet) initial_url = request.GET.get('url', '') # no circular bookmarking... if initial_url.startswith('http://%s' % current_site.domain): initial_url = '' form = ShortenForm(initial={'url': initial_url}) data.update({'form': form}) return render(request, 'shortener/index.html', data)