我想在身份验证管道的末尾添加一个函数,该函数旨在检查该用户是否有“配置文件”表,如果没有,它将创建一个表。Profiles 模型是一个表,我在其中存储了有关用户的一些额外信息:
class Profiles(models.Model):
user = models.OneToOneField(User, unique=True, null=True)
description = models.CharField(max_length=250, blank=True, null=True)
points = models.SmallIntegerField(default=0)
posts_number = models.SmallIntegerField(default=0)
Run Code Online (Sandbox Code Playgroud)
每个用户都必须有一个 Profiles 表。所以,我在管道的末尾添加了一个函数:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'app.utils.create_profile' #Custom pipeline
)
#utils.py
def create_profile(strategy, details, response, user, *args, **kwargs):
username = kwargs['details']['username']
user_object = User.objects.get(username=username)
if Profiles.ojects.filter(user=user_object).exists():
pass
else:
new_profile = Profiles(user=user_object)
new_profile.save()
return kwargs
Run Code Online (Sandbox Code Playgroud)
我收到错误:
KeyError at /complete/facebook/
'details'
...
utils.py in create_profile
username = kwargs['details']['username']
Run Code Online (Sandbox Code Playgroud)
我是 python 社交身份验证的新手,看起来我遗漏了一些明显的东西。任何帮助将不胜感激。
我想在 Heroku 中的应用程序中添加几个新字段。我不想丢失那里的数据。因此,我尝试使用 South,但它一直给我错误,看起来我在某个地方犯了错误,现在我需要删除迁移并重试。在本地环境中,这是通过删除migrations文件夹并删除south_migrationhistory表来完成的。我是 Heroku 的新手,我猜这些命令是相似的,但我似乎找不到它们。在此先感谢您的帮助。
我需要了解 linux 的人的帮助。我无法将我的静态文件推送到 Heroku(我正在尝试使用白噪声而不是 Amazon S3 或任何 CDN)。错误很简单,当我尝试推送到 heroku 时,我得到:
Preparing static assets
Running collectstatic...
Traceback (most recent call last):
OSError: [Errno 30] Read-only file system: '/assets'
Run Code Online (Sandbox Code Playgroud)
所以,我不能运行 collectstatic 因为我没有权限。如果我这样做,sudo git push heroku master我会收到另一个错误:Permission denied (publickey)我猜这是因为“sudo”使用了不同的 SSH 密钥。
我尝试从文件管理器更改文件夹的权限,我也尝试从 bash:
sudo chmod 777 -R static
Run Code Online (Sandbox Code Playgroud)
static 是父文件夹:static/assets。
那么......我该如何解决这个问题?任何帮助将不胜感激。
我收到此错误:
包含的urlconf'fourraxis.urls'似乎没有任何模式.如果您在文件中看到有效模式,则问题可能是由循环导入引起的.
我知道url模式中有一些内容,它看起来像这样:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^perfil/', include('clientes.urls'), namespace="cliente"),
url(r'^admin/', include(admin.site.urls))
]
Run Code Online (Sandbox Code Playgroud)
clientes.urls:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from clientes import views
urlpatterns = [
# login
url(r'^login/$', auth_views.login, {'template_name': 'perfiles/login.html'}, name="login"),
url(r'^logout/$', auth_views.logout, {'template_name': 'perfiles/logged_out.html'}, name="login"),
url(r'^mi_perfil/$', views.mi_perfil, name="mi_perfil"),
url(r'^registro_usuario/$', views.RegistroUsuario.as_view(), name="registro_usuario")
]
Run Code Online (Sandbox Code Playgroud)
RegistroUsuario视图如下所示:
class RegistroUsuario(FormView):
template_name = "perfiles/registro_usuario.html"
form_class = UserCreationForm
success_url = reverse("cliente:mi_perfil") # THIS REVERSE
def form_valid(self, form):
return redirect("cliente:mi_perfil")
context = …Run Code Online (Sandbox Code Playgroud) 现在我正在使用$.val()设置输入的值。但我希望用户在输入中看到不同的文本,并且仍然将设置的值发送val()到服务器。我尝试使用$.text()但它不起作用。有没有办法设置 htmlvalue属性并在输入中设置一个不同的文本,以便 th## Heading ##e 用户查看?
我没有发布任何代码,因为它非常通用。我需要这个的原因是因为我想在文本中显示一个不错的日期字符串(我使用的是日期时间选择器)并将该字符串的压缩版本发送到服务器。
编辑
好的,希望这有帮助:
<input class="form-control date" id="booking_time" name="hour" type="text">
Run Code Online (Sandbox Code Playgroud)
在Javascript中,我有类似的东西:
var date_booked = new Date()
$("#booking_time").val(date_booked.toDateString());
Run Code Online (Sandbox Code Playgroud)
但我想发送到服务器是日期输入的值内部消除了toDateString等待处理,并仍然显示date.toDateString()在文本框中。为此,我希望这会奏效:
$("#booking_time").val(date_booked);
$("#booking_time").text(date_booked.toDateString());
Run Code Online (Sandbox Code Playgroud)
但事实并非如此。
我有一个看起来像这样的网址:
url(r'^client_profile/address/(?P<id>.+)/$', views.ClientProfileAddressView.as_view())
Run Code Online (Sandbox Code Playgroud)
还有一个APIView:
class ClientProfileAddressView(APIView):
renderer_classes = (JSONRenderer,)
permission_classes = (IsAuthenticated,)
def put(self, request):
....
def get(self, request):
....
Run Code Online (Sandbox Code Playgroud)
在这两个put和get我需要访问idURL kwarg,更新对象,第二个更新它的第一个.如何在这些方法中访问url参数?
我有这个代码:
with open('stockitems_misuper.csv', 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(file_rows)
response = HttpResponse(myfile, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=stockitems_misuper.csv'
return response
Run Code Online (Sandbox Code Playgroud)
我收到错误:
关闭文件的I/O操作
如何将创建的csv文件发送到前端?
有没有办法使用python social auth获取Google个人资料图片网址?
所以,这就是我为facebook和twitter所做的事情,用这段代码添加一个管道:
if strategy.backend.name == 'facebook':
url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
elif strategy.backend.name == "twitter":
if response['profile_image_url'] != '':
url = response['profile_image_url']
elif strategy.backend.name == "GoogleOAuth2": # doesn't work
user_id = response['id']
url = "???"
Run Code Online (Sandbox Code Playgroud)
首先,我不知道后端的名称是什么,是"GoogleOAuth2"吗?第二,我应该用什么网址来保存个人资料的头像?这是这样的吗?
这上周工作了.也许我做错了什么并搞砸了别的地方,或者它可能是一个bug,或者它只是一个更新而我在阅读文档时错过了它.
我有一个管道,可以获取用户的头像并保存URL:
def get_avatar(strategy, details, response, user, *args, **kwargs):
url = None
if strategy.backend.name == 'facebook':
url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
elif strategy.backend.name == "twitter":
if response['profile_image_url'] != '':
url = response['profile_image_url']
elif strategy.backend.name == "google-oauth2":
if response['image'].get('url') is not None:
url = response['image'].get('url')
Run Code Online (Sandbox Code Playgroud)
它曾经工作,现在,它给了我错误:
'DjangoStrategy' object has no attribute 'backend'
Run Code Online (Sandbox Code Playgroud)
请帮助,一些测试用户已经在使用我的网站,目前他们没有个人资料图片.
假设我有一个模型评论和另一个模型答案。我想查询所有评论,但还要在查询中包含每个评论的答案数量。我认为annotate()在这种情况下会很有用,但我只是不知道如何写下来。文档写道:
Django 1.8 中的新功能:以前版本的 Django 只允许将聚合函数用作注释。现在可以使用各种表达式来注释模型。
所以。这是我的模型的一个例子:
class Comments(models.Model):
...
class Answers(models.Model):
comment = models.ForeignKey(Comments)
Run Code Online (Sandbox Code Playgroud)
这是一个示例查询:
queryset = Comments.objects.all().annotate(...?)
Run Code Online (Sandbox Code Playgroud)
我不确定如何注释Count每个评论的答案。即有多少答案指向 FK 字段上的每个评论comment。甚至有可能吗?有没有更好的办法?只在 manager 上写一个方法会更好吗?
django ×7
python ×6
heroku ×2
chmod ×1
csv ×1
django-south ×1
django-urls ×1
google-login ×1
google-plus ×1
javascript ×1
jquery ×1
linux ×1