Pregunta

I just set up the environment for an existing Django project, on a new Mac. I know for certain there is nothing wrong with the code itself (just cloned the repo), but for some reason, Django can't seem to retrieve data from the database.

  • I know the correct tables and data is in the db.
  • I know the codebase is as it should be.
  • I can make queries using the Django shell.
  • Django doesn't throw any errors despite the data missing on the web page.

I realize that it's hard to debug this without further information, but I would really appreciate a finger pointing me to the right direction. I can't seem to find any useful logs.

EDIT:

I just realized the problem lies elsewhere. Unfortunately I can't delete this post with the bounty still open.

¿Fue útil?

Solución

Without seeing any code, I can only suggest some general advice that might help you debug your problem. Please add a link to your repository if you can or some snippets of your database settings, the view which includes the database queries etc...

Debugging the view

The first thing I would recommend is using the python debugger inside the view which queries the database. If you've not used pdb before, it's a life saver which allows you to set breakpoints in your Python script and then interactively execute code inside the interpreter

>>> import pdb
>>> pdb.set_trace()
>>> # look at the results of your queries

If you are using the Django ORM, the QuerySet returned from the query should have all the data you expect.

If it doesn't then you need to look into your database configuration in settings.py.

If it does, then you must might not be returning that object to the template? Unlikely as you said the code was the same, but double check the objects you pass with your HttpResponse object.

Debugging the database settings

If you can query the database using the project settings inside settings.py from the django shell it sounds unlikley that there is a problem with this - but like everything double check.

You said that you've set up a new project on a mac. What is on a different operating system before? Maybe there is a problem with the paths now - to make your project platform independent remember to use the os.path.join() method when working with file paths.

And what about the username and password details....

Debugging the template

Maybe your template is referencing the wrong object variable name or object attribute.You mentioned that

Django doesn't throw any errors despite the data missing on the web page.

This doesn't really tell us much - to quote the Django docs -

If you use a variable that doesn’t exist, the template system will insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is set to '' (the empty string) by default.

So to check all the variables available to your template, you could use the debug template tag

{{ debug }}

Probably even better though is to use the django-debugging-toolbar - this will also let you examine the SQL queries your view is making.

Missing Modules

I would expect this to raise an exception if this were the problem, but have you checked that you have the psycopg module on your new machine?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top