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
CodePudding user response:
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.
CodePudding user response:
You can go to:
PyCharm > Preferences > Project > Project Structure
Apply > Ok
And try again.