РЕАЛЬНО РАБОЧИЙ ПРОЕКТ models py class City models Model city models C

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
################## РЕАЛЬНО РАБОЧИЙ ПРОЕКТ ##############
# models.py
class City (models.Model):
city = models.CharField(max_length = 50, verbose_name = "Город")
slug = models.SlugField(max_length = 50)
created = models.DateTimeField(editable=False, auto_now_add=True)
def __unicode__(self):
return self.city
def get_absolute_url(self):
return '/city/%s/' % self.slug
def get_companyes(self):
current_city = City.objects.get(id = self.id)
city_companies_count = len([company for company in self.contacts_set.all()])
return city_companies_count
def get_name (self):
return self.name
#vievs.py
my_company_dict = {'all_company' : Company.objects.all(),
'all_services' : Service.objects.all(),
'all_city' : City.objects.all(),
'last_add_company' : Company.objects.order_by('-created')[:3],}
def all_city_company (request, slug):
"""
Выводит все компании из одного города, все виды деятельности, кол-во компаний в городе
"""
if request.method == 'POST':
login(request)
user = ''
if request.user.is_authenticated():
user = request.user.username
current_city = City.objects.get(slug = slug) # Текущий город
current_city_addresses = current_city.contacts_set.all() # Все адреса с этим городом
company_city_list = []
for contacts in current_city_addresses:
current_company = contacts.company
company_city_list.append(current_company)
paginator = Paginator(company_city_list, 2)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
pages_company = paginator.page(page)
except (EmptyPage, InvalidPage):
pages_company = paginator.page(paginator.num_pages)
my_dict = {'current_city' : current_city,
'pages_company' : pages_company,
'user':user,
}
my_company_dict.update(my_dict)
return render_to_response('company/company_city.html', my_company_dict,)
#template
<p>{% for service in all_services %}
<a href="{{ service.get_absolute_url }}">{{ service.name }} ({{ service.get_companyes }})</a>,
{% endfor %}
################## МОЙ РЕАЛЬНО НЕРАБОТАЮЩИЙ ПРОЕКТ ##############
#models.py
class Catalog(models.Model):
catalog_name = models.CharField(max_length = 50, verbose_name="Имя каталога")
catalog_slug = models.SlugField(max_length=50, verbose_name="Служебное поле")
subcatalog = models.ManyToManyField(Subcatalog, null=True, verbose_name="Подкаталоги", blank=True)
class Meta:
verbose_name_plural = "Каталоги"
verbose_name = "каталог"
def __unicode__(self):
return self.catalog_name
def get_absolute_url(self):
return "/catalog/" % self.slug
#vievs.py
@render_to('index.html')
def index(request):
return {'catalogs': Catalog.objects.all(),
'top': Product.objects.filter(in_top=True),
'user':request.user,}
index.html
{% block left %}
<ul>
{% for service in catalogs %}
<li><a href="{{ service.get_absolute_url }}">{{service.catalog_name}}</a></li>
{% ifequal i.catalog_name catalog_url %}
<ul>
{% for q in subcatalog_urls %}
<li><a href="/catalog/{{i.catalog_name}}/{{q.subcatalog_name}}">{{q.subcatalog_name}}</a></li>
{%endfor%}
</ul>
{% endifequal %}
{%endfor%}
</ul>
{% endblock %}