Django无法从Angular找到网址格式请求

问题描述 投票:0回答:1

Angular 8,Django3。我已经完成了相同的路由和url,视图,服务一百次,但是由于某种原因,这条路由将无法工作,我也不知道为什么。.

角度:

html(带有链接)

<h2>My Restaurants</h2>

<ul *ngFor = "let restaurant of restaurants">
    <a [routerLink]="['/ordersummary', restaurant.id]">Orders</a>
</ul>

app.routing.module

const routes: Routes = [
  {path: 'ordersummary/:id', component:OrderSummaryComponent}
];

OrderSummaryComponent

export class OrderSummaryComponent implements OnInit {

  constructor(
    private orderservice:OrderService,
    private router: ActivatedRoute) { }

  id;
  orders;

  ngOnInit() {
    this.id = this.router.snapshot.paramMap.get('id')
    this.getrestaurantorders(this.id)}

  getrestaurantorders(id){
    this.orderservice.restaurantorders(id).subscribe(
      x => this.orders = x )}}

OrderService

export class OrderService {

private baseUrl2 : string ='http://127.0.0.1:8000/users/'
private restaurantordersUrl : string = this.baseUrl2+'restaurantorders/'

  constructor(
    private http: HttpClient) { }

  restaurantorders(id):Observable<any>{
    return this.http.get<any>(this.restaurantordersUrl+id)
  }

Django

urls.py

#base urls
urlpatterns = [
    path('users/', include('users.urls')),

#users urls
urlpatterns = [
    path(r'restaurantorders/<int:pk>', RestaurantOrders.as_view(), name='restaurantorders'),
    ]

views.py

class RestaurantOrders(generics.RetrieveAPIView):
    serializer_class = RestaurantOrderSerializer
    queryset = Orders.objects.all()

serializers.py

class RestaurantOrderSerializer(serializers.ModelSerializer):
    recipe = RecipeSerializerName
    customer = CustomerSerializerShort
    class Meta:
        model = Orders
        fields = '__all__'

我知道Angular正在要求正确的URL,因为我得到的错误是"Http failure response for http://127.0.0.1:8000/users/restaurantorders/2: 404 Not Found"。但是http://127.0.0.1:8000/users/restaurantorders/2网址就在那里...我不明白,我所有其他请求都可以正常工作。我已经重新启动了服务器,也许接下来重新启动Pycharm。

Angular 8,Django3。我已经完成了相同的路由和url,视图,服务一百次,但是由于某种原因,这一路由将无法工作,我也不知道为什么。.Angular:html(with link)] >

[不确定是否有人会遇到此问题。。但是这里的问题是,我的url模式中的pk值用于Restaurant对象,而我的serializers.py正在查询Orders模型。

django angular http-status-code-404
1个回答
0
投票

[不确定是否有人会遇到此问题。。但是这里的问题是,我的url模式中的pk值用于Restaurant对象,而我的serializers.py正在查询Orders模型。

© www.soinside.com 2019 - 2024. All rights reserved.