def total_count(request):
if request.user.is_authenticated():
total, created = CountArticle.objects.get_or_create(user=request.user)
if created:
total.count.add(*list(Article.objects.all()))
return {"total_count": total.count.count(), }
return {}
len(count.count.all())
count.count.count()
class MyView(TemlateView):
def get_template_names(self):
style = self.request.GET.get('new_style', 0)
if style == 1:
return list(map(lambda t: '_{}'.format(t), super().get_template_names())
return super().get_template_names()
class MyModelAdmin(admin.ModelAdmin):
my_filter = {1: ('field1', 'field2'), 2: ('field3', 'field4')}
def get_fields(self, request, obj=None):
fields = super().get_fields(request, obj)
if obj is None:
return fields
return [f for f in fields if f not in self.my_filter[obj.my_type]]
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = (
'js/my_fields_filter.js',
)
class Main(models.Model):
link_name = models.CharField(max_length = 25)
topic_name = models.CharField(max_length = 100)
content = models.TextField()
def __str__ (self):
return self.link_name
class Meta:
ordering = ["-id"]
class Tag(models.Model):
name = models.CharField(max_length = 25, default=1)
main = models.ManyToManyField(Main, related_name='tags')
def __str__ (self):
return self.name
def main_tag(request, tag_id):
main = Main.objects.filter(tags__id=tag_id)
content={
"main": main
}
return render (request, "index_tag.html", content )
...
url(r'^main/(\d+)/$', main_tag, name='main_tag'),
...
{% for m in main %}
Name: {{ m.topic_name }}
Text: {{ m.content }}
Tags:
{% for tag in m.tags %}
<a href="{% url "main_tag" tag.id %}">{{ tag.name }}</a>
{% endfor %}
{% endfor %}
url(r'^book/(?P<alias>[^/]+)/add_comment/$', 'books.views.add_comment',
name="book_add_comment"),
url(r'^book/(?P<alias>[^/]+)/$', 'books.views.item'),
{% for comment in comments %}
<p>{{ comment.text }}</p>
{% endfor %}
<form action="{% url "book_add_comment" alias=item.alias %}" method="post">
{% csrf_token %}
{{ form }}
</form>
class Peoples(models.Model):
subpodr = ForeignKey(Subpodr)
date = models.DateField(default=datetime.datetime.today())
rab = models.IntegerField(default = 0)
class Meta:
unique_together = ('subpodr', 'date')
peoples_for_main_page = Peoples.objects.values('subpodr__sobj', 'date', 'rab').annotate(
num_peoples=Sum('rab')).order_by('date', 'subpodr__sobj')
sobject = Sobject.objects.get(pk=current_sobject_pk)
peoples_for_sobject_page = Peoples.objects.filter(subpodr__sobj=sobject).annotate(
num_peoples=Sum('rab')).order_by('date', 'subpodr')