ModuleNotFoundError: No module named 'rango' - python

I am trying to use Python shell to import a model, getting error:
ModuleNotFoundError: No module named 'rango'
I've also noticed in my urls.py i am getting 'Unresolved Import:
views'
I think my project structure might be the cause of both errors, I
used eclipse to create django project for the first time.
I have added rango app in the installed apps in setting, just as: 'rango',
HERE IS THE SCREEN FOR PROJECT STRUCTURE AND ERROR: https://imgur.com/a/WlfNzEN
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context_dict = {'boldmessage': "Crunchy, creamy, cookie, candy, cupcake!" }
return render(request, 'rango/index.html', context=context_dict)
models.py
from django.db import models
class Category(models.Model):
# Unique TRUE attr means the name must be unique - can be used as a primary key too!
name = models.CharField(max_length=128, unique=True)
def __str__(self):
return models.Model.__str__(self)
class Page(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __str__(self):
return models.Model.__str__(self)
class user_session(models.Model):
userNAME = models.CharField(max_length=120, unique=True)
addToCarts = models.IntegerField(default=0)
def __str__(self):
# __unicode__ on Python 2
return self.headlin

if your urls.py are in the same folder with views.py you can try it
from . import views
but if the urls.py are in the Tango folder try
from ..rango import views
also can you try to rename you first Tango folder, here can came error
Tango/
Tango/
...
rongo/
Tango/
...
try to rename first Tango folder to be something like this
Projects/
Tango/
...
rongo/
Tango/
...

Related

The model is not displayed in the django admin panel

I don't have the advertisement module displayed in the django admin panel. Here is the model code
from django.db import models
class Advertisement(models.Model):
title = models.CharField(max_length=1000, db_index=True)
description = models.CharField(max_length=1000, default='', verbose_name='description')
creates_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
price = models.FloatField(default=0, verbose_name="price")
views_count = models.IntegerField(default=1, verbose_name="views count")
status = models.ForeignKey('AdvertisementStatus', default=None, null=True, on_delete=models.CASCADE,
related_name='advertisements')
def __str__(self):
return self.title
class Meta:
db_table = 'advertisements'
ordering = ['title']
class AdvertisementStatus(models.Model):
name = models.CharField(max_length=100)
admin.py /
from django.contrib import admin
from .models import Advertisement
admin.site.register(Advertisement)
I was just taking a free course from YouTube. This was not the case in my other projects. Here I registered the application got the name in INSTALLED_APPS. Then I performed the creation of migrations and the migrations themselves. Then I tried to use the solution to the problem here , nothing helped. I didn't find a solution in Google search either.
127.0.0.1:8000/admin/
console
admins.py
The name of the file is admin.py not admins.py. Yes, that is a bit confusing since most module names in Django are plural. The rationale is probably that you define a (single) admin for the models defined.
Alternatively, you can probably force Django to import this with the AppConfig:
# app_name/apps.py
from django.apps import AppConfig
class AppConfig(AppConfig):
def ready(self):
# if admin definitions are not defined in admin.py
import app_name.admins # noqa

ModuleNotFoundError: No module named 'musiclibrary.song'

There is an issue while importing model 'Artist' of my django app in views.py.
from musiclibrary.song.models import Artist
when I runserver it gives ModuleNotFoundError.
from django.shortcuts import render
from django.http import HttpResponse
from musiclibrary.song.models import Artist
def hello_world(request):
return HttpResponse("Hello World!")
def home(request):
return render(request, "home.html")
def artist(request):
artist_list = Artist.objects.all(). //// I have to make this line of code work
context = {'artist_list': artist_list}
return render(request, 'artist.html', context)
Models code:
from django.db import models
class Artist(models.Model):
name = models.CharField(max_length=250)
country = models.CharField(max_length=150)
birth_year = models.IntegerField()
genre = models.CharField(max_length=150)
class Song(models.Model):
Title = models.CharField(max_length=250)
release_date = models.IntegerField()
length = models.DateField()
artist = models.ForeignKey('Artist', on_delete=models.CASCADE)
Error log:
File "/Users/m.zcomputer/PycharmProjects/myFirstApp/musiclibrary/musiclibrary/views.py", line 4, in <module>
from musiclibrary.song.models import Artist
ModuleNotFoundError: No module named 'musiclibrary.song'
This is how my project is organized
You can go to:
PyCharm > Preferences > Project > Project Structure
Mark this module as Source
Like this
Apply > Ok
And try again.
you got that error bacause you don't import correctly.
from song.models import Artist
more :
when you want to import anything from your models.py or etc , you must import them from appname and your appname is song not musiclibrary.

Django FileNotFoundError at /admin

I have model registered on admin page:
models.py
from django.db import models
class Question(models.Model):
cat = models.IntegerField()
quest = models.TextField(max_length=200)
answer = models.CharField(max_length=1000)
path = models.FilePathField()
date = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(default=0)
def __str__(self):
return f'{self.cat} - {self.quest}'
admin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
and I can see a database through admin page:
https://i.stack.imgur.com/SuCcX.png
but I can't click on any record of the table and modify it due to an error:
https://i.stack.imgur.com/M6W5a.png
I did it many times since now, and I have never encountered such an error.
Does anybody have an idea how to fix it?
Acorrding to Django docs FilePathField() has one required argument path, which is:
"The absolute filesystem path to a directory from which this FilePathField should get its choices."
So you need modify your models.py:
class Question(models.Model):
...
path = models.FilePathField(path='/home/images')
...

Django book chapter 6: unable to import Book model

I am learning Django going through the online book, and am presently stuck at Chapter 6. In the Adding Your Models to the Admin Site section the reader is required to create a file named admin.py, within the books app, with this content:
from django.contrib import admin
from mysite.books.models import Publisher, Author, Book
admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)
This is supposed to make these models available for editing at the admin site, but what I get is the following:
ImportError at /admin/ No module named books.models
In the Python command line I get a similar error:
>>> from mysite.books.models import Book
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysite.books.models
These are the contents of the models.py file:
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
I have strictly followed the instructions in the book to this point, thus I should have an exact copy of the code used. The file structure was created automatically by Django:
books
__init__.py
admin.py
models.py
tests.py
views.py
mysite
templates
__init__.py
settings.py
urls.py
views.py
wsgi.py
manage.py
What would be the correct way of importing the models file in the admin module?
This is pretty old book and some things are deprecated in current version of Django.
Try this import:
from books.models import Publisher, Author, Book
instead of:
from mysite.books.models import Publisher, Author, Book

Django: ImportError: cannot import name Count

I just pulled from my github and tried to setup my application on my Ubuntu (I originally ran my app on a Mac at home).
I re-created the database and reconfigured the settings.py -- also update the template locations, etc.
However, when I run the server "python manage.py runserver" get an error that says:
ImportError: cannot import name Count
I imported the Count in my views.py to use the annotate():
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.db.models import Count
from mysite.blog.models import Blog
from mysite.blog.models import Comment
from mysite.blog.forms import CommentForm
def index(request):
#below, I used annotate()
blog_posts = Blog.objects.all().annotate(Count('comment')).order_by('-pub_date')[:5]
return render_to_response('blog/index.html',
{'blog_posts': blog_posts})
Why is not working?
Also, if I remove the "import Count" line, the error goes away and my app functions like normal.
Thanks,
Wenbert
UPDATE:
my models.py looks like this:
from django.db import models
class Blog(models.Model):
author = models.CharField(max_length=200)
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.content
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Comment(models.Model):
blog = models.ForeignKey(Blog)
author = models.CharField(max_length=200)
comment = models.TextField()
url = models.URLField()
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.comment
UPDATE 2
My urls.py looks like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^blog/$','mysite.blog.views.index'),
(r'^display_meta/$','mysite.blog.views.display_meta'),
(r'^blog/post/(?P<blog_id>\d+)/$','mysite.blog.views.post'),
)
This sounds like you're not using Django 1.1. Double check by opening up the Django shell and running
import django
print django.VERSION
You should see something like (1, 1, 0, 'final', 0) if you're using 1.1
I've updated my Django and it turns out that your import statement is correct as module structure was changed a bit. Are you sure your Django is of latest version?

Categories