-1

I am new to django, I have pretty good idea of basics. I am able to build model(Django database) and use it in templates. But now I want to connect external database to the django templates. How do I do that? I referred to the following link - Pulling data to the template from an external database with django But I am still running into errors.

My views.py file looks like this :


    def view(request):
        conn = sqlite3.connect("data_new.db")
        try:
            cur = conn.cursor()
            cur.execute("delete from data_new where date = ''; ")
            cur.execute("select * from data_new;")
            results = cur.fetchall()
        finally:
            conn.close()
        return render("main.html",{"results": results})

Following error is displayed when I run it on server : -

TypeError at /

join() argument must be str or bytes, not 'dict'
Ishaan007
  • 115
  • 9
  • your question isn't clear, what's for you the difference between "django database" and "external database"? Have you created django models and are they managed, i.e. did you make migrations and migrated? Why are you trying to fetch data with raw SQL rather than using your models? You need to explain a bit more what you're trying to achieve. – dirkgroten May 27 '19 at 09:38
  • Also, show us the full error trace. – dirkgroten May 27 '19 at 09:40
  • Your problem has nothing to do with "external databases", cf Chris Curvey's answer. This being said this code snippet is horrible. Django has support for multiple databases and unmanaged models (models built from schema that are not managed by django itself - IOW "legacy" databases) so please use those features instead. Also a GET request __MUST__ be idempotent (it must NOT change the server's state), so you may want to think twice about this "delete" query. – bruno desthuilliers May 27 '19 at 10:45

1 Answers1

1

you forgot to pass request to render()

return render(request, "main.html",{"results": results})
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70