Python 在django项目中,如何让多个用户在同一浏览器中同时登录
我正在django项目中工作,我想在其中实现聊天。我已经为聊天编写了代码,但我同时面临一个问题,我无法登录两个或多个用户。如何使用多个登录进行聊天 我的聊天代码是Python 在django项目中,如何让多个用户在同一浏览器中同时登录,python,django,Python,Django,我正在django项目中工作,我想在其中实现聊天。我已经为聊天编写了代码,但我同时面临一个问题,我无法登录两个或多个用户。如何使用多个登录进行聊天 我的聊天代码是 型号.py from django.db import models from django.contrib.auth.models import User class Chat(models.Model): created = models.DateTimeField(auto_now_add=True) user
型号.py
from django.db import models
from django.contrib.auth.models import User
class Chat(models.Model):
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
message = models.CharField(max_length=200)
def __unicode__(self):
return self.message
def Login(request):
next = request.GET.get('next', '/home/')
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(next)
else:
return HttpResponse("Account is not active at the moment.")
else:
return HttpResponseRedirect(settings.LOGIN_URL)
return render(request, "alpha/login.html", {'next': next})
def Logout(request):
logout(request)
return HttpResponseRedirect('/login/')
def Home(request):
c = Chat.objects.all()
return render(request, "alpha/home.html", {'home': 'active', 'chat': c})
def Post(request):
if request.method == "POST":
msg = request.POST.get('msgbox', None)
c = Chat(user=request.user, message=msg)
if msg != '':
c.save()
return JsonResponse({ 'msg': msg, 'user': c.user.username })
else:
return HttpResponse('Request must be POST.')
def Messages(request):
c = Chat.objects.all()
return render(request, 'alpha/messages.html', {'chat': c})
视图.py
from django.db import models
from django.contrib.auth.models import User
class Chat(models.Model):
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
message = models.CharField(max_length=200)
def __unicode__(self):
return self.message
def Login(request):
next = request.GET.get('next', '/home/')
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(next)
else:
return HttpResponse("Account is not active at the moment.")
else:
return HttpResponseRedirect(settings.LOGIN_URL)
return render(request, "alpha/login.html", {'next': next})
def Logout(request):
logout(request)
return HttpResponseRedirect('/login/')
def Home(request):
c = Chat.objects.all()
return render(request, "alpha/home.html", {'home': 'active', 'chat': c})
def Post(request):
if request.method == "POST":
msg = request.POST.get('msgbox', None)
c = Chat(user=request.user, message=msg)
if msg != '':
c.save()
return JsonResponse({ 'msg': msg, 'user': c.user.username })
else:
return HttpResponse('Request must be POST.')
def Messages(request):
c = Chat.objects.all()
return render(request, 'alpha/messages.html', {'chat': c})
我假设您只想将其用于测试目的。如果是这样,我建议您使用浏览器的“匿名模式”,这样您就可以有两个会话。例如:在Chrome中,您可以通过按
CTRL+SHIFT+N
激活一个匿名窗口(从主浏览器窗口启动一个单独的会话)。你可以在网站上阅读更多关于它的信息。当然,Edge、Firefox等也有类似的功能
或者,您也可以使用多个浏览器—一个Chrome浏览器、一个Microsoft Edge浏览器和一个Firefox浏览器,每次登录时使用不同的用户这不是django问题,也不是python问题。如果您希望两个用户同时登录到dev服务器用户,那么两个不同的浏览器将使用匿名模式使其成为4