1

Hi guys I'm really stuck on something developing my website with Django/Heroku. (And also using mptt)

I get the error: (again2 is the name of a table)

  DoesNotExist: again2 matching query does not exist.

I get no errors when I run locally, but I get a 500 error when I deploy to Heroku. So something has gone wrong in terms of communication between Heroku and my Django database. But I have no idea what to do to fix this?

A list of things I have done (looking at other people's questions):

  • python manage.py makemigrations followed by git commit, pushing to heroku and heroku run manage.py migrate
  • i have imported ''django.contrib.sites' into my installed apps, i have my herokuapp url saved as a site object
  • i have an updated requirements.txt
  • heroku appears to have 'data' and tables in the database it created for the app (heroku postgres)

Below is what I would have in my views:

 def topic_detail(request, x):
     text1 = str(again2.objects.get(pk='Top').get_children())
    return render(request, 'things/topic_detail.html', {
        'text1': text1,
   })

In local this would work, on deployment it would give the error, if i replaced text1 to just be again2.objects.all() it would show the contents on local but nothing on deployment to heroku (with no errors) Essentially what I'm trying to do is (for now) show the children of Top which is a member of again2.

I can't really move forward until I figure this out, your help would be much appreciated, if there is anything else you need let me know please, thank you in advance

nickynw
  • 35
  • 5
  • "heroku appears to have 'data'" what doe you mean by appears to have? Surely you can confirm it – e4c5 Jun 25 '17 at 00:40
  • I can see that it has my heroku app has a datastore, a Heroku Postgres : Database that contains tables, 8.4mb. – nickynw Jun 25 '17 at 00:48
  • Ah but did you check what the tables contain? – e4c5 Jun 25 '17 at 00:48
  • i dont have the software to do that right now but in notepad it contains the phrase again2 but not the contents – nickynw Jun 25 '17 at 00:58

2 Answers2

1

This has nothing to do with migrations, requirements files or virtualenvs. DoesNotExist only means one thing. The object does not exist in your database. When you are using .get in this manner you sould always wrap it in try expect or use get_object_or_404 instead

def topic_detail(request, x):
    try: 
        text1 = str(again2.objects.get(pk='Top').get_children())
        return render(request, 'things/topic_detail.html', {
           'text1': text1,
       })
    except again2.DoesNotExist
        raise Http404

or more concisely

   text = get_object_or_404(again2, pk='Top')

You can confirm that your database does not have this record by using the cli

Now for some unsolicited advice. Please get into the habit of starting class names with an upper case letter.

Update: migrations

Django migrations are a painless way of managing the schema. A simple way to make changes to the models and have them reflected on the database. Applying migrations do not result in data from one server being moved to another. For that you need

./manage.py dumpdata > dumpfile.json
./manage.py loaddata dumpfile.json

Or you can use the posgresql COPY FROM/TO which is more efficient.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • i appreciate the advice as I've only begun learning very recently, one thing I do not understand here is that, surely if the object exists in my local database and i am trying to migrate that to heroku, why would it then tell me the object does not exist? (the solution to try and except works fine but if thats the case I will never be able to retrieve any objects as I appear to be unable to get any of them) – nickynw Jun 25 '17 at 01:14
  • you need to export them and then import them as explained in my update – e4c5 Jun 25 '17 at 01:18
  • i ran both of those in the commandline, but it does not seem to have done anything, is there any more specific information on where to run these? sorry i'm so clueless – nickynw Jun 25 '17 at 01:34
  • The dump on the local, the load on the live – e4c5 Jun 25 '17 at 02:10
  • It does work. Never use the term this does not work here in stackoverflow unless you are looking for downvotes. Please explain what you tried and what happened. If you refer the dumpdata and loaddata documentation you can confirm for yourself that this __does indeed work__ – e4c5 Jun 25 '17 at 16:21
  • i used manage.py dumpdata > dumpfile.json and then heroku run python manage.py loaddata dumpfile.json, it said Installed 116 object(s) from 1 fixture(s), after pushing to heroku and looking at the website, the same problem occurs, which documentation do you mean, or have i followed your instructions incorrectly? again, I've not spent much time here so I appreciate your helping me, I apologise for not providing any more info, as one would expect I was just a bit frustrated – nickynw Jun 25 '17 at 17:07
  • Documentation for manage.py would come in handy.. But according to your comment 116 fixtures have been installed. You can check it by connecting to the database using the console. Check the row counts in the server against the counts in the local – e4c5 Jun 25 '17 at 17:10
  • thank you for your help! i ended up having the same issues as this: https://stackoverflow.com/questions/3076928/switching-django-project-from-sqlite3-backend-to-postgresql-failes-when-loading however i have now got it working (and i definitely never would have gotten there without you) :) – nickynw Jun 25 '17 at 21:34
  • Glad to have been of help and all the best with your project – e4c5 Jun 26 '17 at 00:57
0

This will solve your issue:

  1. First, run this from the terminal in your project root:

    python3 manage.py dumpdata > dumpfile.json

  2. Then deploy it to Heroku. ( I assume you know this)

  3. After deploy, run this from the terminal in your project root:

    heroku run bash

This will start the heroku bash in which you can see your hosted files by typing ls. Here you can now see the dumpfile.json

run python3 manage.py loaddata dumpfile.json and this will transfer all your objects to the Heroku database.

Also to view the same in your Django admin, you need to create superuser in the Heroku bash again and set up the credentials.

python3 manage.py createsuperuser

Then set the username, email and password. Now you can go to your Django admin and verify the database objects transfer successfully.

Hope it helped!

Community
  • 1
  • 1
Shaswat Lenka
  • 544
  • 5
  • 11