Can i have Django urls and Vue routes in the same project? Can i have Django urls and Vue routes in the same project? vue.js vue.js

Can i have Django urls and Vue routes in the same project?


My personal opinion, it's not worth it to keep a bunch of server side pages just for sign-up, login, ... Managing both server-side pages and front-end pages in long run is a headache. But if you like that way, here are my suggestions.

For authentication, use Django auth. No matter if it's a server side HTML page or it's an API end-point. Django auth is simple and secure. Don't roll your own auth, don't store tokens in localstorage or so.

Fully separate these 3:

  • Front-end URLs (i.e. routes stored in Vue)
  • Back-end page URLs (i.e. HTML pages severd by Django)
  • Back-end API end-points URLs (i.e. the ones user never see, only Vue uses them under the hood)

They can be on separated domains but it can be just by a path prefix as well. As you yourself suggested in a comment.

Now when user visits some page in BE, it will use server side rendering and every user click is a browser refresh. Until you hit a FE URLs, then your front proxy should redirect user to FE or you'll serve JS files directly from Django. After that every user click is handled inside Vue without a refresh. If user hits a URL prefix that's for BE, then FE needs to do something like document.location = "/server-side/some-page.


BTW few days ago I answered another question that was similar to this, maybe you find the answer or comments there useful.


  1. So in order to log in from the SPA i need to send a csrf token, and in order to get the token i can create a Django view that returns a CSRF token to the user so that it can be used to login. Wouldn't it provide attackers a way to attack my server (stackoverflow.com/questions/43567052/…)

My suggestion is to turn CSRF protection off and instead make session cookie samesite=Lax (I think that's default in newer versions of Django). All major browsers support this and it prevents CSRF.

Otherwise you can read token from another API or from cookie like here

  1. So on production i will use Nginx to have the Vue app and the Django backend app on the same server and domain, but on development how can i do that? If i run the two apps on different terminals, won't django consider the Vue app to be in a different server?

It can't understand what server it is. The only thing you should care is the domain of the cookie. It should be set on your BE domain. When running on local both FE and BE are running on domain "localhost" so there should be no issue.