Re: How to access the key attributes of a model

2018-03-02 Thread Julio Biason
Hi Alberto,

I'm guessing `def blog(request):` is actually `blog_category_posts`, right?
Because you have `` 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 <
albertosanmartinmarti...@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\w+)/$', views.blog_category_posts,
> name='blog_category_posts'),
>
> blog.html:
>
> {% for post in posts %}
> {{ post.title }}
> {{ post.description }}
> {{ post.category.name }}
> {{ post.author.username }}
> {{ post.creation_date }}
> {% 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+unsubscr...@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+unsubscr...@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.


Re: How to access the key attributes of a model

2018-03-01 Thread Dylan Reinhold
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

On Thu, Mar 1, 2018 at 2:37 PM, Alberto Sanmartin <
albertosanmartinmarti...@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\w+)/$', views.blog_category_posts,
> name='blog_category_posts'),
>
> blog.html:
>
> {% for post in posts %}
> {{ post.title }}
> {{ post.description }}
> {{ post.category.name }}
> {{ post.author.username }}
> {{ post.creation_date }}
> {% 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+unsubscr...@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.
>

-- 
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+unsubscr...@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/CAHtg44BxTh95oR6ctHt4E8DhO2SwShkiyA0t67jg_eFFoFbofA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to access the key attributes of a model

2018-03-01 Thread Alberto Sanmartin
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\w+)/$', views.blog_category_posts, 
name='blog_category_posts'),

blog.html:

{% for post in posts %}
{{ post.title }}
{{ post.description }}
{{ 
post.category.name }}
{{ post.author.username }}
{{ post.creation_date }}
{% 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+unsubscr...@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.