gpt4 book ai didi

angular - testdriven.io:使用 Django Channels 和 Angular 开发实时出租车应用程序:无法从 gcp 上的前端注册

转载 作者:行者123 更新时间:2023-12-05 04:32:36 25 4
gpt4 key购买 nike

我正在学习教程 https://testdriven.io/courses/real-time-app-with-django-channels-and-angular/在 GCE VM 上,能够与前端和 Django 管理和 API 交互。但是,当我尝试从 Angular 前端单击注册 (http POST) 时,我看到了 403 错误。

我认为失败发生在我的 onSumbit() 的订阅部分。触发 onSubmit() 的表单没有来自序列化程序的 password2 参数,我不确定这是否是这里的断点。

onSubmit(): void {
this.authService.signUp(
this.user.username,
this.user.firstName,
this.user.lastName,
this.user.password,
this.user.group,
this.user.photo
).subscribe({
complete: () => this.router.navigateByUrl('/log-in'),
error: (error) => console.error(error),
});

控制台错误是

HttpErrorResponse {headers: HttpHeaders, status: 403, statusText: 'Forbidden', url: 'http://35.274.59.16:8080/api/sign_up/', ok: false, …}
error: {detail: 'CSRF Failed: CSRF token missing.'}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://35.274.59.16:8080/api/sign_up/: 403 Forbidden"
name: "HttpErrorResponse"
ok: false
status: 403
statusText: "Forbidden"
url: "http://35.274.59.16:8080/api/sign_up/"
[[Prototype]]: HttpResponseBase

我的 Django 序列化程序:

from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from django.contrib.auth.models import Group
from .models import Trip

class UserSerializer(serializers.ModelSerializer):
password1 = serializers.CharField(write_only=True)
password2 = serializers.CharField(write_only=True)
group = serializers.CharField()

def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError('Passwords must match.')
return data

def create(self, validated_data):
group_data = validated_data.pop('group')
group, _ = Group.objects.get_or_create(name=group_data)
data = {
key: value for key, value in validated_data.items()
if key not in ('password1', 'password2')
}
data['password'] = validated_data['password1']
user = self.Meta.model.objects.create_user(**data)
user.groups.add(group)
user.save()
return user

class Meta:
model = get_user_model()
fields = (
'id', 'username', 'password1', 'password2',
'first_name', 'last_name', 'group',
'photo', # new
)
read_only_fields = ('id',)

class LogInSerializer(TokenObtainPairSerializer): # new
@classmethod
def get_token(cls, user):
token = super().get_token(user)
user_data = UserSerializer(user).data
for key, value in user_data.items():
if key != 'id':
token[key] = value
return token

class TripSerializer(serializers.ModelSerializer):
class Meta:
model = Trip
fields = '__all__'
read_only_fields = ('id', 'created', 'updated',)

class NestedTripSerializer(serializers.ModelSerializer):
class Meta:
model = Trip
fields = '__all__'
depth = 1

这是我的 docker-compose.yml。

version: '3'

services:
taxi-redis:
container_name: taxi-redis
image: redis:6.2-alpine

taxi-database:
container_name: taxi-database
environment:
- POSTGRES_PASSWORD
image: postgres:14.1
ports:
- 5433:5432
volumes:
- taxi-database:/var/lib/postgresql/data

taxi-server:
build:
context: ./server
command: daphne --bind 0.0.0.0 --port 8000 taxi.asgi:application
# command: python manage.py runserver 0.0.0.0:8000
container_name: taxi-server
depends_on:
- taxi-redis
- taxi-database
env_file:
- ./server/.env
ports:
- 8001:8000
volumes:
- ./server:/usr/src/app
- media:/usr/src/app/media # new
- static:/usr/src/app/static # new

taxi-client:
build:
context: ./client
command: ng serve --host 0.0.0.0
container_name: taxi-client
depends_on:
- taxi-server
environment:
- CHROME_BIN=chromium-browser
ports:
- 4201:4200
volumes:
- ./client:/usr/src/app

nginx:
build:
context: ./nginx
container_name: taxi-nginx
depends_on:
- taxi-server
- taxi-client
ports:
- 8080:80
restart: always
volumes:
- media:/usr/src/app/media
- static:/usr/src/app/static
# - ./server/static:/usr/src/app/static # added this really late.
- ./server/media:/usr/src/app/media

volumes:
taxi-database:
media:
static:

最佳答案

发现使用带有 SSL 证书的域进行路由是令人满意的。不确定这是否是最佳解决方案,甚至不确定这对于 GCP 托管的开发环境是否必要。

关于angular - testdriven.io:使用 Django Channels 和 Angular 开发实时出租车应用程序:无法从 gcp 上的前端注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71629002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com