0

I am just beginning learning Django and working through the tutorial, so sorry if this is very obvious.

I have already a set of Python scripts whose ultimate result is an sqlite3 db that gets constantly updated; is Django the right tool for turning this sqlite db something like a pretty HTML table for a website?

I can see that Django is using an sqlite db for managing groups/users and data from its apps (like the polls app in the tutorial), but I'm not yet sure where my external sqlite db, driven by my other scripts, fits into the grand scheme of things?

Would I have to modify my external python scripts to write out to a table in the Django db (db.sqlite3 in the Django project dir in tutorial at least), then make a Django model based on my database structure and fields?

Basically,I think my question boils down to:

1) Do I need to create Django model based on my db, then access the one and only Django "project db", and have my external script write into it.

2) or can Django utilise somehow a seperate db driven by another script somehow?

3) Finally, is Django the right tool for such a task before I invest weeks of reading...

3
  • You're just trying to get an HTML view for the data inside of your SQLite DB? If so, you probably could just open your db with the sqlite command and execute .mode html and then .dump. You could then proceed to modify styles, etc. If you think that you'll need to do more in the future (if in doubt, you probably will), then using Django may be appropriate. Commented Feb 13, 2014 at 23:00
  • Hi @fpghost, what did you end up doing? I have a similar situation (I want to make a webpage which basically shows data in my script-created database) and don't know if I should bother with django for my current project Commented Jun 25, 2014 at 17:42
  • 1
    @user929404 I did use Django in the end, and have no regrets. I used database routers tinyurl.com/865jzl5 to set up with my external script driven db, and the Django db containing the stuff like auth data. I also used the Django-tables2 app, which gives a simple way to present your data. Another option I considered was flask, which might do what you want with less of a learning curve, not sure. Commented Jun 25, 2014 at 23:01

2 Answers 2

1

If you care about taking control over every single aspect of how you want to render your data in HTML and serve it to others, Then for sure Django is a great tool to solve your problem.

Django's ORM models make it easier for you to read and write to your database, and they're database-agnostic. Which means that you can reuse the same code with a different database (like MySQL) in the future.

So, to wrap it up. If you're planning to do more development in the future, then use Django. If you only care about creating these HTML pages once and for all, then don't.

PS: With Django, you can easily integrate these scripts into your Django project as management commands, run them with cronjobs and integrate everything you develop together with a unified data access layer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Sounds like Django would be a good idea. I'm still struggling a little on the general concepts of what it would be best to do though. Should I make a model that represents my db (has the all the req fields etc), syndb, then tell my old python scripts to just start writing their data to the table in the new Django project db. Or should I add my pre-existing db to the DATABASES dictionary in settings.py and then somehow link my Django model to it?
In other words should I have a single database setup, with one database containing admin stuff plus my external db TABLE, or should I go for a multi db setup, one db for admin other other for external python apps?
The best way to write to the database would be using only Django models. So, you can write your existing scripts as Django management commands and rendering the HTML logic as Django views. And regarding using the current tables, if you write a model that describes an existing table, then you can start using it directly, without the need to syncdb. As syncdb only creates the schema if it doesn't exist.
0

Django can use different databases, sqlite is the easiest one to setup since python standard library comes with everything it needs for sqlite, while you generally need separate software to setup other databases. That's why it's used in the tutorial.

Django core itself does not require a database. You can setup a django based site without a database. However many of the modules like the authentication module does require database setup.

You don't have to write models, you can create a connection with DBAPI or other database drivers directly inside django. Of course, this means that parts of the framework that integrates with the ORM wouldn't be aware of the database connection you create yourself, like the Form class, the generic view, or the automatic admin view, so you'll be missing these features that makes Django very powerful framework.

You can create unmanaged models to allow many parts of django to be aware of your existing database and utilize Django's more advanced features with your existing database. See https://docs.djangoproject.com/en/dev/howto/legacy-databases/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.