我正在尝试访问用户,但在视图为 时出现错误async。
代码:
from django.http import JsonResponse
async def archive(request):
user = request.user
return JsonResponse({'msg': 'success'})
Run Code Online (Sandbox Code Playgroud)
错误信息:
django.myproject.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
from django.http import JsonResponse
from asgiref.sync import sync_to_async
async def archive(request):
# user = sync_to_async(request.user)
# user = sync_to_async(request.user)()
# user = await sync_to_async(request.user)
user = await sync_to_async(request.user)()
return JsonResponse({'msg': 'success'})
Run Code Online (Sandbox Code Playgroud)
仍然遇到同样的错误。
我想访问用户以检查他/她是否有权存档文件。
编辑:我最终发现我必须将其移至临时方法并将其运行为sync_to_async. 我在下面做了这个:
def _check_user(request):
user = request.user
''' Logic here …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 Flutter 应用程序创建一个身份验证 API,该应用程序将使用谷歌身份验证注册/登录表单登录用户。我按照本教程来实现这一点。
到目前为止一切顺利,只是本教程基于 GitHub 登录而不是 Google。我设法让它一直工作到“连接”步骤。我能够code从重定向中获取 ,但是当我访问时,http://127.0.0.1:8000/auth/google/我看到它要求两个字段 ( access_token, code)。当我尝试只发布信息时,我收到以下错误:
"non_field_errors": [
"View is not defined, pass it as a context variable"
]
Run Code Online (Sandbox Code Playgroud)
django python-3.x django-rest-framework django-allauth django-rest-auth
我希望能够通过将数据发送到我的函数来解析来自我的 django rest api 项目中的 post 请求的数据,如果该数字在将其保存到数据库之前有效,则返回 true 或 false,如果它是错误的发送自定义发送给执行请求的客户端的错误请求消息。
有人告诉我我可以覆盖 create 方法来做到这一点,但我不知道如何去做。
到目前为止,我的代码如下所示:
class Messages(models.Model):
phone_number = models.CharField(max_length=256, default='')
message_body = models.CharField(max_length=256, default='')
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.phone_number + ' ' + self.message_body + ' ' + self.created
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
# I assume this is where I would do the check before saving it but not sure how? example would be like:
# if numberValid(self.phone_number):
# then save to the database
# else: …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以保留在同一页面上,而无需使用 FormView 重新加载到 success_url。理想情况下,我希望它只是发送通知或什么都不做。只需处理表格即可。如果这是有道理的。
我的代码目前在views.py中看起来像这样
class Capture(FormView):
template_name = "capture.html"
success_url = '/'
form_class = TestForm
def form_valid(self, form, *args, **kwargs):
print("Submit Capture")
form.save_screenshot()
return super().form_valid(form)
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变这个不重新加载我的页面。欢迎其他库或方法,并且不限于使用 FormView。
我有一个 Django 项目,我想创建一个简单的 Python GUI,以允许用户随时随地更改主机地址和端口,而无需与命令提示符进行交互。我已经有了一个简单的 python 用户界面,但是我将如何在 python 中编写一些能够运行命令python manage.py createsuperuser并填写所需信息的东西,而无需在只调用常规终端命令的 python 脚本中运行它如:
subprocess.call(["python","manage.py", "createsuperuser"])
Run Code Online (Sandbox Code Playgroud)
我不知道这是否可以在没有命令的情况下完成,但如果从那时起我就可以将它实现到我的基本 python GUI 中,这将允许用户createsuperuser, runserver, makemigrations,migrate甚至更改默认主机和端口去。
我遵循了一个教程,允许用户注册帐户,但似乎找不到url路径。当我为其设置名称时,它允许我访问127.0.0.1:8000/帐户/注册,但不能访问127.0.0.1:8000/注册。
我尝试将urlpatterns从路径更改为url方法,但这不能解决问题。
我的文件结构如下:
App:
accounts:
urls.py
views.py
...
Server:
settings.py
urls.py
...
templates:
registration:
login.html
base.html
home.html
signup.html
manage.py
Run Code Online (Sandbox Code Playgroud)
在我的Server \ urls.py中,
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
]
Run Code Online (Sandbox Code Playgroud)
在我的account \ urls.py中,
from django.urls import path
from . import views
urlpatterns = [
path('signup/', views.SignUp.as_view(), name='signup'),
]
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
Using the URLconf defined in Server.urls, Django tried these URL patterns, in this …Run Code Online (Sandbox Code Playgroud) django ×6
python ×3
python-3.x ×2
api ×1
async-await ×1
exe ×1
executable ×1
formview ×1
html ×1
validation ×1
web ×1