Advertisement
ramos680

DJAGNO FK/RELATE_NAME

May 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. class New(models.Model):
  2.     title = models.CharField(max_length=160, blank=True, null=True)
  3.     thumbnail = models.ForeignKey('images.Images', on_delete=models.CASCADE, related_name='imagesrelate', null=True, blank=True)
  4.  
  5.     def __str__(self):
  6.         return self.title
  7.  
  8. -----------------------------------------------2 MODEL-----------------------------------------------------
  9.  
  10. class Images(models.Model):
  11.     image_name = models.CharField(max_length=50, blank=True, null=True)
  12.     image_url = models.SlugField(max_length=100, unique=True)
  13.     image_width = models.IntegerField(blank=True)
  14.     image_height = models.IntegerField(blank=True)
  15.     image_size = models.FloatField(blank=True)
  16.     image_upload = models.ImageField(upload_to=user_directory_path, width_field='image_width', height_field='image_height')
  17.  
  18. -----------------------------------------------Views-----------------------------------------------------
  19. from django.views.generic.detail import DetailView
  20.  
  21. from news.models import New
  22.  
  23.  
  24. class NewsDetail(DetailView):
  25.     model = New
  26.     context_object_name = 'news'
  27.     template_name = 'detailnews.html'
  28.     slug_field = 'category'
  29.     slug_url_kwarg = 'new'
  30.  
  31.     def get_context_data(self, **kwargs):
  32.         context = super(NewsDetail, self).get_context_data(**kwargs)
  33.         context['listnews'] = New.objects.order_by('-date_modification')[:5]
  34.         return context
  35.  
  36. -----------------------------------------------Template Not working-----------------------------------------------------
  37.  
  38. {{news.images_thumbnail.image_url}} -->work
  39. {{news.images_thumbnail}} -->work
  40.  
  41. {% for ima in news.imagesrelate.all %} -->not working
  42. {{ima.image_name}}
  43. {% endfor %}
  44.  
  45. {% for ima in news.images_thumbnail.all %} --> not working
  46. {{ima.image_name}}
  47. {% endfor %}
  48.  
  49. {% for ima in news.imagesrelate.all() %} --> TEMPLATE SYNTAX ERROR
  50. {{ima.image_name}}
  51. {% endfor %}
  52.  
  53. {% for ima in news.images_thumbnail.all() %} --> TEMPLATE SYNTAX ERROR
  54. {{ima.image_name}}
  55. {% endfor %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement