I am trying to start a simple django app. I have been on it for days. I was able to this in flask in a few hrs.
I need advice on connecting to an external database to grab tables and display them on django pages.
This is my code in flask
#app.route("/topgroups")
def topgroups():
con = sql.connect("C:\\Users\\win10\\YandexDisk\\apps\\flask\\new_file.sqlite")
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("SELECT domain, whois, Traffic, Groups,LE,adddate FROM do_1 where Groups in (75,86,66,58,67,57,68,85,48,56,76,77,46,65,47,64,45,55,74,54,44,33,34,43)")
rows = cur.fetchall();
return render_template("index.html", rows = rows)
I will give you the python answer but read until the end because you may be losing a lot on Django if you follow this approach.
Python comes with SQLite capabilities so you don't even need to install packages Python Docs:
Connect
import sqlite3
conn = sqlite3.connect('C:\\Users\\win10\\YandexDisk\\apps\\flask\\new_file.sqlite')
Want to ensure read only? from the docs
conn = sqlite3.connect('file:C:\\Users\\win10\\YandexDisk\\apps\\flask\\new_file.sqlite?mode=ro', uri=True)
Use
cur = conn.cursor()
... (just like for flask)
Note/ My recommendation
One of the biggest advantages of Django is:
Define your data models entirely in Python. You get a rich, dynamic database-access API for free — but you can still write SQL if needed.
And you'll loose a lot without it, from basic stuff like what you asked to even unit-testing capabilities.
Follow this tutorial to integrate your database: Integrating Django with a legacy database.
You may set manage=False and Django won't touch in those tables,
only create new ones to support the app.
If you just use that DB for some special purpose then give a look at Django Multiple databases
Related
For now I always created the structure and logic of my backend with Django. But when I insert data into the database I alwas did that directly via a http request to a php script.
When the projects grows it is getting pretty messy. As well, there were always complications with timestamps from the database to the backend.
I want to eliminate all these flaws but could not find any good example If I could just make a request to a certain view in Django, containing all the information I want to put into the database. Django and the database are on the same machine, but the data that is being inserted comes from different devices.
Maybe you could give me a hint how to search further for this
You can just create a python script and run that.
Assuming you have a virtualenv, ensure you have to activated. And put this in the root of your Django project.
#!/usr/bin/env python
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
# Import after setup, to ensure they are initiliazed properly.
from myapp.models import MyModel, OtherModel
if __name__ == "__main__":
# Create your objects.
obj = MyModel.objects.create(some_value="foo")
other_obj = OtherModel.objects.create(title="Bar", ref=obj)
You can also use transaction to ensure it only commits all or none.
from django.db import transaction
with transaction.atomic():
obj = MyModel.objects.create(some_value="foo")
other_obj = OtherModel.objects.create(title="Bar", ref=obj)
Should one of the creations fail, everything is rolled back. This prevent from ending up with a half filled or corrupt database.
I am a beginner and this is my first full-stack web development project and I have completed the front-end part and created the related tables using MySQL... and now I want to link the tables to front-end using nodejs. How do I proceed further?? Is it proper to use workbench in the first place for a full-stack project?? Please guide me.
SQL Workbench is just an IDE for mySQL so thats fine for building out your DB, setting permissions etc.
Your question is not one that is simple to answer, simply because the steps in creating, setting up a full web app is not that simple to explain..
There are few things you will need to do to hook this up
Ensure you have a mySQL middleware installed
Ensure you have the routes created
Use a templating engine like EJS
Once you have the basic flow working ( meaning you can hit your page and it returns the correct page ) you will want to then hook into the DB before sending the response object back to the browser. A typical flow would be, on your 'get' response, you would perform the mysql 'select'
This should be promis based but will depend on the actual package you install, I don't use mysql but a postgres command is something like
query.pool("SELECT is, name, des from table where id = '10' ").then(results => {
//put in your response code here to send back to the page
}).catch( e => {console.error(e)})
The response code portion is where you would send things back to the page, in an ejs template would then be able to access the response and display the data.
I know, this is a bit light on full explanations and that is because the proper response would be huge!
Judging by the question I would guess you are a bit new to node / DB etc ( sorry if you are not ) I think what may be very helpful is to watch a few youtube videos on setting up Node and EJS ( or any templating engine for that matter )
That should give you the basic understanding and setup of the project.
I have a pyramid game server app that uses sqlalchemy to read/write to postgres database. I want to read a certain table(call it games) from the database at the time this app is created. This games data will be used by one of my wsgi middleware, that is hooked in the app, to send statsd metrics. To do this, I added a subscriber in the main function of my app like:
config.add_subscriber(init_mw_data, ApplicationCreated)
Now, I want to read the games table in the following function:
def init_mw_data(event):
...
...
Anybody know how I can read games table inside the function init_mw_data ?
It based how you configured your application.
Default sqlalchemy template from pyramid-cookiecutter-starter have dbsession_factory.
So, you can do something like this:
def init_mw_data(event):
registry = event.app.registry
dbsession = registry['dbsession_factory']()
dbsession.query(...)
...
I have a project that is connected to an external database and just have view access to it. So I created my models with managed=False flag.
I was wondering how can I find out in django that any change in that database is happened. Is there any solution in django or I should find a method to communicate between that database and my django app. like socket, database triggers and ...?
More details:
Image my models is like this:
class Alert(models.Model):
key = models.CharField(max_length=20)
class Meta:
managed = False
Now i want to be notified in django each time the database is updated. I want a signal to capture database updates and do something in django?
I have a Rails app with MySql DB. It houses a lot of data on which we need to run some reports and create some dashboards. It requires some calculations. What are some ready to use solutions for this? Gems, tools or frameworks that can help getting it developed fast would help.
For charts you can use Fusion chart gem in rails
https://github.com/fusioncharts/rails-wrapper
In order to draw charts you need data in hash Like
Create one lib which will return hash data
example
class Dashboard
def initialize
# initialize here
end
def data_set
{
user_details: User.details,
profile_details: Profile.details
}
end
end
In particular model you can fetch data using queries.
For charts, you have to render charts as per documentation