29đź‘Ť
No problem! I do this all the time.
As far as the “don’t edit or update the data”, just don’t add anything to your app that would update the data. Salem’s suggestion about using permissions on the MySQL side is a good idea as well.
For retrieving the data, you have two options:
1) You can create Django models that correspond to your tables in the MySQL database. You can do this manually, or you can use the “inspectdb” command with manage.py to give yourself a good starting point. Then do something like this:
def myview(request):
rows = MyModel.objects.using('mysql').all()
return render_to_response("mytemplate.html", {"rows" : rows })
2) You can manage the connections and queries manually within your app. This is perfectly valid within a view:
def myview(request):
conn = MySQLdb.connect("connection info here")
try:
cursor = conn.cursor()
cursor.execute("select * from mytable")
rows = cursor.fetchall()
finally:
conn.close()
return render_to_response("mytemplate.html", {"rows" : rows})
finally — Django is perfectly happy to use MySQL as a database. It might simplify things if your DBA will let Django create its tables right in the same database.
2đź‘Ť
To make your access to the database “read only”, I guess the best option is to create a limited used in the MySQL side with only SELECT:
GRANT SELECT ON target_database.* TO your_user@'your_host' IDENTIFIED BY 'your_password';
This will make sure that in any case an update/alter will succeed.
Usually you model your database tables as objects because this makes it easier to work with database records from Python and gives you some abstraction, but you can execute raw SQL queries if you feel this is the right thing to do.
Depending on how you want to present your data, you may need to convert it to something.
If your want to make your application more dynamic (for example, retrieving new data in 10 seconds intervals and presenting it to the user without refresh) you probably will need to convert it to some format more suitable to be used with AJAX, like JSON or XML (Django has some serialization tools ready to be used). If you just want a “static” application( ie: user clicks in a link/button and goes to a page where data is presented, and to be refreshed user has to refresh the page) you can use the objects as retrieved from the database in your view.
- [Django]-Using JSON in django template
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
- [Django]-How do I reuse HTML snippets in a django view
1đź‘Ť
There is going to be no problem if the data is being updated.
Now for retrieving the data from database you first have to import the concerned model
in your views
from app_name.models import model_name
def view_report(request):
r_name=request.POST.get('r_name','default_value')
r=model_name.objects.get(report_name=r_name)
return render_to_response('url',{'r':r})
In your template
{{r.report_desc}}
- [Django]-Visual Editor for Django Templates?
- [Django]-Django 2, python 3.4 cannot decode urlsafe_base64_decode(uidb64)
- [Django]-Django Rest JWT login using username or email?