Issue
I'm changing view of homepage with app names pages. I've added pages to settings. this is how directory look like:
- trydjango
- src
- pages
- init
- views
- products
- trydjango
- init
- settings
- urls
- manage
- pages
- src
views' code:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello Again</h1>")
urls' code
from django.contrib import admin
from django.urls import path
from src.pages.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
]
and I see this error when I run server
ModuleNotFoundError: No module named 'src'
Solution
First you need to understand what an app in Django is compared to a project.
When you register an app django will look in the project root folder when you try to import it.
Your project root is where your manage.py
file is. In your case the src
folder.
So when you want to import your views module you need to state
from pages.views
rather than
from src.pages.views
I suggest that you read through and follow (by coding it yourself) the Django tutorial to learn more about project structure and creating your own apps with models, urls etc.
Answered By - ms-
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.