Alan66
Alan66

Reputation: 21

ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package while importing models from database

I am trying to pull the data from admin pannel. But when i print the post object here,it throws the error. I have tried almost every method but nothing seems working

views.py

from django.template import RequestContext

from django.shortcuts import render, get_object_or_404

from .models import Mam


def index(request):
    question = Mam.objects.all()
    #question = get_object_or_404(Mam)
    context = {'latest_question_list': question}
    return render(request, 'index.html', context)

models.py

from django.db import models

# Create your models here.
class Mam(models.Model):
    name = models.TextField(max_length=60)

    def __str__(self):
        return self.name

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    {%for i in context %}
        <p>{{  i }}</p>
    {% endfor%}`enter code here`
</head>
<p></p>
<body>

</body>
</html>

Upvotes: 0

Views: 174

Answers (1)

Rom101
Rom101

Reputation: 11

This occurs because you might be running the python file directly. In order to properly import the django model, you should let the django server start.

See here for an answer to a similar problem : Django Tutorial. No module named ‘main.models’

Also, check there are no relative path in the part of your file that is executed directly.

Upvotes: 1

Related Questions