尝试使用一个非常简单的websockets应用程序来使用Django通道进行身份验证,该应用程序回送用户使用前缀发送的任何内容"You said: ".
我的流程:
web: gunicorn myproject.wsgi --log-file=- --pythonpath ./myproject
realtime: daphne myproject.asgi:channel_layer --port 9090 --bind 0.0.0.0 -v 2
reatime_worker: python manage.py runworker -v 2
Run Code Online (Sandbox Code Playgroud)
我在本地测试时运行所有进程heroku local -e .env -p 8080,但您也可以单独运行它们.
注意我打开了WSGI localhost:8080和ASGI localhost:9090.
路由和消费者:
### routing.py ###
from . import consumers
channel_routing = {
'websocket.connect': consumers.ws_connect,
'websocket.receive': consumers.ws_receive,
'websocket.disconnect': consumers.ws_disconnect,
}
Run Code Online (Sandbox Code Playgroud)
和
### consumers.py ###
import traceback
from django.http import HttpResponse
from channels.handler import AsgiHandler
from channels import Group
from channels.sessions import channel_session …Run Code Online (Sandbox Code Playgroud) 我正在使用 AbstractUser 模型来创建自定义身份验证模型。
问题是我无法覆盖用户名字段的默认表单字段验证器,这是我迄今为止尝试过的:
class RegularUserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(RegularUserForm, self).__init__(*args, **kwargs)
self.fields['username'].help_text = None
self.fields['username'].default_validators = []
self.fields['username'].validators = []
Run Code Online (Sandbox Code Playgroud)
不知道如何做到这一点,覆盖 help_text 成功,我也尝试使用 [None],而不是[]与self.fields['username'].validators = [validate_username]其中validate_username是我创建了一个自定义的验证。
例如,这是表单代码:
class RegularUserForm(forms.ModelForm):
username = forms.CharField(max_length=30, validators=[validate_username])
email1 = forms.EmailField(required=True, label='')
class Meta:
model = RegularUser
fields = ['username', 'password', 'email', 'email1', 'gender', ]
widgets = {'password': forms.PasswordInput(attrs={'placeholder': 'enter password'}),
'email': forms.EmailInput(attrs={'placeholder': 'enter email'})
}
def clean(self):
cleaned_data = super(RegularUserForm, self).clean()
email = self.cleaned_data.get('email')
email1 = self.cleaned_data.get('email1') …Run Code Online (Sandbox Code Playgroud) 我正在使用DJango 1.8和Python 3.4
当运行以下视图时,Django抛出类型错误 - 对象不是JSON Serializable
Views.py
from django.http import HttpRequest,HttpResponse
from django.http import JsonResponse
from json import dumps
def get_stats(request):
if request.method == "POST":
srch_dropV = request.POST['srch_dropAJ']
else:
srch_dropV = ''
if(srch_dropV == 'Green'):
students = GreenBased.objects.all()
if(srch_dropV == 'Yellow'):
students = YellowBased.objects.all()
response_data = {}
try:
response_data['result'] = 'Success'
response_data['message'] = list(students)
except:
response_data['result'] = 'Ouch!'
response_data['message'] = 'Script has not ran correctly'
return HttpResponse(JsonResponse(response_data), content_type="application/json")
Run Code Online (Sandbox Code Playgroud)
我正在尝试从mysql数据库中读取几行并将其显示在html文件中,当我正在运行上面的视图时,我正面临下面的错误消息
TypeError: YellowBased: YelloBased object is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
在HTML页面中,我有一个下拉列表..基于所选的选项,我的Ajax会返回从mysql表中获取的记录.
Models.py …