Friday, March 2, 2018

Re: How to access the key attributes of a model

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.

On Thu, Mar 1, 2018 at 7:37 PM, Alberto Sanmartin <albertosanmartinmartinez@gmail.com> wrote:
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. 

--
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 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

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEM7gE2O3Yq32-U1BxZJtqOtVmP%2BCdyZTdk6itk9Fz%3DShxs-Ow%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment