i want to know how to use SlugField with slugify to generate url for detail_blog page without using get_absolute_url

28 views
Skip to first unread message

Kanchan Prasad

unread,
Nov 27, 2014, 4:26:49 PM11/27/14
to django...@googlegroups.com
my model.py is
class BlogPost(models.Model):
    title        = models.CharField(max_length=100)
    text         = models.TextField()
    created_on   = models.DateTimeField(auto_now_add=True,auto_now=False)
    updated_on   = models.DateTimeField(auto_now_add=False,auto_now=True)
    submitted_by = models.ForeignKey(User)
    slug         = models.SlugField(max_length=100,unique=True)
    def __str__(self):
        return self.title
    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.title)
            super(BlogPost,self).save(*args,**kwargs)

urls.py

url(r'^profile/latest-quotation/(?P<slug>[\w-]+)/$',add_view, name='add_view'),

views.py
def add_view(request,slug):
    blog = get_object_or_404(BlogPost, slug=slug)
    com_form = CommentForm(request.POST or None)
    if com_form.is_valid():
        form = com_form.save(commit=False)
        form.blogpost = blog
        form.name = request.user
        form.save()
        return HttpResponseRedirect(reverse('socialnetwrok:add_view'))
    return render(request,'socialnetwork/detailview.html',{'blog':blog,'com_form':com_form})

my template where i am using it
        {% for quotation in latest_quotation_list %}
            <li><h2>
        <a href="{% url 'socialnetwork:add_view' title.slug %}">{{quotation.title}}</a>
            </h2>    </li>
        <p>{{quotation.text|truncatewords:100}}</p>

please help me i need help

aRkadeFR

unread,
Nov 28, 2014, 3:57:23 AM11/28/14
to django...@googlegroups.com
Hello Kanchan,

Sorry to say, I didn't get your question. I don't see any get_absolute_url
in your email.

And your code seems legit.

Could you provide more information about the problem?

Thank you,

aRkadeFR
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/45cb3726-5fd5-469b-b076-0eab453b95b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Collin Anderson

unread,
Nov 30, 2014, 1:36:16 PM11/30/14
to django...@googlegroups.com
Hi,

Is this what you're looking for?

{% url 'add_view' quotation.slug %}

Collin
Reply all
Reply to author
Forward
0 new messages