class PetAdmin(admin.ModelAdmin):
...
def get_urls(self):
urls = super(PetAdmin, self).get_urls()
my_urls = [
url(r'^(?P<id>\d+)/radius/$', self.admin_site.admin_view(PetAdminRadius))
]
return my_urls + urls
class UserAdmin(admin.ModelAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2'),
}),
)
def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super(UserAdmin, self).get_fieldsets(request, obj)
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
import cStringIO as StringIO
import xhtml2pdf.pisa as pisa
def render_to_pdf(template_src, context_dict, filename='contract.pdf'):
""" Отдаю PDF файл """
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(
StringIO.StringIO(html.encode('utf-8')),
result,
encoding='UTF-8',
show_error_as_pdf=True
)
if not pdf.err:
response = HttpResponse(result.getvalue(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
return HttpResponse(u'We had some errors!')
def contract_text(request):
text = ContractText.objects.filter(user=request.user)
return render_to_pdf('pfd_template.html', {'text': text}, 'new_contract.pdf')
/etc/apache2/
/etc/nginx/
- sites
+ project_name_one
- env (тут окружение)
- tmp
- logs
- source (тут весь код)
+ project_name_two
- env (тут окружение)
- tmp
- logs
- source (тут весь код)
class CommentAdmin(admin.ModelAdmin):
list_display = ('news__title')
class CommentAdmin(admin.ModelAdmin):
list_display = ('news_title_fnc')
def news_title_fnc(self, obj):
return obj.news.title
python manage.py makemigrations
python manage.py migrate
«Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живёте» © Стив Макконнелл
queryset.filter(**params).only('dates', 'type_date').select_related(*select_rel).order_by('-dates', '-create')
queryset.values('dates').annotate(count=Count('dates')).order_by() # Сбросил сортировку
SELECT `dates`, COUNT(`dates`) AS `count`
FROM `gigdate_dateall`
WHERE (`dates` IN (2015-08-31, 2015-09-01, 2015-09-02, 2015-09-03) AND `type_date` = 0)
GROUP BY `dates` ORDER BY NULL
dates = models.DateField(_(u'Дата календаря'), blank=False)
...
class Meta:
ordering = ['-dates', '-create']
SELECT `dates`, COUNT(`dates`) AS `count`
FROM `gigdate_dateall`
WHERE (`dates` IN (2015-08-31, 2015-09-01, 2015-09-02, 2015-09-03) AND `type_date` = 0)
GROUP BY `dates`, `create` ORDER BY `dates` DESC, `create` DESC
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
class ValidLogin(object):
def process_request(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('signin'))
return None
MIDDLEWARE_CLASSES = (
'project.middleware.ValidLogin',
)
from django.conf.urls import patterns, url
from django.contrib.auth.decorators import user_passes_test
urlpatterns = patterns('crm.views',
url(r'^$',
user_passes_test(lambda u: u.is_superuser or u.is_moderator)(HomeCRM.as_view()),
name='home'),
)
from django.contrib.auth.decorators import login_required
urlpatterns = patterns('crm.views',
url(r'^$',
login_required(HomeCRM.as_view()),
name='home'),
)