How to retrieve information from a Django template - python

I have a django template that calls a python methods but I can't seem to find a way for the python method to retrieve information from the template such as user input. Could anybody tell me how to do this?

I think you're under a misconception (A reasonably common one) that because you can use django variables in the html template, the template can "send" information back to django/the database.
Instead, try to get into the habit of thinking in terms of request/response. The user requests a web page, the django server builds up the response using content from the database and an html template (via the template language) and serves it to the user. If the response page has a form in it, that's not the "template" sending information back to the Django server, that's the response web page providing the user will tools to make ANOTHER request (this time a POST request).
The template is merely a generic container that you fill up with your dynamic content to build up the entire HTTP response.

Related

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.

Pass js variable to Jinja template in Ckan

I want to pass javascript variable value to jinja2 template to query db according to some user requests.I can store user requests in js variable but I cannot pass variable value to Jinja.Do you have any idea or example?I use ckan project so I have to use Jinja template.I made some research maybe I have to use ajax request but how can I learn which function will be running in backend side as far as I know ckan use pyloans framework in backend side.How to render template in pyloans framework.Do you have any example?
As #E.Serra has already pointed out this is not possible: the Jinja-templates are executed on the server, while the JavaScript is executed afterwards in the browser.
Hence, you can either
compute the desired content when the page is first loaded and include it in your Jinja-template. For this, you would implement the ITemplateHelpers interface and implement a custom helper function which you can then call fro your template.
compute the desired content asynchronously after the page has loaded. For this you would send a web request from JavaScript to a CKAN API end point. If none of the existing API functions provide what you need then you can add your own using the IActions interface.

URL masking in Python Django

I have built a website using Django framework(www.example.com). While navigating the site the URL changes to like (www.example.com/home or /profile etc).
Is there some way that the current url is masked by a placeholder eg(www.example.com/home should be shown as www.example.com).
This should work throughout the website.
The url shown to the user would remain same (www.example.com) to where ever the user navigates on the site
HIGHLY NOT RECOMMENDED
Keep a single link in your urls.py, containing r'^$'.
Handle all requests purely as POST request.
Note: You'll have to hard code every parameter, and detect manually from where the request is coming.
To know how to work with POST request in detail check Django docs

How to append the #id in tornado url?

I am currently working on the WhiteBoard application. I need to append the #id to url. it is same as http://awwapp.com/draw.html#1205febd. I am new to tornado framework. How to call this url to view. Please explain me. Thanks.
#1205febd is not about back-end. It's a so-called target. When you have a link like <a href="#1205febd"> on page http://awwapp.com/draw.html browser will append that hash to URL automatically (on click).
If you want to perform some actions on back-end depending on that hash you should definitely to get the id on front-end and use some RIA framework (JQuery, AngularJS etc.) to send AJAX queries to your back-end (Tornado?) and render the layout in browser depending on its response.

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