def index(request, tag=None): if tag: tag_model = get_object_or_404(Tag, name=tag) urls = tag_model.url_set.all() else: urls = Url.objects.all() if request.method == "POST": form = UrlForm(request.POST) if form.is_valid(): url_data = form.cleaned_data['url'] title = form.cleaned_data['title'] tags = form.cleaned_data['tags'] try: url = Url(url=url_data, title=title) url.fill_title() url.save() url.add_tags_from_string(tags) if tag: url.add_tag(tag) except IntegrityError: pass return HttpResponseRedirect(request.path) else: form = UrlForm() return render_to_response("index.html", { 'urls': urls, 'form': form, })
def test_add_url_with_tag(self): url = Url(url="http://example.com", title="My Title") url.save() url.add_tag("test") self.assertEquals(len(url.tags.all()), 1)