How to right code in Django and AngularJS? - python

Now I am working with Django on server side and jQuery on client. Django views functions return templates with js code.
How it should look using AngularJS. Should I return JSON from Django and render response using JS ?
Many thanks!

It really depends on what you want to do, there are many ways, but yeah, ideally your routes and templates are handled in AngularJS, Angular requests information from Django or POSTs information, and they communicate using JSON.
You need to put your Angular templates and files in the static folder, you can use grunt or django-pipeline to better manage all the files you have.

Related

Does Django render web pages on the server side?

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.

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

How to serve django templates to javascript

I am using django on the server side and obviously javascript on the client side. Now i want to use the plate template engine on the client.
What's the best way so serve django templates to the client? We taught of some ways doing that.
Create a view that serves the raw templates.
probably not the best method
Copy the needed templates to the static folder.
this could be done with a custom static files finder
the broser is able to cache the templates
Provide the templates using a template tag which puts the raw template into a javascript variables.
templates received this way can not be cached seperatly
is a django app out there that makes this easyer?
The reason i need the templates on the client is, that i want to use the same clients on the server and the client side. When the page is first loaded, the full template is rendered on the server, when navigating trough the application only the needed data gets loaded and the page change is done using push state.
If you need to be able to have A) dynamically generated plate templates, or B) dynamically created plate templates (e.g., entered into the DB via the admin, etc.), You'll want to go with 1 (not a bad thing - django is made for serving text content, so as long as you need to have it in a dynamic manner, there's no problem doing it). 3 is a bad choice, because it means that a browser can't cache the static resource (if it's output into each page)... unless you need different plate templates for each page of course.
If you don't need A or B from above, I'd just stick the templates in your static dir, as you mentioned (e.g., collectstatic or simply add them to your repo, if they're a part of your app).
Regarding an app that makes this easy - you could look at Django Chunks (output a static chunk into a place in the page, like `{% chunk "header-snippet" %}), but I don't think you need that.

Can you serve static HTML pages from Pyramid?

I have a Pyramid app using Mako templates and am wondering if it is possible to serve static HTML pages within the app?
For the project I'm working on, we want to have relatively static pages for the public "front-facing" bits, and then the application will dynamically serve the meat of the site. We would like one of our internal users to be able to edit some of the HTML content for these pages to update them.
I have my static folder that I'm serving CSS and scripts from, but that doesn't seem to really fit what I'd like to do. I could create views for the pages and basically have static content in the mako templates themselves but I think the application would need to be restarted if someone were to update the template for the changes to appear? Maybe that's not the case?
Long term I would probably do something like store the content in a db and have it dynamically served but that's outside of the scope at this time.
Is there a reasonable way to accomplish this or should I not even bother and set up the public pages as just a regular static HTML site and just link to my app altogether?
Thanks!
You can serve static html from Pyramid by using views that return pre-fabricated responses. You'll have a more fun time doing it though by just having your web server serve static html if it finds it, otherwise proxying the request to your Pyramid app.

What is the best way to serve static web pages from within a Django application?

I am building a relatively simple Django application and apart from the main page where most of the dynamic parts of the application are, there are a few pages that I will need that will not be dynamic at all (About, FAQ, etc.). What is the best way to integrate these into Django, idealing still using the Django template engine? Should I just create a template for each and then have a view that simply renders that template?
Have you looked at flat pages in Django? It probably does everything you're looking for.
If you want to just create a template for each of them, you could use the direct_to_template generic view to serve it up.
Another option would be the django.contrib.flatpages app, which would let you configure the static URLs and content via the database.

Categories

Resources