Answer the question
In order to leave comments, you need to log in
Django view primarykey how to get data from the database in the form as in the example?
It should be changed to
Namepart1:
racdel1
racdel2
Namepart2:
razdel3
razdel4
razdetl5
and so on.
I know that there are two cycles and it is necessary to shamanize with a view, but how?
class NamePart(models.Model):
title = models.CharField(max_length=255,verbose_name=" .")
def get_absolute_url(self):
return "/url/%i/" % self.id
def __unicode__(self):
return '%s' % (self.title)
class Rozdil(models.Model):
title = models.CharField(max_length=255,verbose_name="Налу")
part_of = models.ForeignKey(NamePart,verbose_name="н")
is_main = models.BooleanField(default=1,verbose_name="Гоний ?")
def get_absolute_url(self):
return "/rozdil/%i/" % self.id
def __unicode__(self):
return '%s' % (self.title)
Answer the question
In order to leave comments, you need to log in
# urls.py
url(r"^/show-all/(?P<pk>\d+)/", some_view, name='show_all' ),
url(r"^/rozdil/(?P<pk>\d+)/", some_view1, name='rozdil' ),
url(r"^/name-part/(?P<pk>\d+)/", some_view2, name='name_part'),
# models.py
class NamePart(models.Model):
title = models.CharField(max_length=255,verbose_name=" .")
def get_absolute_url(self):
# используй
# https://docs.djangoproject.com/en/1.8/topics/http/urls/#reverse-resolution-of-urls
return reverse('name_part', self.id)
def __unicode__(self):
return u'%s' % (self.title)
class Rozdil(models.Model):
title = models.CharField(max_length=255,verbose_name="Налу")
part_of = models.ForeignKey(NamePart,verbose_name="н")
is_main = models.BooleanField(default=1,verbose_name="Гоний ?")
def get_absolute_url(self):
return reverse('rozdil', self.id)
def __unicode__(self):
return u'%s' % (self.title)
#views.py
def some_view(request):
foo = []
for name_part in NamePart.objects.all():
foo.append({
'name_part': name_part,
'rozdils': Rozdil.objects.filter(part_of=name_part.pk)
})
return render(request, 'template_show_all.html', {'foo': foo})
# template_show_all.html
{% for buzz in foo %}
<h1>{{ buzz. name_part.title}}
{% for bar in buzz.rozdils %}
<h2>{{ bar.title }}
{% endfor %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question