Accessing a django-piston REST API via a django view within the same project - django

I'm building a small web service. To showcase what the service can do I am going to build a lite-weight interface. I'm having a hard time figuring out how my REST API and regular Django views can play nicely together.
Here's my setup:
Using django-piston to build a simple CRUD REST web service.
Using Django views with httplib2 to GET/POST to/from that web service.
Both are being run from the same Django project (and thus same web server).
Right now I have simple read REST service working in the browser. But when I try to use httplib2 from the Django view the request just hangs.
My questions:
-Am I thinking about this the right way?
-Is there a better way to accomplish this?
-Should my REST web service be a different project (and web server) than my REST interface?
Any help would be greatly appreciated!

Generally, I'd demonstrate an API working via unit tests, rather than live views, but can see how that might not be what you need.
So (in line with akonsu's comment above) if you're experiencing this problem local dev, it's the single threaded devserver that's blocking the API from running while the view is executing.
Have you tried a multi-threaded version of the runserver? Like this one?

Related

Django, FastAPI and DRF

I want to make a project, which uses Django as backend, PostgreSQL as database and FastAPI with Django REST Framework for REST.
Don't see any problems with making a project just with Django, DRF and Postgres, but face with difficulties when speak about FastAPI and DRF at the same time.
So there is no problem in connecting Postgres to Django, and there is no problem to make endpoints for DRF.
But how can I connect fastapi? Where to place endpoints and how to run all this stuff together?
In some examples I saw that FastAPI isntance is initiated in WSGI.py and then server runs from calling commands such like this:
uvicorn goatfish.wsgi:app
But i am not sure that it works like this when I mix more than only Django and FastAPI.
I want to use FastAPI for optical character recongnition and DRF for user registration, logins etc.
Are there any advices about making project with such a structure? Or maybe someone have a repository with such kind of project on github?
EDIT: Hope to see answers here, but for now I only see the solution in making classic Django + DRF app, then make FastAPI app with endpoints, run these apps on different ports and do some chain of actions:
From django app we load an image to form and when we submit this form we send POST request to FastAPI endpoint, which will run OCR process and then return JSON with recognized text and then will send this JSON to the Django Callback endpoint, which will process it and save to the database.
What do you think about such thing?
I think, you may:
Mix of fastapi+django. But this is only for replace DRF and use fastapi as rest framework.
Microservices. You may to run Django on one port, fastapi - another. And both may to use one shared database.
Mircoservices. All from point 2, but some api tasks (for example sign-in/sign-up) on Django and another - on fastapi.
As an alterantive you could use django ninja.
It uses similar concepts as FastAPI (Routes, Pydandantic Model Validation, Async Support) but is an Django app.
Which of course is more the monolithic approach but still valid.
Well, so after few days of thinking about I decided, that there is no sense in a question which I asked :)
Here we should talk about microservice architecture, where such kind of problem just doesn't exist. All we need is to make as much services as we need in our project using any framework (Django, FastAPI, Flask, Ruby etc.) and make connections between them.
As example I can run my main Django server on port 8000, my FastAPI server on port 5000 and my DRF service on port 6000. And then I can just do whatever I want from my main Django server making requests to endpoints of FastAPI and DRF.
So that's very simple example, now I'm diving deeper into microservice architecturing, but that's definitely what I need.

What are the options to configure a reactjs and django application?

I am using reactjs for frontend and Django REST API for the backened. What is the best possible way to integrate both?
which of these two is a good option?
Running two servers for frontend and backend?
or
replacing django templates with reactjs?
Your help is highly appreciated.
Few options here
Django templates with react.
Not my preferred method. Essentially, you are blending django templating and jsx. The benefit here is low over head. It requires little configuration and allows you to write react and leverage the django templating language in the same file. If you need to get something up and running quickly, its a great solution. Have a look at this library https://github.com/Frojd/django-react-templatetags
Using django webpack loader
This will allow you to separate your react code from django code but still keep all your code to one repository. You need to configure django settings to find your react code. Then on your prod/dev server, have your web server point to the directory where your static react code lives or write a django url and view that will serve the react apps index file. It will be located in /static/ after you configure correctly and run python manage.py collectstatics. Benefits here are that it keeps the code to one repository but still isolates the python and javascript code. This is a middleground solution of the three. Quick note. You won't have react hot reload with this method for development. Here is the library that helps you configure this setup https://github.com/owais/django-webpack-loader
Having 2 separate applications
Similar to what you are doing right now, have a separate react repository, either served by a nodejs backend or deploy the code to a cdn service like amazon s3 and serve the one page app from there. And then as its counterpart, have your django app on a separate server with its consumable rest api (will need to configure allowed cors) . This method requires a lot of operational work: deploying, configuring, and management of 2 separate code bases. If you have the time and resources I do recommend this setup. The decoupling of the 2 apps allows this solution to scale the best
What do you mean two servers? You mean two projects/repositories?
Yes, you can keep frontend in the separate project. It make sence if you have multiple clients for your backend (like mobile apps and web). Different developers can have permission to edit only their repositories. Also it make sence if you are going to use some microservices structure for your project. But more simpliest way is to keep frontend and backed in the same project. Try to check some tutorial about Django+reactjs apps.

Running a meteor app as part of a wider django project

I'm currently working on a project which would require some realtime functionalities such as Multi-user chatrooms etc.
Ideally, I’m looking to have meteor run the chat application(on a different port) and mongodb act as message broker to the django back-end which would take care of user registration , management and everything 'non-realtime' related.
This would involve setting up a reverse-proxy which would redirect to a different port based on the url (please let me know if i'm wrong in this)
Would this be possible(or even advisable)? Another option would be to implement the same with tornado. but I have no experience with building tornado-based apps and rather do this with a framework I’m comfortable with.
Thanks,
You can have Django serve the Meteor front-end while providing access to its data using django-ddp, giving you some distinct advantages:
Continue to serve your existing Django project/apps.
No extra services or ports to manage.
Scale out by simply adding more front-end Python/Django servers (server to server IPC is done via the existing database connection).
Use django.contrib.auth user accounts in your Meteor app.
Familiar Python/Django code (no "callback" style such as with Tornado).
Use time-tested, trusted relational databases.
Use Django migrations to effectively manage schema changes.
There's a Gitter chat room where I can give you assistance if you need it.
DISCLAIMER: I'm the author of django-ddp.
A meteor application is more than capable of handling the user registration flow and many other things. Why not just build the application entirely in meteor? Your application sounds like a perfect candidate for meteor, with realtime interaction with your database at the core.
The other option would be to use swampdragon which adds realtime data binding within django. It allows for simple bi directional communication between the server and the client. Again, essential for a chat application. It nice and easy to get setup and running as well.
Are there any specific reasons to not implementing your application in one framework alone?

Angular JS and Django

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

Apache/wsgi/django configuration

We have two django applications running on the same server that interact with an API that uses oauth. They function as expected, communicating with each other, when run under the django development server. However, when deployed using apache/wsgi they don't work together.
(To be more specific, one application is an instance of the Indivo server; the other one is a custom application that interacts with Indivo.)
What is the best way to trouble shoot this?
Make sure that the Django instances are working by themselves first. For example, one app could be started under Apache, and the other using ./manage.py runserver. Reverse which one is running using Apache and verify that all works as expected.
Use the Apache error logs to look for errors such as failed requests.
Since one of your apps appears to implement a web API, use something like the Google Chrome Postman App to exercise the site from a web browser.
Learn how to use the Django logging framework to log information about your apps as they execute.