At the moment I have a django backend with jquery frontend. Looking to move to reactjs. The issue is that I don't have a server per se, so I can't make calls to django from frontend.
My website is not publicly available, but sent as a html document by email. So django is used just to crunch a lot of data and make a lot of plots and tables, then I use django-bakery to output all of that into a static html file that gets sent by email.
My question is how would you go from a standard django html template:
<div class="card">
{% for k, v in tables.items %}
<div id="{{ k }}">
To a reactjs template, without making calls to a django server. How would I get the data from django?
Thanks!
Related
Good day,
So I have made a mobile Android/IOS app that communicates with django backend through the http requests. As you understand the backend is hosted on some https://www.example.com domain...
However, in case a user accesses that domain or any extensions (/home, /profile and etc..) from any web browser from any platform, I want to display just a plain page (maybe the name of the app). How can I do so?
Thanks
If you don't want to add new middleware you can add this in your base.html template. Other way would be to write separate middleware in which you check user_agent and redirect to specific page containing your app name
{% if request.user_agent.is_mobile %}
NAME OF YOUR APP
{% else %}
Normal page
{% endif %}
I have a Django app which renders html template and loads data into html via ajax from front end(based on url's dynamic id).
For eg., this is my site's dynamic url pattern "www.mysite.com/order/< some dynamic id>", I want django to cache & return all the requests to this url prefix r"^**www.mysite.com/order/**" to render the html without hitting my views function.
Currently, each dynamic id in the url pattern ("/order/100","/order/101","/order/102..etc")is coming to my views and rendering the same template for each.
I'm expecting, anyone visits with prefix "/order/" the html template needs to be rendered automatically without hitting my views. I'm currently using redislite for caching, Please help me with a solution.
UPDATE : It seems using a front-end framework like React or Angular JS will solve my "avoiding html template rendering" case with django. I didn't wanted django to waste time on rendering html, so I thought of caching the templates.
You can cache per-view using cache_page. This may be most appropriate for what you're trying to do.
from django.views.decorators.cache import cache_page
#cache_page(60 * 15) # 15 mins
def my_view(request):
...
Or you can cache fragments of templates, where you use the cache template tag and a time (in seconds) to cache the block of markup;
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
This type of caching can be as simple as above, or you can do more complex caching by varying on the logged in user, for example. You can read the docs on that here
I'm trying to set value to Django session within the template and then posible used in the view.
I'm doing something like this
{% block body %}
<html>
{% request.session.fav_color="red" %}
<div> Is your favorite color {{ request.session.fav_color}} ?</div>
</html>
{% endblock %}
There are several reasons why this may not be a good idea:
Templates should deal with presentation only. If you are placing logic in the template it is supposed to be presentation logic. Placing business logic in the template is a violation of the SoC (separation of concerns).
Rendering the template often is one of the last things you do in a view so it is hard to get the cat back in the bag if you have already sent the data to the browser (and if you rendered the template but haven't sent data down the pipe you can just update the value in the view instead of trying to do it in the template).
If you are interacting with the user the traditional way to do it is using a form (even if it is a form with only the submit button) - or posting data to the server in the frontend using AJAX.
I am building an application with Django as the backend and React js for making the interface of the application.
I have a set of Posts which I want to display.
Currently, the approach which I am following is -
Get the template having the compiled js code linked to it.
Then again make get call to get the posts
My question is - In this current approach I am making 2 GET calls to the backend, one for rendering the template and then again for getting the Post.
What is the best way to achieve this? Is this the usual flow how applications are built using Django and React JS?
First off: I don't see anything wrong with doing this in two requests, because one loads the application itself and the second loads the content. To me this seperation makes sense and might turn out to be useful in the future if you want to reuse say the Endpoint, that yields the the posts(i.e. the content).
Answering your question: If, for whatever reason, you absolutely want to load everything with a single GET, a good way of doing so, would be to pass a list of posts to the context as a JSON-serialized object and then load these into the JS-context within the Django-template.
i.e. in the view.py:
from json import dumps
def view(request):
context = {
'posts':get_posts(),
}
render_to_response('django_template.html', context,
context_instance=RequestContext(request))
def get_posts():
qs = Posts.objects.all()
return dumps({'posts': qs })
in the django_template:
{% block content %}
<div id="root"></div>
{% endblock %}
{% block javascript %}
<script>
var INITIAL_POSTS = {{ posts|safe }};
</script>
<script type="text/javascript"
src="PATH_TO_STATIC_REACT_ASSET.JS"></script>
{% endblock %}
you should now have your posts in your JS context and can load them in your React component. Once again: I would agree with Daniel Rosemans comment
First Quesiton:
This form submits to demo_form?name=ABC
<form action="demo_form" method="get">
name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form>
Is there a way to make it submit to demo_form/ABC/?
Second Question:
Even if users don't use my form, if they use a web crawler to simply visit demo_form?name=ABC or demo_form/ABC/, it would yield the same result. I want to prevent that. What's the best way of making those two URLs only valid if the user submit the name via my form? I am learning django so hopefully the solution would work with django framework.
Thanks in advance!
Is there a way to make it submit to demo_form/ABC/?
You could intercept the submission in JavaScript, construct the URL manually, then set location. That would break if JS wasn't available.
More sanely, you could send an HTTP 301 redirect response when you get the request for demo_form?name=ABC
What's the best way of making those two URLs only valid if the user submit the name via my form?
Generally speaking, visiting a form should not be a pre-requisite for anything involving a GET request. A large portion of the point of GET is that the results are bookmarkable, linkable, etc.
It would be more understandable if it was a POST request, as those are intended to change data on the server and you will want to protect against CSFR. The standard protection against CSRF is a token stored in the form and in a cookie