Monday, November 23, 2015

redirecting to home page instead of given route

I'm new to django.
I'm trying to save bookmark collected from form . But it is not getting saved in DB and also it is redirecting to home page



def bookmark_save_page(request):
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            # Create or get link.
            link, dummy = Link.objects.get_or_create(
            url=form.cleaned_data['url']
            )
            # Create or get bookmark.
            bookmark, created = Bookmark.objects.get_or_create(
            user=request.user,
            link=link
            )
            # Update bookmark title.
            bookmark.title = form.cleaned_data['title']
            # If the bookmark is being updated, clear old tag list.
            if not created:
                bookmark.tag_set.clear()
            # Create new tag list.
            tag_names = form.cleaned_data['tags'].split()
            for tag_name in tag_names:
                tag, dummy = Tag.objects.get_or_create(name=tag_name)
                bookmark.tag_set.add(tag)
            # Save bookmark to database.
            bookmark.save()
            return HttpResponseRedirect(
                '/user/%s/' % request.user.username
                 )
    else:
        form = BookmarkSaveForm()
    variables = RequestContext(request, {
            'form': form
            })
    return render_to_response('bookmark_save.html', variables)



URL's:


urlpatterns = [
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', main_page),
    url(r'^sec/$', secondary_page),
    url(r'^user/(\w+)/$', user_page),
    url(r'^login/$', 'django.contrib.auth.views.login'),
    url(r'^logout/$', logout_page),
    url(r'^register/$', register_page),
    url(r'^register/success/$', TemplateView.as_view(template_name="registration/register_success.html")),
    url(r'^save/$', bookmark_save_page),
]


models:

class Link(models.Model):
    url = models.URLField(unique=True)
    def __unicode__(self):
        return self.url
   
class Bookmark(models.Model):
    title = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    link = models.ForeignKey(Link)
    def __unicode__(self):
        return '%s , %s ' %(self.user.username, self.link.url)
   
class Tag(models.Model):
    name = models.CharField(max_length=64, unique = True)
    bookmarks = models.ManyToManyField(Bookmark)
    def __unicode__(self):
        return self.name

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b93c4d54-ca16-4f21-8f0c-3953b6a53ad3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment