2

I have django+angular6 web app and, probably, problems with routing.

My 1st example of urls.py looks like:

url(r'index/', views.GenericView.as_view()),
url(r'api/checks/', views.GetChecksView.as_view()),
url(r'api/editcheck/', views.ChecksEdit.as_view()),
url(r'api/createcheck/', views.CreateCheckView.as_view()),

and current version of app-routing-module.ts:

const routes: Routes = [
  { path: '', redirectTo: '/checks', pathMatch: 'full' },
  { path: 'checks', component: ChecksComponent },
  { path: 'editcheck/:id', component: CheckEditComponent },
  { path: 'createcheck', component: AddCheckComponent}
];
@NgModule({
  exports: [
    RouterModule
  ],
  imports: [
    RouterModule.forRoot(routes)
  ]
})
export class AppRoutingModule { }

While running on local, on 127.0.0.1:8000 I am obviously getting page not found error.

Solution could be simple, but when I change r'index/' at urls.py to r'' it runs into an error when I'm trying to post any data.

Already tested a lot of options, such as posting through python script and changing angular component models, didnt work any good. I hope I could be sure that the problem in these parts of code, but if its not - will update post with other parts.

As for python, it works only in first example, second one says

CSRF verification failed. Request aborted

Note: I'm not using any auth on backend. Thanks in advance for extended answers

Inizio
  • 2,226
  • 15
  • 18
  • Shouldn't it be `e'/'` ? – Dawid Zbiński Aug 01 '18 at 05:53
  • @DawidZbiński Thought about it, but when I change it doesn't work any better – Michael Hearth Aug 01 '18 at 06:00
  • I'm not really into python, but as I go through other question it might be because you're not using regex for it. Try using `r'^$'`. Also, the first answer to [this question](https://stackoverflow.com/questions/9692625/csrf-verification-failed-request-aborted-on-django) might come handy. – Dawid Zbiński Aug 01 '18 at 06:07
  • Which version of Django are you using. If you are using 2.0, you should use [path](https://docs.djangoproject.com/en/2.0/topics/http/urls/#example) in urls.py. If not, you are defining urls wrong, check [this example](https://docs.djangoproject.com/en/1.11/topics/http/urls/#example). – zeynel Aug 01 '18 at 06:52
  • @fxgx I'm using 1.9.x version with python 2.7.15, even though my declaration isn't correct why does it work with python requests but not with angular? I haven't declared any csrf tokens and other headers, like only basic ones. – Michael Hearth Aug 01 '18 at 07:08
  • 1
    @MichaelHearth Somewhere in your views the CSRF protection is enabled obviously, maybe the Mixins or Generic Views you are using. You can disable it in urls.py. import `from django.views.decorators.csrf import csrf_exempt`, then use it like this: `url(r'^api/checks/$', csrf_exempt (views.GetChecksView.as_view())),`. If something works with python requests but not in angular, then you are doing routing wrong in angular, are you sure that you are addin `api/` prefix to the urls in angular? – zeynel Aug 01 '18 at 07:46

0 Answers0