【问题标题】:Django cannot find url pattern request from AngularDjango 找不到来自 Angular 的 url 模式请求
【发布时间】:2020-08-21 15:11:15
【问题描述】:

Angular 8,Django 3。我已经做了一百次相同的路由和 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 )}}

订单服务

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)
  }

姜戈

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..

【问题讨论】:

    标签: django angular http-status-code-404


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-04
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2019-11-21
      • 2014-12-13
      • 1970-01-01
      相关资源
      最近更新 更多