is_voted = user in question.voted_users.all()
is_voted = question.voted_users.filter(pk=user.pk).exists()
voted = request.POST['choice']
voted_choice = get_object_or_404(Choice, pk=voted)
def foo_view(request):
return render(request, 'foo_tpl.html', {'is_error': False})
{% if is_error %}
shit happens
{% endif%}
date¶
Formats a date according to the given format.
Uses a similar format as PHP’s date() function (php.net/date) with some differences.
>>> d
datetime.date(2015, 1, 1)
>>> d.strftime('?y=%Y&m=%m&d=%d')
'?y=2015&m=01&d=01'
>>>
Установка
# Поддерживаемые операционные системы:
# RHEL 5, RHEL 6
# CentOS 5, CentOS 6
# Debian 7
# Ubuntu 12.04, Ubuntu 12.10, Ubuntu 13.04, Ubuntu 13.10, Ubuntu 14.04
# Подключитесь к серверу через SSH под рутом
ssh root@ваш.сервер
# Скачайте инсталляционный скрипт
curl -O http://vestacp.com/pub/vst-install.sh
# Запустите его
bash vst-install.sh
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
SOCIAL_AUTH_VK_OAUTH2_SCOPE = ['email']
SOCIAL_AUTH_TWITTER_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_PIPELINE = (
...
'apps.accounts.social.save_avatar', # это твоя кастомная функция
...
)
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
# views.py
liked_post_ids = UserLikes.objects.filter(user=request.user).values_list('id', flat=True)
# template.html
{% if post.id in liked_posts_ids %}
This post was already liked.
{% else %}
You can like this post.
{% endif %}
# models.py
from django.contrib.auth.models import User
from django.db import models
class Post(models.Model):
user = models.ManyToManyField(User)
slug = models.SlugField()