Search code examples
djangodjango-database

Django database connection issue


I am trying to teach myself Python and Django and so far I am doing ok but I have hit a snag. I have been following the Django MVA as well as using the "Hello Web App" book and searching the web for help when needed but I can't seem to get past this one so here goes...

I set up a very simple web app with Django with only one table and one model. I was able to set up the admin module and I can view and manipulate the data in my database in the admin view and the shell but when I launch the site my view doesn't seem to be finding any of the data.

My Views.py

from django.shortcuts import render, render_to_response
from django.http import HttpRequest, HttpResponse
from django.template import RequestContext
from datetime import datetime
from app.models import Order
from app.models import *;



def MMIR(request):
    order_list = Order.objects.all();
    return render(request, 'app/MMIR.html',{'oder_list':order_list});

My template: MMIR.html

{% extends "app/layout.html" %}
{% block content %}

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>MMIRs</title>
</head>
<body>
    <h2>MMIRs</h2>
        <ul>
           {% for order in order_list %}
               <li>{{order.MMIR}}</li>
            {%empty%}
                <li>Sorry there are no orders to display</li>
           {% endfor %}
         </ul>

</body>
</html>
{% endblock %}  

My database connection string in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'C:/Users/USER/Documents/Visual Studio  2015/Projects/DjangoWebProject2/DjangoWebProject2/db.sqlite3',  

And my models.py:

from django.db import models

# Create your models here.

class Order(models.Model):
    MMIR = models.CharField(max_length=10)
    AWB = models.CharField(max_length=25)
    Shipper = models.CharField(max_length=20)
    Vendor = models.CharField(max_length=25)
    order_type = models.CharField(max_length=25) 

When I go to the MMIR page I always get the "sorry there are no orders to display" message when I know there are orders in the database. I have looked everywhere I can think of but I can't seem to figure out what I have done wrong. Can anyone point me in the right direction?

Thanks Max


Solution

  • Look for the typo here:

    return render(request, 'app/MMIR.html',{'oder_list':order_list});

    change that to

    return render(request, 'app/MMIR.html',{'order_list':order_list});
    

    and I suspect you'll be in good shape.