在浏览器中刷新的角度应用后:Django响应

发布于 2025-02-10 01:11:41 字数 981 浏览 1 评论 0 原文

我目前正在从事一个应用程序:Django后端和Angular Frontend。一切正常,但是在浏览器中刷新一页之后,我再也不会得到角度响应了。但是我得到的是我的Django服务器的成功答案(httpresponse:200)。

我的django urls.py如下:

from django.urls import path
from quickstart import views

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('images/', views.ImageList.as_view()),
    path('images/<str:id>/', views.Image.as_view()),
    path('images/<str:id>/explanations', views.Explanation.as_view())
]

我的app-routing.modules.ts:

const routes: Routes = [
  {path: 'home', component: MainComponent},
  {path: 'images', component: ImageComponent, children: [
    {
      path:':id', component: ImageDetailComponent
    }
  ]},
  {path: '', redirectTo: '/home', pathMatch: 'full'},
  
];

@NgModule({
  imports: [RouterModule.forRoot(
    routes
  )],
  exports: [RouterModule]
})
export class AppRoutingModule { }

I´m currently working on an application: Django backend and angular frontend. Everything works fine, but after refreshing one page in the browser I don´t get the angular response anymore. But what I get is a successful answer from my Django server (HTTPResponse: 200).

My Django urls.py is as followed:

from django.urls import path
from quickstart import views

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('images/', views.ImageList.as_view()),
    path('images/<str:id>/', views.Image.as_view()),
    path('images/<str:id>/explanations', views.Explanation.as_view())
]

My app-routing.modules.ts:

const routes: Routes = [
  {path: 'home', component: MainComponent},
  {path: 'images', component: ImageComponent, children: [
    {
      path:':id', component: ImageDetailComponent
    }
  ]},
  {path: '', redirectTo: '/home', pathMatch: 'full'},
  
];

@NgModule({
  imports: [RouterModule.forRoot(
    routes
  )],
  exports: [RouterModule]
})
export class AppRoutingModule { }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2025-02-17 01:11:41

角生产构建被用作静态文件。它向后端服务器提出了API请求以进行CRUD操作。

您需要做的是在Angular Dist(很可能是Dist文件夹中的index.html文件)中使用静态文件。

在Django 中使用某种路由(建议为Django的API或Web应用程序一个前缀,以了解您的要求。请查看 this

或拥有独立的服务托管了一些可以使用角度的位置(看看 this ),让角处理路由。

Angular production build is served as a static file. it makes the api request to the backend server for the CRUD operations.

what you need to do is serve the static files in angular dist(most probably the index.html file in the dist folder) folder.

using some sort of routing in django (suggestion have a prefix for the API or web app for django to know what are you asking for. look at this)

or have a standalone service hosted some where to serve Angular site(have a look at this) and let angular handle the routing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文