I am building a site using angularjs. The backend is django and django-tastypie.
currently we hosted the frontend stuffs(angularjs) in a seperate domain(different from django domain).
we seperated all angularjs htmls from django
it is the correct way?
i found some others were rendered the angularjs templates through django views.
Have a look at https://www.youtube.com/watch?v=vWJorwEQWLk (which uses django rest framework, but the principles are the same). Normally, you should not use different domains to host angularjs and django (this will complicate sessions handling, etc.).
I don't know about the *correct * way necessarily, but separating Django and Angular templates is certainly a very sensible thing to do. From the Django point of view, Angular templates are just static files.
What I've done in the past is to have a single base Django view that includes a few js variables set from template vars, then served the rest of the Angular stuff via static files and ajax.
Related
I've made a project using Django and Bootstrap (I plan to use React for the frontend). Then I started building REST API with DRF and it seems that my DRF views are pretty similar (if not duplicative) to my original Django views such as DetailView, UpdateView and etc.Now I wonder if I can keep my old views or should I completely replace them with DRF ones? Or should I use DRF views for CRUD and keep particular Django views?
And another qusetion: am I right that when working with Django and DRF Django becomes just an ORM provider also responsible for business logic, authentication and some other features?
Thanks.
Django is a fullstack web development framework. Here you can progress in different ways. You can use Django and ReactJS without having to develop any RestAPI. It depends on how you approach it with ReactJs.
Do you want to develop the frontend entirely with ReactJS? If so, you can develop your backend directly using DRF and consume APIs on the frontend only with React.
While developing a full stack with Core Django, if you want to make the Frontend dynamic, you can use Django and DRF together, or you can develop APIs without using DRF.
You can use DRF and Core Django together for different scenarios, or you may only need one of them. It depends on you and your project.
Edit for second question:
Django Rest Framework requires Django. Also, with Django you can do everything DRF does. By writing more code of course. This process provides many conveniences with DRF; like authentication modules, json serializers..
I'm just wondering since Jquery is used less and less and instead of classic use with Ajax, if it is possible to let for example React or Vue handle all of the functionality where Javascript would be needed with DRF in one hand , and consider all of it as statics.
And in the other hand, keep working with Django and regular templates.
i'm just asking about the possiblity to do so.
Yes. There are many tutorials available for using Django, some with Wagtail as a CMS, with DRF as the backend. You can use a single template, and Vue or ReactJS for the front-end.
Here's a workshop video that might help get you started from the folks at Learn Wagtail: https://www.youtube.com/watch?v=xUWd3o6z2bk
(I chose Wagtail, because it is a popular Django CMS with DRF API endpoints out-of-the box.)
I'm in the process of porting my website front-end to Angular JS. The backend is based on Django.
I initially planned to develop a restful API for Angular to fetch data from backend.
However, I've found that the easiest way would be to generate partials templates in Django. Is there any disadvantage of this approach that I'm missing?
Currently I don't plan use any data binding.
The problem with your approach is that you're side stepping one of the things Angular is spectacular at which is routing.
I've recently started creating a Django website which uses Angular Js and had a difficult time wrapping my head around how I would use django templates with angular templates.
To sum it up;
I created an app named band which has one page.
I setup Django rest framework to handle crud operation via a restful service for the band app.
I have one template for band, which does NOT use inheritance or anything since this interferes with routing. (I struggled with this a bit, maybe i'm off but below is my determination and direction)
The reason for the struggle is typically in Django you have a server side web framework. Angular is a client side framework. Django separates it's template directory from static directory content. Your JavaScript angular files will be looking for template includes which it needs to find. My particular production server setup uses Django (Mod-WSGI) to handle rendering the template while static content like images, js and css are rendered by NGINX. Basically I just decided okay since Angular aims to be for SPA (single page applications) I would simply have a django template for band which does not inherit from a base master page or anything like that and mostly leave up everything in that band template to be rendered and updated via angular.
After reading this article about separating server and client, Separate REST JSON API server and client?
I want to know if this problem also exists in Django.
In Django, are these 2 separate phenomenons?
Django is MVC. Take out the view, have django as the backend and just send JSON. Create a separate say Emberjs or Angularjs app. Make these clients access backend's REST resources. In this case, how can you put the projects together to deploy to Heroku? Can you just deploy a "JAVASCRIPT-HEAVY-CLIENT" to Heroku, and have it just talk to ur REST server?
Since Django is MVC, don't completely take out the view, but integrate Emberjs/Angularjs into the View, but still use REST resources? In this way, you can put all the angularjs components/js files into the static folder and deploy to Heroku.
Are these the same? and realistically, how do you put them together to be able to deploy to Heroku?
Heroku is a application server platform. It's not really designed to serve static code.
The approach I've previously taken is to build the Django part normally, and have a single view that serves out a bootstrap template for everything under the JS App root.
Say I have a {Angular,Ember} app living at mydomain.com/app/, then everything under that will serve the bootstrap template (which includes serialised values queried from database) and calls the JS boostrap method to startup your app, and then the app takes over routing from that point, and renders out it's views.
At this point, all the data for the views is coming from django-rest-framework/django-tasypie.
Using this method and leveraging django-pipeline & django-boto's S3 storage backend, You should be able to serve a decently sized project with Django & {Angular,Ember}
This might be a silly question that might relate to my ignorance about angular js, but is it possible to use a Django framework mostly for backend and database interactions, in conjunction with an Angular JS app/website?
I know angular js still requires Node Js for serving the data so, it seems like that would be a conflict. There was a similar but slightly more technical and specific from what I was looking for here: Django, REST and Angular Routes
What alternatives do I have if I want to experiment with Angular JS but need some database functionality?
Also how do you go about the fact that your code will be open to everyone?
Not a dumb question. But yes, it is totally doable. I put together a git seed for an angular web app with a django rest framework api. Check it out, I think it is exactly what you are looking for. Clone it down and try it out! Easy instructions to follow.
Angular-Django Seed
There is no dependency on NodeJS whatsoever. Your web framework handles server requests, and your angular app runs in the browser. You would use Django to serve the files used by the angular runtime, typically this includes a mix of HTML, JS and CSS files.
Your angular app may request data from the server, in which case it would 'call into' your Django code via an XHR request using the built in $http service (or possibly $resource service).