Does Django render web pages on the server side? - python

Does Django render websites and serve them from a views folder similar to how the express framework works in JavaScript? Or does it render pages on the client side similar to an applicatipn running express in the back-end and some fron end framework such as Vue.js in the front-end

Django can do either, but it is designed to render on the server side in its default configuration, not the client side.
To use server side rendering, use Django as intended and according to the many tutorials out there.
To use client side rendering with something like React or Vue, use the Django Rest Framework.

If you are talking about the template language, it happens on the backend. This is true of every framework and every language. In the templates, the placeholders are replaced, they produce a valid html/javascript page, and that's what is returned to the browser (or, to whatever accessed that url). Essentially, it is a shortcut for creating an html file by concatenating strings.
A suggestion: don't use the template language, in any backend framework. Just use django to create rest services that return the data, and use a front-end framework like Vue/angular/react to create the actual front end part.

It's now 2022 and you should definitely check out https://www.reactivated.io/.
It effectively is plain old Django with server-rendered React.

Related

Why do I need web api to link between django and other js framworks

why do i need a web API to link between django and other JS frameworks for example django with angular?
And is it necessary to build a web API like (REST API) to link between back end and front end?
Django is not JS library. It's a Python-based server framework. But your JS library communicates with a server via REST Api. So you need a web API to link between django and other JS frameworks.
Without more work than it is worth, the DJango and Angular frameworks probably cannot pass data between one and the other, since both run at the same logical level in the stack.
REST APIs are the generally accepted mechanism for communicating between SPAs implemented in different frameworks.

Django and React

I haven't been able to locate anything saying one way or the other, but does anyone know if you can have function based views in django and have react as your frontend? No use of templates.
If so, would it look like this?
# fad index route
def fad_index(request):
context = {'fads': Fad.objects.all()}
return render(request, 'fad/fad_index.html', context)
But instead of fad_index.html, would it just be the route that you want it to take in react?
When you use Django views, whether class or function based, you are required to render html responses, so there is no point on using React this way since the front-end functionality is already embedded in the html you are rendering.
React is thought for creating front-end applications that run independently from the back-end application. For that, the back-end application must provide specific url points for fetching or posting data from and to the front-end. These url points are known as end-points, and this back-end architecture is known as API, and come in two main flavors, SOAP and REST, and differ, basically, in the format of the data that comes from and goes to the front-end. SOAP APIs use xml, and REST APIs use json.
If you have a REST API back-end, you can use ajax in a React front-end to fetch and post data from and to the back-end's end-points.
For making a REST API back-end using Django, you need to complement it with another library called Django Rest Framework, that allows Django views to render json packages instead of html, so your React front-end can use these end-points.
React is simply a component composition library. You can use it to render single components on a web page or as the rendering layer of a more complex Single Page Application (SPA)
The general best practice when creating single page applications is to send the data over in a JSON format using a REST API. JSON because it can be parsed in almost any language and REST because the server should not care what the client is specifically doing.
There is a module called django rest framework that allows you to return raw data from REST style endpoints in a JSON format.

Can I use DRF to create a page based site (e.g. a blog)?

Can I use Django Rest Framework to create an "ordinary" site - like a blog, with templated HTML pages ala "normal" django?
The reason I ask this is that I am building a website (sort of a blog), but for sections of the site, the functionality will be provided by making CALLS to a REST API.
So, my question essentially is this:
Is DRF equivalent to (all the features/functionality of django) + ability to create/use RESTful APIs?
Django Rest Framework is an add-on to Django. It doesn't replace Django; it's just another app. You still create your models via Django, and can use views and templates as normal.

Python backend and html/ajax front end, need suggestions for my application

Currently I have written a python script that extracts data from flickr site, and dump as a python class object and YAML file.
I was planning to turn this into a website:
and a simple html front page that can send a request to the backend, triggers the python scripts running on the backend
the response will be parsed and render as a table on the html page.
As i am very new to python, I am not sure how to plan my project. is there any suggestions for building this application? any framework or technologies i should use? For example, i guess i should use ajax in the front end, how about the backend?
Thanks in advance for your suggestions!
You can look on Django or Flask.
As for me, Flask is much better for you, because it smallest and simplier. You can read about building Flask app here:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
And about Django here:
http://www.djangobook.com/en/2.0/index.html
I used both for building applications with AJAX.
Please note that Django has better Python 3 support.
Also you can see on Bottle or something other. Try google if you'll dislike Django and Flask. For example, comparison of Python web frameworks:
http://www.sixfeetup.com/blog/4-python-web-frameworks-compared
you should use Django framework for your app. You can integrate your scripts into Django views. And you can also use the loaddata system in order to insert your yaml data into the database

Web based wizard with Python

What is a good/simple way to create, say a five page wizard, in Python, where the web server component composes the wizard page content mostly dynamically by fetching the data via calls to a XML-RPC back-end. I have experienced a bit with the XML-RPC Python module, but I don't know which Python module would be providing the web server, how to create the static content for the wizard and I don't know how to extend the web server component to make the XML-RPC calls from the web server to the XML-RPC back-end to be able to create the dynamic content.
If we break down to the components you'll need, we get:
HTTP server to receive the request from the clients browser.
A URL router to look at the URL sent from client browser and call your function/method to handle that URL.
An XML-RPC client library to fetch the data for that URL.
A template processor to render the fetched data into HTML.
A way to send the rendered HTML as a response back to the client browser.
These components are handled by almost all, if not all, Python web frameworks. The XML-RPC client might be missing, but you can just use the standard Python module you already know.
Django and Pylons are well documented and can easily handle this kind of project, but they will also have a lot of stuff you won't need. If you want very easy and absolute minimum take a look at using juno, which was just released recently and is getting some buzz.
These frameworks will handle #1 and provide a way for you to specify #2, so then you need to write your function/method that handles the incoming request (in Django this is called a 'view').
All you would do is fetch your data via XML-RPC, populate a dictionary with that data (in Django this dictionary is referred to as 'context') and then render a template from the context into HTML by calling the template engine for that framework.
Your function will just return the HTML to the framework which will then format it properly as an HTTP response and send it back to the client browser.
Simple!
UPDATE: Here's a description of how to do wizard style multiple-step forms in Django that should help you out.

Categories

Resources