How to access the key attributes of a model

18 views
Skip to first unread message

Alberto Sanmartin

unread,
Mar 1, 2018, 7:35:45 PM3/1/18
to Django users
This is mi code:

models.py:

@autoconnect
class Category(models.Model):
    name = models.CharField(max_length=100)
    slug = models.CharField(max_length=100, blank=True)
    description = models.CharField(max_length=200)
    creation_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.name

    def pre_save(self):
        """metodo de la clase Category para calcular el slug de una categoria"""
        self.slug = self.name.replace(" ", "_").lower()
        print(self.slug)


@autoconnect
class Post(models.Model):
    Status = ((1, "Publicado"), (2, "Borrador"), (3, "Eliminado"))
    status = models.IntegerField(choices=Status, default=2, blank=False)
    title = models.CharField(max_length=100, blank=False)
    slug = models.CharField(max_length=100, default=' ', blank=True)
    description = models.CharField(max_length=200, blank=False)
    content = models.TextField(default=" ", blank=False)
    category = models.ForeignKey(Category, default=1, blank=False)
    creation_date = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to="photos", default='/image.jpg', blank=False)
    author = models.ForeignKey(User, default=1)

views.py:

def blog(request):
    posts = models.Post.objects.filter(status=1).order_by("-creation_date")
    print(posts)
    categories = models.Category.objects.order_by("name")
    context = {
        "posts":posts,
        "categories":categories
    }
    return render(request, "blog.html", context)

urls.py:

url(r'^blog/categorias/(?P<slug>\w+)/$', views.blog_category_posts, name='blog_category_posts'),

blog.html:

{% for post in posts %}
    <a href="{% url 'detail_post' slug=post.slug %}">{{ post.title }}</a>
    <p>{{ post.description }}</p>
    <p><a href="{% url 'blog_category_posts' slug=post.category.slug %}">{{ post.category.name }}</a></p>
    <p><a href="{% url 'blog_author_posts' username=post.author.username %}">{{ post.author.username }}</a></p>
    <p>{{ post.creation_date }}</p>
{% endfor %}

When I click on the link of the category in the blog.html, I want to be able to pass the attribute slug to url, but it does not work.
Please help
Thanks in advance. 

Dylan Reinhold

unread,
Mar 1, 2018, 8:04:46 PM3/1/18
to django...@googlegroups.com
That seems right. Do you get an error, or just no data in the url?
You should also look into the slugify helper, https://docs.djangoproject.com/en/2.0/ref/utils/#django.utils.text.slugify

Dylan

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dccfe67f-c6cc-4239-91ef-2c9a85b43099%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julio Biason

unread,
Mar 2, 2018, 6:22:45 AM3/2/18
to django...@googlegroups.com
Hi Alberto,

I'm guessing `def blog(request):` is actually `blog_category_posts`, right? Because you have `<slug>` on your URL, our view will receive it as another parameter, after request. So it should be `def blog_category_posts(requests, slug):` and then you can get the slug, filter the posts and return them to the template.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dccfe67f-c6cc-4239-91ef-2c9a85b43099%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554
Reply all
Reply to author
Forward
0 new messages