I currently have an app which uses HTML/CSS/JQuery. As of right now all my data is saved in Jquery (and therefore not saved between sessions), but I am learning Django and want to learn how I would save that JQuery data into Django models. So how do javascript and django communicate in order to save information from one to the other?
Related
Here's the view that updates the timestamp of a model in database whenever it's called,
def data(request):
new = Data.objects.filter(user=request.user)
new.update(timestamp=timezone.now())
return HttpResponse('')
This view is related to this URL,
url(r'^go/$', views.data, name='data')
Everything is fine, but how can I call this view & update the database without refreshing the page?
This is a great question as its something that may not seem as logical in django as say node or js based server and client interactions. Let me give a generic response then go more detailed.
Django is a web server. It has some newer functionality that allows it to more easily interact with javascript than it could in the past, but it is normally used in conjunction with a javascript framework. I'll normally build a django app w/ django-rest-framework and reactjs. So my workflow is normally to build the datamodel, serve up the page with compiled js as a static asset, and have those two communicate via a rest-api stood up with DRF.
Specifically to your question, you can simply have your django model communicate with the server via ajax posting to endpoint in your app. An example of a jquery/ajax post is available at this link. The only other thing you will want to look into is supporting csrf which Django discusses in the docs.
Let me know if this helps. If it does but requires specific snippets, let me know.
I think what I'm trying to do is very simple, but I'm having a really bad time trying to search this on the internet. I have this populated sqlite db file in my server, and I want to import its structure and data into my web application. This file is generated from another server-side app in a path which is accessible to the web application.
Your web application should access the database with its proper adapter (depending on the language and the framework) and then provide the data to the front-end as a set of services. It strongly depends on the amount of data and its variability whther you want to serialize all the data at application load time, or just to provide data access using several endpoints (by using the sqlite database as the repository for your application models).
It's not much different from a normal web application, provided you don't want to write on the sqlite database and so consider all your models read-only.
I have a django project that uses postgresql. I am trying to use angular as a front end in my html pages. Which is the best approach to use the angular js controllers in the django project to connect to the django models and the database to perform basic operations like insert, delete, update etc?
Currently i just have static data that i defined in my angular controller. But i would like to know how to get the data from the database dynamically. Any advice will be helpful
As per my experience with Django and AngularJs, first you should make api in django to get the data from database. Afterthat, in single page application, you should make angularjs service for each api in controller. After rendering data, you can submit / modify your data either through api or through form submission(with form validation).
I am creating a web application using Ruby Sinatra. However, I think this question is more related to html/javascript.
In the front end I am using d3.js for some pages. I want to create and use data files based on user input. So for each user session I want to create a data file for that session.
I think I can create the custom html/javascript for each request.
Is there any other way to do this, such as passing the file name to the javascript?
You can use sessionStorage (some examples is here http://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/)
I am new in restful programing. I have a django website, my goal is to load part of the website asynchronously.
By default what I do now is:
call a url (e.g. localhost:8080/index) -> Routes the Django view -> View takes data from database (mongodb if you are curious) -> View gets the template -> Render all together and send back to browser
What I want to improve is:
When I have opened a url e.g. localhost:8080/index and I trigger an event (e.g. click a <a>) to send a request to my database and load other data.
My question:
What is the step I have to do to bridge the javascript / ajax with my mongodb safely?
Additionally I am considering to use a front end MCV framework (I think about backbone/I have limited knowledge as well) to handle the front-end requests. E.g. localhost:8080/index#2 returns objects with id : 2 of my database.
You should look at the following rest API frameworks for django. They will help save you a lot of time.
Django Tastypie
Django Rest Framework
I have used both and personally like tastypie better. Once you have integrated one of these in your project you can use any front end javascript framework like angularjs or backbone or even not use one and simply make a call to the resource url to get the data via ajax.